0

I have made an insert and update multi_query with the code below. When I submit the form both actions execute and alert that it was successful - looking in PHPMyAdmin I can confirm that it has been inserted and updated. However, it then tells me that that the query couldn't be executed php client output.

if(isset($_POST['submit']) && isset($_POST['partno']) !== "") {
    $date = date('Y-m-d');
    $partNumber = $_POST['partno'];
    $checkedIn = $_POST['checkedin'];
    $amountReceived = $_POST['amountreceived'];
    $stockQuantity = $_POST['stockquantity'];
    $location = $_POST['stocklocation'];
    $checkBox = ($_POST['selectGoods']) ? 1:0;

    $insert = "UPDATE part SET InventoryOnHand = '".$stockQuantity."' WHERE OurPartNo = '".$partNumber."'; ";
    $insert .= "INSERT INTO goods_received (Date, PartNumber, AmountReceived, Who, Location, Received) VALUES ('$date', '$partNumber', '$amountReceived', '$checkedIn', '$location', '$checkBox')";


    if (mysqli_multi_query($con, $insert)){
        echo '<script>alert("Insert Successful")</script>';
    } else{
        echo '<script>alert("Tom Is a Idiot and this is not working")</script>';
    }

    var_dump($insert);
}

Copy for the var_dump and the execute issue:

string(212) "UPDATE part SET InventoryOnHand = '10' WHERE OurPartNo = 'FK06-P'; INSERT INTO goods_received (Date, PartNumber, AmountReceived, Who, Location, Received) VALUES ('2021-04-13', 'FK06-P', '1', 'TOm222', 'GF1', '0')"

Couldn't execute query. Commands out of sync; you can't run this command now

I have already tried:

$insert = "UPDATE part SET InventoryOnHand = '".$stockQuantity."' WHERE OurPartNo = '".$partNumber."'; ";
mysqli_store_result($insert);
$insert .= "INSERT INTO goods_received (Date, PartNumber, AmountReceived, Who, Location, Received) VALUES ('$date', '$partNumber', '$amountReceived', '$checkedIn', '$location', '$checkBox')";
mysqli_use_result($insert);
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Do you happen to have a select query above this bit of code? – aynber Apr 13 '21 at 14:37
  • @aynber No there is not – toommmyyy10 Apr 13 '21 at 14:38
  • 3
    Your code is vulnerable to [SQL injection](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). Consider dropping `mysqli_multi_query` in favor of executing individual prepared statements to [prevent it](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – El_Vanja Apr 13 '21 at 14:48
  • There does not seem to be any good reason to do these 2 queries in the multi_query. Instead do 2 seperate queries but within a transaction. That way at least if one goes wrong you will be able to tell which one – RiggsFolly Apr 13 '21 at 14:57
  • Regarding ___I have already tried:___ That of course wont work as you have only put the query into a string variable, you have not actually submitted it to the database for execution – RiggsFolly Apr 13 '21 at 14:58
  • @RiggsFolly how do I go about doing the transaction? Even if you can only give me a rough idea. – toommmyyy10 Apr 13 '21 at 15:01
  • Always start with the manual [mysqli::begin_transaction](https://www.php.net/manual/en/mysqli.begin-transaction.php) The Examples are normally very useful when learning – RiggsFolly Apr 13 '21 at 15:03
  • 1
    @RiggsFolly brilliant thank you – toommmyyy10 Apr 13 '21 at 15:49
  • phpMyAdmin is a MySQL administration tool written in PHP, it is not a database itself. You are probably using MySQL or MariaDB as your DB. – Dharman Apr 13 '21 at 19:43

0 Answers0