在这里的示例中,NULL值通常是我通过的其他变量.我试图传递NULL以查看是否存在问题,因为这些是唯一可以为NULL的值,其他一切都是必需的.
mysql_query("INSERT INTO `imstillr_crm`.`customerinfo`
(`id`, `companyname`, `primarycontact`, `primaryemail`,
`prefphone`, `secondarycontact`, `secondaryemail`, `optionalphone`,
`department`, `website`)
VALUES
(NULL, '".$company."', '".$primarycontact."', '".$primaryemail."',
'".$prefphone."', 'NULL', 'NULL', 'NULL,
'".$department."', '".$website."')");
Run Code Online (Sandbox Code Playgroud)
我只是用较少的变量尝试了这个并且它起作用了.我有点困惑.我甚至插入了一条记录并复制了SQL字符串并添加了我的变量.
我想你在这里犯了一个错误:
'NULL, '".$department.
Run Code Online (Sandbox Code Playgroud)
想必是你打算的
'NULL', ".$department."
Run Code Online (Sandbox Code Playgroud)
但是:这看起来好像你试图将文字文本"NULL"插入数据库,我认为这不是你想要的.NULL是一个关键字,因此您只需在插入空值的地方使用NULL字(因此不需要引号).
但是,除了当前问题之外,还有一些风险会让您分心,您实际上不应该通过编写字符串来执行SQL语句.这就是SQL注入攻击成功的方式.更好的方法是将PDO与预准备语句一起使用.以下是如何使用它的简要说明:
http://www.php.net/manual/en/pdo.prepared-statements.php
主要优点是:
为了完整起见,这是您的代码在PDO中的外观
$stmt = $dbh->prepare("INSERT INTO imstillr_crm.customerinfo (id, companyname, primarycontact, primaryemail, prefphone, secondarycontact, secondaryemail, optionalphone, department, website) VALUES (:id, :companyname, :primarycontact, :primaryemail, :prefphone, :secondarycontact, :secondaryemail, :optionalphone, :department, :website)");
$stmt->bindParam(":id", NULL);
$stmt->bindParam(":companyname", $company);
$stmt->bindParam(":primarycontact", $primarycontact);
...
$stmt->execute();
Run Code Online (Sandbox Code Playgroud)
请注意您不需要在值周围加上引号.它们可能包含各种字符,如引号,半冒号等,这些不会导致数据库出现任何问题(它们可能会导致应用程序中的其他问题,但这是另一个讨论).