Amirul Danish
Amirul Danish

Reputation: 13

How to solve PHP error 'Notice: Array to string conversion in…'

I've been reading in every thread in here that is related to this but I always get it wrong.

Please help cause I always get the error

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

$orderItemStatus = false;

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

$connect->query($orderItemSql);

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

$connect->close();

    echo json_encode($valid);
}

But when the code runs I get an error like:

Notice: Array to string conversion in C:\xampp\htdocs\inventori\php_action\createOrder.php on line 25

Notice: Array to string conversion in C:\xampp\htdocs\inventori\php_action\createOrder.php on line 25

Notice: Array to string conversion in C:\xampp\htdocs\inventori\php_action\createOrder.php on line 25

Notice: Array to string conversion in C:\xampp\htdocs\inventori\php_action\createOrder.php on line 25

Notice: Array to string conversion in C:\xampp\htdocs\inventori\php_action\createOrder.php on line 25 {"success":true,"order_id":1,"messages":"Successfully Added"}

Upvotes: 0

Views: 896

Answers (1)

matt
matt

Reputation: 700

Perhaps you could try to echo out your $_POST data before the $orderItemSql = ... line to see what it contains:

echo '<pre>'.print_r($_POST, true).'</pre>';

This should at least tell you if any of the $_POST data you are trying to use in your SQL insert is not a string when it should be.

Upvotes: 2

Related Questions