dp9599 2015-02-08 05:29
浏览 52
已采纳

如何在PHP中使用MySQL NOT NULL?

I've form to insert values in MySQL database with PHP (Example code is below):

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<style>
label{text-align:left;
       width:150px;
       display:block;
           float:left;
           clear:right;
       font-size:18;}

form {
    width: 30%;
    margin: 0 auto;
}
</style>
<label>Name:</label><input name=name autocomplete=off><br>
<label>Date of Birth:</label><input size=2 name=dd placeholder=dd> / <input size=2 name=mm placeholder=mm> / <input size=6 name=yyyy placeholder=yyyy><br>
<label>Time of Birth:</label><input name=tob placeholder="hh : mm : ss"><br>
<label>City:</label><input name=city><br><br>
<center><input type=submit value=Save></center>
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$servername = "localhost";
$username = "userame";
$password = "password";
$dbname = "myDB2";
if(empty($_POST["name"])){ echo "Name is Required<br>";}
else {
$name = $_POST["name"];
}
var_dump($name);
$tob = $_POST["tob"];
$city = $_POST["city"];
$dd = $_POST["dd"];
$mm = $_POST["mm"];
$yyyy = $_POST["yyyy"];

$dob = "$yyyy-$mm-$dd";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//Inserting Data
$sql = "INSERT INTO Person
VALUES('$name','$dob','$tob','$city')";
if ($conn->query($sql) === TRUE) {
    echo "<br>New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?>

AND The COLUMN Name is set to varchar(60) NOT NULL UNIQUE data-type & constraint.


In-spite of

  • NOT NULL constraint for Name Column, and

  • if(empty($_POST["name"])){ echo "Name is Required";} else { $name = $_POST["name"]; }

When I inserts submit form without entering any value in Name Input in the form, I get following message (according to code)

Name is Required
NULL
New record created successfully

And My Database output like:

screen0

Here you can see that var_dump($name) says $name is NULL BUT Still MySQL Database accepting this value for Name Column!


So, My question is What is the use of NOT NULL or How can I use MysQL NOT NULL with PHP? for inserting data with the help for form?

  • 写回答

4条回答 默认 最新

  • dongze5043 2015-02-08 05:37
    关注

    You need to translate PHP's null into SQL's NULL type appropriately. When you do simple string interpolation in PHP:

    "INSERT INTO foo VALUES ('$bar')"
    

    where $bar is null, the result ends up as:

    INSERT INTO foo VALUES ('')
    

    MySQL sees that as an empty string, which is not NULL, so it's fine.

    You have to do something like this:

    $value = null;
    if ($value === null) {
        $bar = 'NULL';
    } else {
        $bar = "'$value'";
    }
    $query = "INSERT INTO foo VALUES ($bar)";
    

    Here the result is one of these two:

    INSERT INTO foo VALUES (NULL)
    INSERT INTO foo VALUES ('baz')
    

    Then MySQL will see the null as NULL.

    However, if you're going to such lengths in PHP, you can obviously simply not fire the query if your values are empty, instead of firing the query and letting MySQL fail due to constraints.

    You further need to learn about SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值