Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php从json字符串/数组在mysql中插入多行?_Php_Mysql_Json - Fatal编程技术网

使用php从json字符串/数组在mysql中插入多行?

使用php从json字符串/数组在mysql中插入多行?,php,mysql,json,Php,Mysql,Json,“我正在尝试开发一个脚本,它允许我在mysql数据库中插入jsonarray中的多行,但在测试时,只插入了一行,下面是我的代码: <?php $con = mysqli_connect($host,$user,$password,$database) or die('Unable to Connect'); if($_SERVER["REQUEST_METHOD"]=="POST") { $jsonData=file_get_contents

“我正在尝试开发一个脚本,它允许我在mysql数据库中插入jsonarray中的多行,但在测试时,只插入了一行,下面是我的代码:

<?php 
    $con = mysqli_connect($host,$user,$password,$database) or die('Unable to Connect');
     if($_SERVER["REQUEST_METHOD"]=="POST")
     {
         $jsonData=file_get_contents("sampledata.json");
         $jsonString=json_decode($jsonData,true);
         foreach($jsonString['Order Summary'] as $cart)
         {
             $name=$cart['ProductName'];
             $price=$cart['ProductPrice'];
             $quantity=$cart['ProductQuantity'];
             $cost=$cart['ProductCost'];
             $seller=$cart['SellerId'];
             $stmt=$con->prepare("INSERT INTO sProducts(Name,Price,Quantity,Cost,Sellerid)VALUES(?,?,?,?,?)");
             $stmt->bind_param("sssss",$name,$price,$quantity,$cost,$seller);
             if($stmt->execute())
              return json_encode("data inserted");
            return json_encode("error");     
         } 
     }

首先,您正在循环的第一次迭代中重新尝试,这意味着脚本将停止。
return
应仅用于从函数返回

如果同时删除
return
s,则循环将继续,直到完成

您不应该在循环中准备查询,这是不必要的,而且在开始循环之前准备查询效率更高(当您在循环中绑定和执行时,可以多次使用不同的值)。我还在代码中添加了一些空格,以使其更易于阅读

$con = mysqli_connect($host,$user,$password,$database) or die('Unable to Connect');
$msg = "data inserted";

if ($_SERVER["REQUEST_METHOD"]=="POST") {
    $jsonData = file_get_contents("sampledata.json");
    $jsonString = json_decode($jsonData,true);
    $stmt = $con->prepare("INSERT INTO sProducts (Name, Price, Quantity, Cost, Sellerid) VALUES (?, ?, ?, ?, ?)");

    foreach($jsonString['Order Summary'] as $cart) {
        $name = $cart['ProductName'];
        $price = $cart['ProductPrice'];
        $quantity = $cart['ProductQuantity'];
        $cost = $cart['ProductCost'];
        $seller = $cart['SellerId'];
        $stmt->bind_param("sssss", $name, $price, $quantity, $cost, $seller);
        if (!$stmt->execute())
            $msg = "Something went wrong";
    } 
}
return json_encode($msg);

为什么不一步一步地调试它?
return…
?您的代码块在任何函数中吗?当您返回时,脚本停止。因此,它只插入第一行。然后,您不需要为每次迭代准备查询-在开始循环之前准备一次。