I have a INSERT query to copy the value of id into another row called sortingId
, which works fine.
It works too when I try to copy id, into sortingId directly in phpmyadmin.
But combined in the following code, it dosen't work. No errors, but sortingId is just 0.
And if I change $sql2
to "asdjh" it doesn't get and error, so it seem like the second mysql_query
is just ignored and never executed
$sql1 = "INSERT INTO items (name) VALUES (\'TEC TEST !"#!12\');";
$sql2 = "SELECT @last_id := MAX(id) FROM items; UPDATE items SET sortingId = id WHERE id = @last_id;";
$sql = mysqli_query($this->db, $sql1);
$sql = mysqli_query($this->db, $sql2);
if (mysqli_insert_id($this->db) > 0) {
$this->response($this->json(array( 'inserted_id' => mysqli_insert_id($this->db))), 200);
}
UPDATE - with the big help from tadman:
Had to separate the mysqli queries by removing the ";" and make a query for each line:
$sql1 = "INSERT INTO items (name) VALUES (\'TEC TEST !"#!12\')";
$sql2 = "SELECT @last_id := MAX(id) FROM items";
$sql3 = "UPDATE items SET sortingId = id WHERE id = @last_id";
$sql = mysqli_query($this->db, $sql1);
if(mysqli_insert_id($this->db) > 0){
$responsedId = mysqli_insert_id($this->db);
$sql = mysqli_query($this->db, $sql2);
$sql = mysqli_query($this->db, $sql3);
$this->response($this->json(array( 'inserted_id' => $responsedId)), 200);
}