mysql数据库不能添加NULL值

WBOY
Lepaskan: 2016-06-23 13:48:26
asal
1177 orang telah melayarinya

数据都验证了,但是不能给数据添加NULL值,报错如下:

Column 'loumiandijia' cannot be null
Salin selepas log masuk

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

代码如下
<?phprequire ('inc/config.php');$page_title = '住宅数据添加-feige数据中心';include ('inc/header.php');require (MYSQL);if ($_SERVER['REQUEST_METHOD'] == 'POST') {		$trimmed = array_map('trim', $_POST);	$errors = array();	if (!empty($trimmed['tudi_id'])) {		$tudi_id = mysqli_real_escape_string ($conn, $trimmed['tudi_id']);	} else {		$errors[] = '地块编号不能为空';	}		if ( isset($trimmed['city']) && filter_var($trimmed['city'], FILTER_VALIDATE_INT, array('city' => 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 '<p style="font-weight: bold; color: #C00">程序发生错误,请重新添加</p>'; 			echo '<p>' . mysqli_stmt_error ($stmt) . '</p>';		}			mysqli_stmt_close($stmt);		}			} if ( !empty($errors) && is_array($errors) ) {	echo '<p style="font-weight: bold; color: #C00">发生下列错误:<br />';	foreach ($errors as $msg) {		echo " - $msg<br />\n";	}	echo '请在试一遍</p>';}?><?phpinclude ('inc/footer.php');?>
Salin selepas log masuk


代码太多 分开发


回复讨论(解决方案)

<div class="portlet wrap mT50">	<div class="portlet-title">土地数据添加</div>	<div class="portlet-body">	<form action="tudi_add.php" method="post">		<div class="control-group">			<label class="control-label">地块编号</label>			<div class="controls">				<input type="text" name="tudi_id" class="add-in" value="" />例子:DAEJ2014054			</div>		</div> 		        <div class="control-group">			<label class="control-label">区域</label>			<div class="controls">				<select name="city" class="select-city">                    <?php 					$q = "SELECT city_id, city_name FROM f_city ORDER BY city_id ASC";							$r = mysqli_query ($conn, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($conn));					if (@mysqli_num_rows($r) > 0) {						while ($row = mysqli_fetch_array ($r, MYSQLI_NUM)) {							echo "<option value=\"$row[0]\"";							if (isset($trimmed['city']) && ($trimmed['city'] == $row[0]) ) echo ' selected="selected"';							echo ">$row[1]</option>\n";						}						mysqli_free_result ($r);					} else {						echo '<option>暂无区域,请添加一个新地区</option>';					}					mysqli_close($conn);					?>				</select>			</div>		</div>		<div class="control-group">			<label class="control-label">地块位置</label>			<div class="controls">				<input type="text" name="weizhi" class="add-in w600" value="" />			</div>		</div> 		        <div class="control-group">			<label class="control-label">土地用途</label>			<div class="controls">				<input type="text" name="yongtu" class="add-in" value="" />例子:商业、商务、娱乐康体、居住用地			</div>		</div>         <div class="control-group">			<label class="control-label">容积率(FAR)</label>			<div class="controls">				<input type="text" name="rongjilv" class="add-in" value="" />例子:1.0<FAR≤2.273			</div>		</div>         <div class="control-group">			<label class="control-label">建筑密度(D)</label>			<div class="controls">				<input type="text" name="midu" class="add-in" value="" />例子:D≤30.46%			</div>		</div>        <div class="control-group">			<label class="control-label">绿地率(GAR)</label>			<div class="controls">				<input type="text" name="lvdi" class="add-in" value="" />例子:GAR≥30%			</div>		</div>		<div class="control-group">			<label class="control-label">发布日期</label>			<div class="controls">				<input type="text" name="fabu_time" class="add-in" onClick="WdatePicker()" value="" />时间格式例如:2014-12-12			</div>		</div> 		<div class="control-group">			<label class="control-label">成交日期</label>			<div class="controls">				<input type="text" name="chengjiao_time" class="add-in" onClick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" value="" />时间格式例如:2014-12-12 10:00:00			</div>		</div> 		<div class="control-group">			<label class="control-label">保证金截止时间</label>			<div class="controls">				<input type="text" name="jiezhi_time" class="add-in" onClick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" value="" />时间格式例如:2014-12-12 15:00:00			</div>		</div>         <div class="control-group">			<label class="control-label">竞买保证金(万元)</label>			<div class="controls">				<input type="text" name="baozhengjin" class="add-in" value="" />直接填写数字,不要带上单位			</div>		</div>        <div class="control-group">			<label class="control-label">占地面积(亩)</label>			<div class="controls">				<input type="text" name="mianji_m" class="add-in" value="" />直接填写数字,不要带上单位			</div>		</div>	        <div class="control-group">			<label class="control-label">占地面积(?)</label>			<div class="controls">				<input type="text" name="mianji_p" class="add-in" value="" />直接填写数字,不要带上单位			</div>		</div>	        <div class="control-group">			<label class="control-label">规划建筑面积(?)</label>			<div class="controls">				<input type="text" name="guihua_p" class="add-in" value="" />直接填写数字,不要带上单位			</div>		</div>	        <div class="control-group">			<label class="control-label">起拍价(万/亩)</label>			<div class="controls">				<input type="text" name="qipaijia" class="add-in" value="" />直接填写数字,不要带上单位			</div>		</div>	        <div class="control-group">			<label class="control-label">成交单价(万/亩)</label>			<div class="controls">				<input type="text" name="chengjiaodanjia" class="add-in" value="" />直接填写数字,不要带上单位			</div>		</div>	        <div class="control-group">			<label class="control-label">成交总地价(万元)</label>			<div class="controls">				<input type="text" name="chengjiazongjia" class="add-in" value="" />直接填写数字,不要带上单位			</div>		</div>	        <div class="control-group">			<label class="control-label">楼面地价</label>			<div class="controls">				<input type="text" name="loumiandijia" class="add-in" value="" />直接填写数字,不要带上单位			</div>		</div>	        <div class="control-group">			<label class="control-label">溢价率</label>			<div class="controls">				<input type="text" name="yijialv" class="add-in" value="" />百分比数字			</div>		</div>	        <div class="control-group">			<label class="control-label">竞得人</label>			<div class="controls">				<input type="text" name="jingderen" class="add-in w600" value="" />			</div>		</div>	        <div class="control-group">			<label class="control-label">起始价(万元)</label>			<div class="controls">				<input type="text" name="churang_p" class="add-in" value="" />直接填写数字,不要带上单位			</div>		</div>        <div class="control-group">			<label class="control-label">地图位置</label>			<div class="controls">				<input type="text" name="dituweizhi" id="dituweizhi" readonly="readonly" class="add-in w600" /> <a href="javascript:;" id="up">上传</a>			</div>		</div>	        <div class="control-group">			<label class="control-label">出让须知</label>			<div class="controls">				<textarea name="xuzhi" cols="150" rows="10" class="add-in"></textarea> 			</div>		</div>			        <div class="control-group">			<label class="control-label">交易是否成功</label>			<div class="controls">				<select name="is_cheng" class="select-city">						<option value="0">暂未交易</option>						<option value="1">成功交易</option>							<option value="2">流拍</option>												</select>			</div>		</div>		        <div class="form-actions">            <button class="btn" name="submit" type="submit">提交</button>	   	</div>   	</form>    </div></div>
Salin selepas log masuk

字段设置为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<br />MySQL Error: " . mysqli_error($conn));		if (mysqli_affected_rows($conn) == 1) {			echo '这条数据已经添加成功';			$_POST = array();			} else {			echo '<p style="font-weight: bold; color: #C00">程序发生错误,请重新添加</p>'; 		}			}
Salin selepas log masuk


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

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

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


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

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

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan