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

时间:2017-11-13 17:46:59

标签: 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);


?>

但是我得到了 -

  

致命错误:不能在第30行使用stdClass类型的对象作为数组。

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

请帮帮忙?

3 个答案:

答案 0 :(得分:0)

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

改变这个:

{{1}}

也许您需要使用: fetch_assoc

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

祝你好运

答案 1 :(得分:0)

如果您想获取列数据,请使用fetch assoc:

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

if ($resulty !== false) {
    $fila = $resultado->fetch_assoc()
    $orderid = $fila['orderId'];
}

答案 2 :(得分:0)

最好的解决方案是:

https://dev.mysql.com/doc/refman/5.7/en/insert-select.html

您无需拉出数据即可将其插回。让MySQL为你做。

这是我对组合insert-select语句的看法。你会试试吗?

$sqlquery = "INSERT INTO orderitems (orderId, productId, quantity)
SELECT orderId, $ID, $QUANTITY from tableorders WHERE tableNumber='$TABLE' ORDER by orderDate DESC limit 1";

我放弃了偏移,因为我没有看到它的目的。

以下是一个例子:

想象一下,有一个名为tasks的表有很多行,我们想从中选择一个id值,并将它与一些外部值一起插入到这个测试表中。

mysql> create table test (id int, order_id int, product_id int);
Query OK, 0 rows affected (0.09 sec)

mysql> describe test;
+------------+---------+------+-----+---------+-------+
| Field      | Type    | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| id         | int(11) | YES  |     | NULL    |       |
| order_id   | int(11) | YES  |     | NULL    |       |
| product_id | int(11) | YES  |     | NULL    |       |
+------------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into test select id, 5, 6 from tasks order by id asc limit 1;
Query OK, 1 row affected (0.06 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from test;
+------+----------+------------+
| id   | order_id | product_id |
+------+----------+------------+
|    1 |        5 |          6 |
+------+----------+------------+
1 row in set (0.00 sec)

mysql> insert into test(id, order_id, product_id) select id, 5, 6 from tasks order by id asc limit 1;
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from test;
+------+----------+------------+
| id   | order_id | product_id |
+------+----------+------------+
|    1 |        5 |          6 |
|    1 |        5 |          6 |
+------+----------+------------+
2 rows in set (0.00 sec)

mysql>

我希望它能说明插入选择方法的可能性