简体   繁体   English

使用PHP将多个值插入MySQL数据库

[英]Inserting Multiple values into MySQL database using PHP

I'm wondering how to insert multiple values into a database. 我想知道如何将多个值插入数据库。

Below is my idea, however nothing is being added to the database. 下面是我的想法,但是什么都没有添加到数据库中。

I return the variables above (email, serial, title) successfully. 我成功返回了上面的变量(电子邮件,序列号,标题)。 And i also connect to the database successfully. 而且我还成功连接到数据库。

The values just don't add to the database. 这些值只是不添加到数据库中。

I get the values from an iOS device and send _POST them. 我从iOS设备获取值并发送_POST它们。

$email = $_POST['email'];
$serial = $_POST['serial'];
$title = $_POST['title'];

After i get the values by using the above code. 在我通过使用上面的代码获得值之后。 I use echo to ensure they have values. 我使用echo来确保它们具有值。

Now I try to add them to the database: 现在,我尝试将它们添加到数据库中:

  //Query Check
        $assessorEmail = mysqli_query($connection, "SELECT ace_id,email_address FROM assessorID WHERE email_address = '$email'");
        if (mysqli_num_rows($assessorEmail) == 0) {

            echo " Its go time add it to the databse.";

            //It is unqiue so add it to the database
            mysqli_query($connection,"INSERT INTO assessorID (email_address, serial_code, title)
            VALUES ('$email','$serial','$title')");

        } else {

            die(UnregisteredAssessor . ". Already Exists");

        }

Any ideas ? 有任何想法吗 ?

Since you're using mysqli, I'd instead do a prepared statement 由于您使用的是mysqli,因此我将准备一份声明

if($stmt = mysqli_prepare($connection, "INSERT INTO assessorID (email_adress, serial_code, title) VALUES (?, ?, ?)"))
{
    mysqli_stmt_bind_param($stmt, "sss", $email, $serial, $title);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
}

This is of course using procedural style as you did above. 当然,这是像上面一样使用过程样式。 This will ensure it's a safe entry you're making as well. 这样也可以确保您输入的内容安全。

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

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