PHP在json对象发布的mysql中插入数据

时间:2021-12-21 16:53:01

I want to send some data from a jQueryMobil.listwidget via PHP to a mysql database.

我想通过PHP将一些数据从jQueryMobil.listwidget发送到mysql数据库。

I get and post my listitems like this:

我得到并发布我的listitems,如下所示:

function getItems()
{ 
        var listview_array = new Array();
        $( "#itemList li" ).each(function(index) {
            listview_el = new Object();
            listview_el.id = index;
            listview_el.name=$(this).text();
            listview_el.owner="owner";
            listview_array.push(listview_el);
        });
        var stringifyObject = JSON.stringify(listview_array);
        //alert(stringifyObject);
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "insert.php",
            data: { mydata: stringifyObject },
        });
        //showItems();

}

i want to add my json-object to a mysql database/table which exists. On my request my data is sent but the if(prepStmnt) never succeeds.

我想将我的json-object添加到存在的mysql数据库/表中。根据我的要求,我的数据已发送,但if(prepStmnt)从未成功。

 <?php

    $con = new mysqli($servername, $username, $password, $dbname);
    if (!$con) {
      die('Could not connect: ' . mysqli_error($con));
    }

    echo "preps";
    if($preparedStatement = $con->prepare('INSERT INTO Einkaufsliste (item, owner) VALUES (:name, :owner)')){
        $preparedStatement->execute(json_decode($_POST["mydata"], true));
        $preparedStatement->close();
        echo "done";
    };

    $con->close();
    ?> 

Can you please tell my why no data is stored in my db?

你能告诉我为什么数据库中没有数据存储吗?

1 个解决方案

#1


1  

MySQLi does NOT support named parameters.

MySQLi不支持命名参数。

if($preparedStatement = $con->[...snip...] (:name, :owner)')){
                                            ^^^^^^^^^^^^^

That's outright illegal in MySQLi, so your prepare fails, and everything else just falls of the end of the script, because you have no error checking.

这在MySQLi中是完全非法的,因此您的准备失败,而其他所有内容都落在脚本的末尾,因为您没有错误检查。

$prepare = $con->prepare(...);
if (!$prepare) {
   die(mysqli_error($con));
}

A proper mysqli prepared statement would be

一个适当的mysqli准备声明将是

$stmt = $con->prepare('INSERT ... VALUES (?, ?)');

Note the ? placeholders.

注意?占位符。

Never EVER assume your query will suceed. Even if your sql actually is correct, there's a near infinite number of ways for the query to fail. Always assume failure, check for that failure, and treat success as a pleasant surprise.

永远不要假设您的查询将成功。即使你的sql实际上是正确的,但是查询失败的方法几乎无穷无尽。总是假设失败,检查失败,并将成功视为一个惊喜。

#1


1  

MySQLi does NOT support named parameters.

MySQLi不支持命名参数。

if($preparedStatement = $con->[...snip...] (:name, :owner)')){
                                            ^^^^^^^^^^^^^

That's outright illegal in MySQLi, so your prepare fails, and everything else just falls of the end of the script, because you have no error checking.

这在MySQLi中是完全非法的,因此您的准备失败,而其他所有内容都落在脚本的末尾,因为您没有错误检查。

$prepare = $con->prepare(...);
if (!$prepare) {
   die(mysqli_error($con));
}

A proper mysqli prepared statement would be

一个适当的mysqli准备声明将是

$stmt = $con->prepare('INSERT ... VALUES (?, ?)');

Note the ? placeholders.

注意?占位符。

Never EVER assume your query will suceed. Even if your sql actually is correct, there's a near infinite number of ways for the query to fail. Always assume failure, check for that failure, and treat success as a pleasant surprise.

永远不要假设您的查询将成功。即使你的sql实际上是正确的,但是查询失败的方法几乎无穷无尽。总是假设失败,检查失败,并将成功视为一个惊喜。