我如何解决这个错误“捕获致命错误:类mysqli_result的对象无法转换为字符串”?

时间:2017-09-11 05:05:13

标签: php

$sql = "INSERT INTO placed_req(username,goodsauto,minitruck,largetruck,price,qty) VALUES('$user_check','$ga','$mt','$lt','$r','$qty')";
$result = mysqli_query($con,$sql);
$sql2="SELECT reqid FROM placed_req WHERE username='$user_check' AND price='$r'";
$ret=mysqli_query($con,$sql2);
$sql1 = "INSERT INTO inv_detail (inv_id,p_name,qty,price) VALUES('$ret','$user_check','$qty','$r')"; //i'm getting that error in this line
$result1 = mysqli_query($con,$sql1);
if(isset($result1))
    echo "<br></br> Invoice generated successfully";
    header("refresh:10,url=placeorders.php");
} else {
    echo "<br></br> values not selected";
}

2 个答案:

答案 0 :(得分:1)

如果你想将req_id放入插入查询中,你必须正确地获取req_id

$sql2="SELECT reqid FROM placed_req WHERE username='$user_check' AND price='$r'";

$ret=mysqli_query($con,$sql2);

$row = mysqli_fetch_assoc($ret);
/* above instruction fetch record from database*/

$inv_id = $row['reqid'];

然后将$inv_id放入插入查询语句

$sql1 = "INSERT INTO inv_detail (inv_id,p_name,qty,price) VALUES('$inv_id','$user_check','$qty','$r')"; //i'm getting that error in this line
$result1 = mysqli_query($con,$sql1);

答案 1 :(得分:0)

如果您从

修复查询
"SELECT reqid FROM placed_req WHERE username=\'" . $user_check . "\' AND price=\'" . $r . "\'";

要:

$sql1 = "INSERT INTO inv_detail (inv_id,p_name,qty,price) VALUES('$ret','$user_check','$qty','$r')";

并且

$sql1 = "INSERT INTO inv_detail (inv_id,p_name,qty,price) VALUES(\'" . $ret . "\',\'" . $user_check . "\',\'". $qty . "\',\'" . $r . "\')";

ret

您最终可以在查询中使用变量的值。和@pritamakumar说的一样,你必须正确地获取{{1}}