我通过php将一些数据插入到mysql数据库中。问题是,当我尝试将空值插入到列中时,它不会像这样存储: NULL 并且它实际上将NULL保存为文本或当我从我的php脚本中删除if语句时它插入该列中的可用空间。 这是我的PHP代码:
<?php
register();
function connectToDatabase(){
$connection=mysqli_connect("localhost","username","pass","db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($connection,"utf8");
return $connection;
}
function register(){
$connection = connectToDatabase();
$OrgID = $_REQUEST['OrgID'];
$OrgName = $_REQUEST['OrgName'];
$OrgAddress = $_REQUEST['OrgAddress'];
$OrgPhone = $_REQUEST['OrgPhone'];
if($OrgName!=""){
$check = mysqli_query($connection,"select count(*) from organization where org_name='$OrgName'");
$row = mysqli_fetch_array($check);
if($row[0]>=1){
print "old_user";
}else {
$result = mysqli_query($connection,"insert into organization (org_id,org_name,org_address,org_phone) values('$OrgID','$OrgName','$OrgAddress','$OrgPhone')");
if($result!=""){
print "ok";
}else if($result==""){
print "no";
}
mysqli_close($connection);
}
}else {
print "null";
}
}
?>
当我把if语句检查为这样时,它将NULL保存为文本:
if($OrgID == '')
{
$OrgID = 'NULL';
}
这是我的表结构:
CREATE TABLE IF NOT EXISTS `organization` (
`org_pid` int(11) NOT NULL AUTO_INCREMENT,
`org_id` varchar(16) DEFAULT NULL,
`org_name` varchar(45) NOT NULL,
`org_address` varchar(255) DEFAULT NULL,
`org_phone` varchar(45) NOT NULL,
PRIMARY KEY (`org_pid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
答案 0 :(得分:0)
null
应设置为:
$OrgID = null;
即。没有引号。