致命错误:未捕获错误:在...中的字符串上调用成员函数fetch_row()...(while($ sql-> fetch_row()){)

时间:2018-09-25 05:16:31

标签: php mysql

该行出现了未捕获的错误,因为我不知道在while()语句中放入什么。我只想确保语法可以显示数据。

<?php   

require_once 'core.php';

    $valid['success'] = array('success' => false, 'messages' => array(), 'order_id' => '');

    if($_POST) {    
        $orderDate     = date('Y-m-d', strtotime($_POST['orderDate'])); 
        $clientName    = $_POST['clientName'];


        $sql = "INSERT INTO orders (order_date, client_name, order_status) VALUES ('$orderDate', '$clientName', 1)";

        $order_id;
        $orderStatus = false;
        if($connect->query($sql) === true) {
            $order_id = $connect->insert_id;
            $valid['order_id'] = $order_id; 
            $orderStatus = true;
        }

            // echo $_POST['productName'];
        $orderItemStatus = false;

        for($x = 0; $x < count($_POST['namaBahan']); $x++) {            

            while ($sql->fetch_row()) {

                    // add into order_item
                    $orderItemSql = "INSERT INTO order_item (order_id, id_bahan, kuantiti, jenis_kuantiti, harga_per_unit, jumlah, order_item_status) 
                    VALUES ('$order_id', '".$_POST['namaBahan'][$x]."', '".$_POST['kuantiti'][$x]."', '".$_POST['jenisKuantiti'][$x]."','".$_POST['harga'][$x]."', '".$_POST['jumlahValue'][$x]."', 1)";

                    $connect->query($orderItemSql);     

                    if($x == count($_POST['namaBahan'])) {
                        $orderItemStatus = true;
                    }       
            } // while  
        } // /for quantity

        $connect->query($orderItemSql);

        $valid['success'] = true;
        $valid['messages'] = "Successfully Added";      

        $connect->close();

        echo json_encode($valid);
    }

有人可以帮我解决那一行的问题吗?

  

致命错误:未捕获错误:调用成员函数fetch_row()   C:\ xampp \ htdocs \ inventori \ php_action \ createOrder.php:27中的字符串   堆栈跟踪:抛出#0 {main}   C:\ xampp \ htdocs \ inventori \ php_action \ createOrder.php在第27行

1 个答案:

答案 0 :(得分:0)

您需要存储query调用后的结果集。然后在结果集上使用fetch_row,而不是您的$sql字符串。

应该是这样的:

$result = $connect->query($sql);
if($result) {
            $order_id = $connect->insert_id;
            $valid['order_id'] = $order_id; 
            $orderStatus = true;
        }

        // echo $_POST['productName'];
    $orderItemStatus = false;

    for($x = 0; $x < count($_POST['namaBahan']); $x++) {            

        while ($result->fetch_row()) {

注意::您的代码非常适合SQL Injection! Use Prepared Statements