-2

I'm trying to input multiple queries guys using mysqli. Yet it's not populating the database. Any ideas?

$q2="UPDATE ticketinfo SET ticketstatus = $status where ticketno = $ticket; 
  insert into ticketinfo (remarks) values ('$remarks')";

$ex2= mysqli_multi_query($conn,$q2);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Kev
  • 1
  • 3
  • Check for syntax errors. – Sougata Bose Dec 26 '19 at 10:58
  • I did bro. But it seems it's not getting to these part. if ($ex2){ echo 'We have successfully processed your ticket update. Thank you.>'; } else { echo 'We have received an error while processing your ticket update. Please try again.'; mysqli_error ($conn); } – Kev Dec 26 '19 at 11:00

1 Answers1

0

SQL queries should be executed sequentially. Never use mysqli_multi_query() with variable input. You should be using parameterized prepared statements. There is hardly any use case for mysqli_multi_query() at all.

Your code should look like this:

// your mysqli connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'username', 'password', 'dbname');
$mysqli->set_charset('utf8mb4'); // always set the charset

// First query
$stmt = $mysqli->prepare('UPDATE ticketinfo SET ticketstatus = ? WHERE ticketno = ?');
$stmt->bind_param('ss', $status, $ticket);
$stmt->execute();

// Second query
$stmt = $mysqli->prepare('INSERT INTO ticketinfo (remarks) VALUES (?)');
$stmt->bind_param('s', $remarks);
$stmt->execute();

I used two prepared statements and bound the input separately. This much better, cleaner and safer option than mysqli_multi_query().

Dharman
  • 30,962
  • 25
  • 85
  • 135