“类mysqli_result的对象无法转换为字符串”在PHP上 - MYSQLi

时间:2014-05-07 03:43:33

标签: php mysqli

我有错误

  

捕获致命错误:类mysqli_result的对象无法转换为

中的字符串

我的代码

<?php 
session_start();
require('config-home.php');
$user = $_SESSION["username"];
$result = $mysqli->query("SELECT * FROM customers WHERE username ='$user'");
$id = $result->fetch_object();
// get id user logined
$id_user = $id->id_customer;
if(isset($_POST['buy_submit']) && isset($_SESSION["products"]) ){


    $requiredate = $_POST['requiredate']; // get date order
    $comments = $_POST["comments"];
    $query = $mysqli->query("INSERT INTO `order`(`id_customer`, `orderDate`, `requiredDate`, `comments`) VALUES ('$id_user', CURRENT_TIMESTAMP , '$requiredate', '$comments')"); //ghi vao DTB phieu mua hang
    $id_order = $mysqli->query("SELECT id_order FROM `order` WHERE id_customer ='$id_user' AND shippedDate='$requiredate'");
    foreach ($_SESSION["products"] as $item){
        $id_item = $item["id"];
        $quantity = $item["quantity"];
        $price = $item["price"];
        $mysqli->query("INSERT INTO `orderdetail`(`id_order`,`cd_code`,`quantityOrdered`,`priceEach`) VALUES ('$id_order','$id_item','$quantity','$price')");
    }
unset($_SESSION["products"]);
}
?>

此代码在文件payment.php中,我需要将客户的记录数据顺序记入表order并将记录数据产品(客户已购买)记录到orderdetail

但我有错误

$mysqli->query("INSERT INTO `orderdetail`(`id_order`,`cd_code`,`quantityOrdered`,`priceEach`) VALUES ('$id_order','$id_item','$quantity','$price')");

我检查$ _SESSION [&#34;产品&#34;](客户已购买的产品)并将所有数据记录到表ỏderdetail

1 个答案:

答案 0 :(得分:0)

到这个

$res2= $mysqli->query("SELECT id_order FROM `order` WHERE id_customer ='$id_user' AND shippedDate='$requiredate'");
$row= $res2->fetch_object();

$id_order = $row->id_order;