当前位置:Gxlcms > PHP教程 > mysql数据库不能添加NULL值

mysql数据库不能添加NULL值

时间:2021-07-01 10:21:17 帮助过:7人阅读

数据都验证了,但是不能给数据添加NULL值,报错如下:
Column 'loumiandijia' cannot be null

其他数据在为空是用NULL都不行

代码如下
 1)) ) {		$city = mysqli_real_escape_string ($conn, $trimmed['city']);;	} else {		$errors[] = '请选择区域';	}	$weizhi = (!empty($trimmed['weizhi'])) ? mysqli_real_escape_string ($conn, $trimmed['weizhi']) : NULL;	$yongtu = (!empty($trimmed['yongtu'])) ? mysqli_real_escape_string ($conn, $trimmed['yongtu']) : NULL;	$rongjilv = (!empty($trimmed['rongjilv'])) ? mysqli_real_escape_string ($conn, $trimmed['rongjilv']) : NULL;	$midu = (!empty($trimmed['midu'])) ? mysqli_real_escape_string ($conn, $trimmed['midu']) : NULL;	$lvdi = (!empty($trimmed['lvdi'])) ? mysqli_real_escape_string ($conn, $trimmed['lvdi']) : NULL;		if(empty($trimmed['fabu_time'])) {		$errors[] = '发布日期不能为空';	} else {		$fabu_time = mysqli_real_escape_string ($conn, $trimmed['fabu_time']);	}	if(empty($trimmed['chengjiao_time'])) {		$errors[] = '成交日期不能为空';	} else {		$chengjiao_time = mysqli_real_escape_string ($conn, $trimmed['chengjiao_time']);	}	if(empty($trimmed['jiezhi_time'])) {		$errors[] = '保证金截止时间不能为空';	} else {		$jiezhi_time = mysqli_real_escape_string ($conn, $trimmed['jiezhi_time']);	}		if (is_numeric($trimmed['baozhengjin']) && ($trimmed['baozhengjin'] > 0)) {		$baozhengjin = mysqli_real_escape_string ($conn, (float) $trimmed['baozhengjin']);	} else {		$errors[] = '请输入一个正确的竞买保证金(万元)';	}	if (is_numeric($trimmed['mianji_m']) && ($trimmed['mianji_m'] > 0)) {		$mianji_m = mysqli_real_escape_string ($conn, (float) $trimmed['mianji_m']);	} else {		$errors[] = '请输入一个正确的占地面积(亩)';	}	if (is_numeric($trimmed['mianji_p']) && ($trimmed['mianji_p'] > 0)) {		$mianji_p = mysqli_real_escape_string ($conn, (float) $trimmed['mianji_p']);	} else {		$errors[] = '请输入一个正确的占地面积';	}	if (is_numeric($trimmed['guihua_p']) && ($trimmed['guihua_p'] > 0)) {		$guihua_p = mysqli_real_escape_string ($conn, (float) $trimmed['guihua_p']);	} else {		$errors[] = '请输入一个正确的规划建筑面积';	}	if (is_numeric($trimmed['qipaijia']) && ($trimmed['qipaijia'] > 0)) {		$qipaijia = mysqli_real_escape_string ($conn, (float) $trimmed['qipaijia']);	} else {		$errors[] = '请输入一个正确的起拍价(万/亩)';	}	$chengjiaodanjia = (!empty($trimmed['chengjiaodanjia'])) ? mysqli_real_escape_string ($conn, (float) $trimmed['chengjiaodanjia']) : NULL;	$chengjiazongjia = (!empty($trimmed['chengjiazongjia'])) ? mysqli_real_escape_string ($conn, (float) $trimmed['chengjiazongjia']) : NULL;	$loumiandijia = (!empty($trimmed['loumiandijia'])) ? mysqli_real_escape_string ($conn, (float) $trimmed['loumiandijia']) : NULL;	$yijialv = (!empty($trimmed['yijialv'])) ? mysqli_real_escape_string ($conn, $trimmed['yijialv']) : NULL;	$jingderen = (!empty($trimmed['jingderen'])) ? mysqli_real_escape_string ($conn, $trimmed['jingderen']) : NULL;	$churang_p = (!empty($trimmed['churang_p'])) ? mysqli_real_escape_string ($conn, (float) $trimmed['churang_p']) : NULL;	$dituweizhi = (!empty($trimmed['dituweizhi'])) ? mysqli_real_escape_string ($conn, $trimmed['dituweizhi']) : NULL;	$xuzhi = (!empty($trimmed['xuzhi'])) ? mysqli_real_escape_string ($conn, $trimmed['xuzhi']) : NULL;	if ( isset($trimmed['is_cheng']) && is_numeric($trimmed['is_cheng']) && ($trimmed['is_cheng'] >= 0) ) {		$is_cheng = mysqli_real_escape_string ($conn, $trimmed['is_cheng']);;	} else {		$errors[] = '请选择交易是否成功';	}			if (empty($errors)) {		$q = 'INSERT INTO f_tudi (tudi_id, city_id, weizhi, yongtu, rongjilv, midu, lvdi, fabu_time, chengjiao_time, jiezhi_time, baozhengjin, mianji_m, mianji_p, guihua_p, qipaijia, chengjiaodanjia, chengjiazongjia, loumiandijia, yijialv, jingderen, churang_p, dituweizhi, xuzhi, is_cheng) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';		$stmt = mysqli_prepare($conn, $q);		mysqli_stmt_bind_param($stmt, 'iissssssssdddddddddsdssi', $tudi_id, $city, $weizhi, $yongtu, $rongjilv, $midu, $lvdi, $fabu_time, $chengjiao_time, $jiezhi_time, $baozhengjin, $mianji_m, $mianji_p, $guihua_p, $qipaijia, $chengjiaodanjia, $chengjiazongjia, $loumiandijia, $yijialv, $jingderen, $churang_p, $dituweizhi, $xuzhi, $is_cheng);		mysqli_stmt_execute($stmt);		if (mysqli_stmt_affected_rows($stmt) == 1) {			echo '这条数据已经添加成功';			$_POST = array();			} else {			echo '

程序发生错误,请重新添加

'; echo '

' . mysqli_stmt_error ($stmt) . '

'; } mysqli_stmt_close($stmt); } } if ( !empty($errors) && is_array($errors) ) { echo '

发生下列错误:
'; foreach ($errors as $msg) { echo " - $msg
\n"; } echo '请在试一遍

';}?>


代码太多 分开发


回复讨论(解决方案)

	土地数据添加		    

字段设置为not null了吧
$loumiandijia = (!empty($trimmed['loumiandijia'])) ? mysqli_real_escape_string ($conn, (float) $trimmed['loumiandijia']) : NULL;

改为
$loumiandijia = (!empty($trimmed['loumiandijia'])) ? mysqli_real_escape_string ($conn, (float) $trimmed['loumiandijia']) : ‘’;
试试

字段设置为not null了吧
$loumiandijia = (!empty($trimmed['loumiandijia'])) ? mysqli_real_escape_string ($conn, (float) $trimmed['loumiandijia']) : NULL;

改为
$loumiandijia = (!empty($trimmed['loumiandijia'])) ? mysqli_real_escape_string ($conn, (float) $trimmed['loumiandijia']) : ‘’;
试试


没有设置字段设置为not null
你看下表

但是我换了一个写法就可以写入数据库,这是怎么回事?
	if (empty($errors)) {		$q = "INSERT INTO f_tudi (tudi_id, city_id, weizhi, yongtu, rongjilv, midu, lvdi, fabu_time, chengjiao_time, jiezhi_time, baozhengjin, mianji_m, mianji_p, guihua_p, qipaijia, chengjiaodanjia, chengjiazongjia, loumiandijia, yijialv, jingderen, churang_p, dituweizhi, xuzhi, is_cheng) VALUES ('$tudi_id', '$city', '$weizhi', '$yongtu', '$rongjilv', '$midu', '$lvdi', '$fabu_time', '$chengjiao_time', '$jiezhi_time', '$baozhengjin', '$mianji_m', '$mianji_p', '$guihua_p', '$qipaijia', '$chengjiaodanjia', '$chengjiazongjia', '$loumiandijia', '$yijialv', '$jingderen', '$churang_p', '$dituweizhi', '$xuzhi', '$is_cheng')";		$r = mysqli_query ($conn, $q) or trigger_error("Query: $q\n
MySQL Error: " . mysqli_error($conn)); if (mysqli_affected_rows($conn) == 1) { echo '这条数据已经添加成功'; $_POST = array(); } else { echo '

程序发生错误,请重新添加

'; } }


还有一个问题是我写入数据库后为空的值不少NULL而是里面是什么都没有,空的,这个和我看教程的不一样,教程里面空的值就是一个NULL,这是怎么回事?

是你插入的时候是null吧,我看你的表设计里面是都不能为null的你还传入这个那肯定会报错的。

是你插入的时候是null吧,我看你的表设计里面是都不能为null的你还传入这个那肯定会报错的。


默认是否不行吗?还要怎么设置

否的那列,就是表示 NULL,既然 NULL?定?否。你?NULL?去?然?啊。

你新的那??法?把null??''的,所以可以。

人气教程排行