-2

I got uncaught error in that line because I don't know what to put into the while() statement. I just want to make sure the syntax can display the data.

<?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);
    }

Can somebody help me to solve that statement in that line?

Fatal error: Uncaught Error: Call to a member function fetch_row() on string in C:\xampp\htdocs\inventori\php_action\createOrder.php:27 Stack trace: #0 {main} thrown in C:\xampp\htdocs\inventori\php_action\createOrder.php on line 27

  • As the error message says, `$sql` is a string (your query). When you make the query, you need to store the result in a variable: `if ($result = $connect->query($sql) === true)` and use _that_ variable: `$result->fetch_row()`. Although, this would solve the current issue, you should read the docs and set up some error handling etc. (and use prepared statements) – M. Eriksson Sep 25 '18 at 05:18

1 Answers1

0

You need to store your result set post query call. And then utilize fetch_row on the result set, instead of your $sql string.

It should be like this:

$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()) {

Note: Your code is very much open to SQL Injection ! Use Prepared Statements.

Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57