简体   繁体   English

SQL查询可在SQL中使用,但不适用于PHP

[英]SQL Query works in SQL but not in PHP

I have a relational insert which works like a charm in mysql, but when I put in into a query in PHP, nothin' doin'. 我有一个关系插入,它像mysql中的超级按钮一样工作,但是当我将其插入PHP查询中时,没什么可做的。 Can someone help? 有人可以帮忙吗?

        $qry = "
            INSERT into orders
                ( customerid, date, order_status ) 
            VALUES 
                ( '$customerid', '$date', $order_status );
            INSERT into order_items
                ( orderid, isbn, item_price, quantity )
            VALUES
                ( LAST_INSERT_ID(), '12345', 5, 1 )
        ";

when I remove the second insert, it works as advertised in PHP. 当我删除第二个插入时,它的工作方式与PHP中宣传的一样。 I am running EasyPHP5.3. 我正在运行EasyPHP5.3。 Thanks! 谢谢!

Unless you are using mysqli_multi_query() you cannot run more than one query at a time in PHP. 除非使用mysqli_multi_query()否则在PHP中一次不能运行多个查询。 So you'll need to break that query into two queries or use the previously mentioned function. 因此,您需要将该查询分为两个查询或使用前面提到的函数。

Hope this work for you 希望这项工作对您有帮助

        $qry = "
        INSERT into orders
            ( customerid, date, order_status ) 
        VALUES 
            ( '$customerid', '$date', $order_status )";

        $qry.=" INSERT into order_items
            ( orderid, isbn, item_price, quantity )
        VALUES
            ( LAST_INSERT_ID(), '12345', 5, 1 )
    ";   

    $mysqli->multi_query($query)       

OK, so SQL and PHP may have some common grounds but they still have differences. 好的,因此SQL和PHP可能有一些共同点,但仍然存在差异。

If you want to perform multiple queries, at time it might result into an error in PHP because PHP needs to send a request to SQL before it executes the query and one request is to one query. 如果要执行多个查询,这可能会导致PHP错误,因为PHP需要在执行查询之前向SQL发送一个请求,而一个请求就是向一个查询发送。 (I think meehee). (我想看)。

If you were to transfer your code to PHP this is the code, I guess you already know how to setup a connection between your php file and your database right, if not feel free to ask or update your question. 如果您要将代码转移到PHP,这就是代码,我想您已经知道如何在php文件和数据库之间建立连接,如果您不随意提出或更新问题的话。 But this is how to do your code in PHP: 但这是在PHP中执行代码的方法:

<?php
$sqlOrders = "INSERT INTO orders (customer_id, date, order_status)
VALUES
('$customer_id','$date','$customer_status')";

$sqlOrderItems = "INSERT INTO order_items (orderid, isbn, item_price, quantity)
VALUES
(LAST_INSERT_ID(), '12345', 5, 1)";

After that you need to call this command for this is the request. 之后,您需要为此命令调用此命令。

if(!mysqli_query($link <--- connection to your database,$sqlOrders)){
die('Error: ' . mysqli_error($link));
}

if(!mysqli_query($link,$sqlOrderItems)){
die('Error: ' . mysqli_error($link));
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM