2017-04-12 82 views
1

我用插入在PHP中值是这样的:PHP INSERT的MySQL 5.7

$sql = " INSERT INTO `table` (name, email) VALUES ('$name','$email') "; 

考虑到我的表有以下几列:ID,姓名,电子邮件地址,电话

在MySQL 5.7它不会让我再做这个查询,因为它说地址没有默认值。

我为它添加一个默认值在MySQL,但有些领域不容许默认值,像一滴,文字...

我的问题是:我总是有来形容我的表中的每一列我查询,所以它可以工作?像:

$sql = " INSERT INTO `table` (id,name, email, address, phone) 
VALUES ('', '$name','$email', '', '') "; 
+3

***你的脚本是在对SQL注入攻击的风险。(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) ***。 [Escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)并不安全!了解[PDO](http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi]([准备](http://en.wikipedia.org/wiki/Prepared_statement) http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)并考虑使用PDO,[这真的很简单](http://jayblanchard.net/demystifying_php_pdo.html)。 –

+2

为什么连接字符串而不是使用[prepared statements](https://secure.php.net/manual/en/pdo.prepare.php)? – Artemis

+1

如果你可以有一个BLOB列的默认值,它会是什么? –

回答

0

你不必来描述你的每查询每列,但特别是对INSERT查询,你必须确保该列你指定要么有一个默认值,或者可以是NULL

对于其他查询,如SELECTUPDATE,你可以选择你想要的那些,而不考虑的内容是什么(当然同样适用于UPDATEINSERT,即不能为NULL列,不能有在它的NULL值)。

你可以改变你的台,使得你并不总是插入任一值有一个默认值,或者只是在默认情况下将它们设置为NULL(如果没有在插入该列提供一个值)。

ALTER TABLE table_name 
CHANGE column_name column_name type DEFAULT NULL