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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)