PHP - 存储 SELECT 查询并在 INSERT 查询中使用它

标签 php mysql sql

您好,我在使用插入查询中的选择查询中的数据时遇到问题。

这是我的 php 代码 -

   <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbName = "arestaurant";


    $ID = $_GET["Id"];
    $QUANTITY = $_GET["Quantity"];
    $TABLE = $_GET["Table"];

    //Make connection
    $conn = new mysqli($servername,$username,$password,$dbName);
    // check connection
    if(!$conn) {
        die("Connection Failed. ".mysqli_connect_error());
    }

    $sql = "INSERT INTO tableorders (tableNumber,isPaid)
            VALUES ('".$TABLE."','0')";
    $result = mysqli_query($conn,$sql);



    $y = "SELECT orderId from tableorders WHERE tableNumber='$TABLE' ORDER by orderDate DESC limit 1 offset 0 ";
    $resulty = mysqli_query($conn,$y);

    if ($resulty !== false) {
    $value = mysqli_fetch_field($resulty);
    $orderid = $value['orderId']; < --  error


}

    $sqlquery = "INSERT INTO orderitems (orderId, productId, quantity)
            VALUES ('".$orderid."','".$ID."','".$QUANTITY."')";
    $result2 = mysqli_query($conn,$sqlquery);


?>

但我得到 -

Fatal error: Cannot use object of type stdClass as array on line 30.

我有几种方法来存储它然后再次使用它,但我似乎可以找到解决方案。

请帮忙?

最佳答案

此错误是因为函数 mysqli_fetch_field 返回一个对象,您可能需要阅读 http://php.net/manual/es/mysqli-result.fetch-field.php 这个函数返回每列的信息。

更改此:

    $value = mysqli_fetch_field($resulty);
    $orderid = $value->orderId; 

也许您需要使用:fetch_assoc

http://php.net/manual/es/mysqli-result.fetch-assoc.php

祝你好运

关于PHP - 存储 SELECT 查询并在 INSERT 查询中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47270456/

相关文章:

javascript - 将 JS 变量设置为 PHP 变量错误

php - MYSQL子查询和正则表达式

php - 如何从 Laravel 5.2 中的集合中获取相关模型?

mysql - 子查询返回多行错误

SQL 作为查询工作,但在作为存储过程调用时失败

mysql - SQL JOIN 从第二个表中获取不同的列

mysql - 在 MySQL/InnoDB 中没有表锁定的一致读取和更新

php - 如何在php中通过metakeywords显示相关项目

php - 谷歌地图 API v3 : How to Set Zoom Level And Map Center To User Submitted Location?

php - 收到 CException 错误