【发布时间】:2016-07-04 23:25:44
【问题描述】:
我试图添加列 id,这将是具有自动增量属性的主键。所以我执行这个 SQL 查询:
ALTER TABLE `user_info` ADD `id` INT NULL AUTO_INCREMENT PRIMARY KEY FIRST ;
列创建成功,甚至为已有数据创建id。
但我无法再在表格中插入任何内容了。
<?php
require "init.php";
$name=$_POST["user"];
$user_email=$_POST["user_email"];
$user_pass=$_POST["user_pass"];
$sql_query="SELECT * FROM user_info WHERE user_email='$user_email'";
$check=mysqli_fetch_array(mysqli_query($con,$sql_query));
if(isset($check)){
$response["success"]=false;
$response["message"]="Email already exists";
echo json_encode($response);
}else{
$sql_query="insert into user_info values('$name','$user_email','$user_pass');";
if(mysqli_query($con,$sql_query)){
//echo "<h3> Data Insert success</h3>";
$response["success"]=true;
$response["message"]="Registration successful";
echo json_encode($response);
}
else{
$response["success"]=false;
$response["message"]="Registration unsuccessful";
echo json_encode($response);
}
}
?>
我搜索了一下,发现我可以插入 0 或 null 来代替我的 id。
我故意读到:
为了利用列的自动递增功能,插入行时不要为该列提供值。数据库将为您提供一个值。
当我删除列 id 时代码运行良好,但当我添加它时,我在我的 json 中得到Registration unsuccessful 的响应。
我错过了什么?
【问题讨论】:
-
我不确定我是否正确读取了 sql,但您不应允许 ID 列使用 NULL 值。