search
HomeBackend DevelopmentPHP Tutorialmysql数据库不能添加NULL值

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

Column 'loumiandijia' cannot be null

其他数据在为空是用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');?>


代码太多 分开发


回复讨论(解决方案)

<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>

字段设置为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>'; 		}			}


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

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

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


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

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

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

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!