PHP只插入第一行

时间:2016-06-29 17:03:29

标签: php

此PHP代码仅插入第一行然后返回错误:在非对象上调用成员函数fetch_assoc()。 请帮助。

$sqlquery = "SELECT * FROM shopcart WHERE UserID='$Useremail'";
    if(!$result = $db->query($sqlquery)) die ('Error getting Food Information ['.$db->connect_error.']');
    $result = $db->query($sqlquery);

    while($row = $result->fetch_assoc()){
        //echo $row['name'];
        $name= $row['name'];
        $productid = $row['ProductID'];
        $initialPrice = $row['InitialPrice'];
        $qty = $row['Quantity'];



        //insert into to ordereditems
        $sql = "INSERT INTO ordereditems (OrderID, ProductName, ProductID, Quantity, SalesPrice) 
        VALUES ('$orderid', '$name', '$productid','$qty','$initialPrice')";
        if(!$result = $db->query($sql)) die('There was an error Processing Order Again [' . $db->error . ']');

    }

4 个答案:

答案 0 :(得分:2)

您正在废弃it

$result

请注意,您很容易受sql injection attacks攻击,并且所有这些代码都可以简单地替换为单个$sqlquery = "SELECT * FROM shopcart WHERE UserID='$Useremail'"; if(!$result = $db->query($sqlquery)) die ('Error getting Food Information ['.$db->connect_error.']'); $result = $db->query($sqlquery); ^^^^^^--- your first query if(!$result = $db->query($sql)) die('There was an error Processing Order Again [' . $db->error . ']'); ^^^^^^-kill the first query result, replace with new result 查询。

答案 1 :(得分:0)

您的插入操作会覆盖$result变量。将其更改为其他名称,如下所示:

if(!$result2 = $db->query($sql)) die('There was an error Processing Order Again [' . $db->error . ']');

答案 2 :(得分:0)

在循环的最后一行中,您将覆盖$ result。

<body>
<figure class="crossfade">
    <figure></figure>
    <figure></figure>
    <figure></figure>
    <figure></figure>
    <figure></figure>
  </figure>
</body>

应该是:

if(!$result = $db->query($sql)) die('There was an error Processing Order Again [' . $db->error . ']');

答案 3 :(得分:0)

您正在使用INSERT查询覆盖SELECT查询的初始结果。这是固定的代码:

$sqlquery = "SELECT * FROM shopcart WHERE UserID='$Useremail'";
if(!$result = $db->query($sqlquery)) die ('Error getting Food Information ['.$db->connect_error.']');
//$result = $db->query($sqlquery);   // NOT NEEDED AS ALREADY EXECUTED AND ASSIGNED in the IF statement above

while($row = $result->fetch_assoc()){
    //echo $row['name'];
    $name= $row['name'];
    $productid = $row['ProductID'];
    $initialPrice = $row['InitialPrice'];
    $qty = $row['Quantity'];

    /*
    instead of assigning these variables manually you could also use:

    extract($row);

    this would convert name, ProductID, InitialPrice and Quantity to local variables $name, $ProductID, $InitialPrice and $Quantity. However make sure the db result does not contain key "result" as it would overwrite your $result object again :)
     */


    //insert into to ordereditems
    $sql = "INSERT INTO ordereditems (OrderID, ProductName, ProductID, Quantity, SalesPrice) 
    VALUES ('$orderid', '$name', '$productid','$qty','$initialPrice')";

    //if(!$result = $db->query($sql)) die('There was an error Processing Order Again [' . $db->error . ']');  // this is what overwrites $result and the next while check throws an error you just got
    if(!$db->query($sql)) die('There was an error Processing Order Again [' . $db->error . ']'); //here's the fixed version, $db->query for insert does not return an object but boolean (true/false) so no need to assign it to a $result variable
}