简体   繁体   English

使用准备好的语句 PHP MYSQL 将空值插入数据库

[英]Inserting null values to database using prepared statements PHP MYSQL

Hi I am using prepared statements for the first time.嗨,我第一次使用准备好的语句。 I have a form whose values, i am inserting in Mysql database using Mysqli prepared statements.我有一个表单,它的值使用 Mysqli 准备好的语句插入到 Mysql 数据库中。 But the problem is if user leaves an input box empty, Query doesn't insert row to the database.但问题是如果用户将输入框留空,Query 不会向数据库插入行。

Form形式

<form action="test.php" method="post" class="signupform">
    <input type="text" Placeholder="Name" name="name" Required="required"/>
    <br />
    <input type="email" Placeholder="Email-id" name="email" Required="required"/>
    <br />
    <input type="password" Placeholder="Password" name="pass" Required="required"/>
    <br />
    <span>Male<input type="radio" name="sex" value="M" checked="checked"/>&nbsp;&nbsp;&nbsp;Female<input type="radio" name="sex" value="F"/></span>
    <br />
    <input type="text" Placeholder="City" name="city"/>
    <br /><br />
    <input type="submit" value="CREATE MY ACCOUNT" name="submit"/>
</form>

<?php

if(isset($_POST['submit'])){
    include_once('includes/db.php');
    $name=$_POST['name'];
    $pass=$_POST['pass'];
    $email=$_POST['email'];
    $sex=$_POST['sex'];
    $city = $_POST['city'];
    if ($stmt = $mysqli->prepare("INSERT INTO login VALUES('',?,?,?,?,?,'')")) {
        $stmt->bind_param("sssss", $name, $email, $pass, $sex, $city);
        $stmt->execute();
        if($stmt){
            echo "result inserted";
        }
    }
}

?>

On using above form and query when i fill all the boxes of form it insert a new row for me.在使用上面的表单和查询时,当我填写所有表单框时,它会为我插入一个新行。 But if i leave an input box empty, It doesn't insert any row.但是如果我将输入框留空,它不会插入任何行。

I also have seen a lot of questions which says that if i use variables like this我也看到很多问题说如果我使用这样的变量

if(empty($_POST['city'])) { $city = null; } else { $city = $_POST['city']; }

then it will work and most of them are accepted answers.那么它就会起作用,并且其中大部分都是可以接受的答案。 I am confused why this solution is not working for me ???我很困惑为什么这个解决方案对我不起作用???

Any help is appreciated...Thanks任何帮助表示赞赏...谢谢

Your query is wrong:您的查询有误:

if ($stmt = $mysqli->prepare("INSERT INTO login VALUES('',?,?,?,?,?,'')")) {

It should be something like:它应该是这样的:

if (!empty($name) || !empty($pass) || !empty($email))
    {
        $stmt = $mysqli->prepare("INSERT INTO login(`name`,`password`,`email`,`sex`,`city`) VALUES(?,?,?,?,?)");
        $stmt->execute([$name, $pass, $email, $sex, $city]);
        echo "result inserted";

    } else {
        echo 'You have not entered all of the fields.';
    }

In this instance, if the variables are not empty then perform insert.在这种情况下,如果变量不为空,则执行插入。 Else if they are empty fire a echo stating the fields haven't been filled in.否则,如果它们是空的,则会发出一个回声,说明这些字段尚未填充。

If you are happy for the fields to be null simply change !empty() to empty() but as Fred -ii- stated above, ensure your database allows NULL within them fields.如果您对这些字段为空感到高兴,只需将!empty()更改为empty()但如上述 Fred -ii- 所述,请确保您的数据库允许在这些字段中使用 NULL。

Probably this is not one of the smartest way to do it, but hey, it will get the job done.可能这不是最聪明的方法之一,但是嘿,它会完成工作。

One of the things that you need to do before assigning a variable to an $_POST field, you need to check if that $_POST field isset and its not empty, then assign the value if not empty, Currently if someone leaves out a field in your form when you run the query you will probably get a notice of undefined.在将变量分配给 $_POST 字段之前需要做的一件事,您需要检查该 $_POST 字段是否已设置并且不为空,如果不为空则分配值,目前如果有人遗漏了一个字段当您运行查询时,您的表单可能会收到未定义的通知。

This is what you can do.这是你可以做的。

<?php

if (isset($_POST['submit'])) {
    include_once('includes/db.php');


    if (!empty($_POST['name'])) {

        $name = $_POST['name'];
    } else {

        $name = " ";
    }

    if (!empty($_POST['pass'])) {

        $pass = $_POST['pass'];
    } else {

        $pass = " ";
    }

    if (!empty($_POST['email'])) {

        $email = $_POST['email'];
    } else {

        $email = " ";
    }

    if (isset($_POST['sex'])) {

        $sex = $_POST['sex'];
    } else {

        $sex = " ";
    }

    if (!empty($_POST['city'])) {
        $city = $_POST['city'];
    } else {

        $city = " ";
    }
    if ($stmt = $mysqli->prepare("INSERT INTO login VALUES(?,?,?,?,?)")) {
        $stmt->bind_param("sssss", $name, $email, $pass, $sex, $city);
        $stmt->execute();
        if ($stmt) {
            echo "result inserted";
        } else {

            echo "could not insert";
        }
    }
}

?>

There are other better ways to do this.还有其他更好的方法可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM