Home > OS >  MySQL INSERT with a field coming from another table fails
MySQL INSERT with a field coming from another table fails

Time:07-17

I Need to insert data in a table but 1 of the fields will come from another table but not working... Code as below:

   //Save payment to table paylog
   $sql  = "INSERT INTO `paylog`(`payment_id`, `date_approved`, `newcredit`, `before`) VALUES (?,?,?,?)";
   $stmt = $conn->prepare($sql);
   $stmt->bind_param('ssss',$payment->id,$dateApproved,
          $payment->transaction_amount,(SELECT credit from tokens WHERE token=$payment->external_reference LIMIT 1));
   $stmt->execute();
   $stmt->close();

Assistance welcome Paulo

CodePudding user response:

Just move that SELECT subquery up inside of $sql, keep $payment->external_reference as a binded parameter. See https://stackoverflow.com/a/42132551/7977859

  • Related