我有一个数字框<input type='number' step='any' name='number' id='number'>
,我取这个值,然后在函数中调用它,像这样:
insertIntoDB($number)
function insertIntoDB($number = null){
//insert into db
"INSERT INTO 'tablename' (number) VALUES " . $number;
}
,我得到这个错误:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' '
是否有离开,所以如果用户留下空白字段,它将输入值为空?
您可以在创建表本身时设置默认值,但除此之外,您粘贴的代码缺少一些括号:
INSERT INTO `tablename` (number) VALUES (" . $number.")";
如果想在表中插入和获取默认值时使用NULL,可以在创建表时这样做:
CREATE TABLE test
(
id INT NOT NULL AUTO_INCREMENT,
someField VARCHAR(15) NOT NULL DEFAULT 'on'
)
现在,当您尝试插入数据时,您可以执行以下操作:
insert into `tablename` (id, someField) values (null, null)
并在列someField
中获得具有递增ID和字符串'on'的行。
如果您想在向表中插入数据时检查是否为空,那么您可以使用以下内容来确保您有值:
$someField=(isset($number))?$number:'null';
INSERT INTO `tablename` (number) VALUES (" . $someField.")";
这是假设您在发送表单时已经验证了$number是数字或未提交。