Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial mysql数据库不能添加NULL值

mysql数据库不能添加NULL值

Jun 23, 2016 pm 01:48 PM
mysql null database Add to

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

Column 'loumiandijia' cannot be null
Copy after login

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


代码太多 分开发


回复讨论(解决方案)

<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>
Copy after login

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


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

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

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


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

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

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

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

See all articles