0

I have a very specific problem and nothing I could find online was able to tell me where my error was.

I want to pass two mysql queries at once. Separately, they work perfectly but together they fail. I've tries JOIN, adding ; and the multi_queries method. Everything fails.

Now I am stuck with this code:

// data insertion
$sql = "INSERT INTO comments (id, name, email, comment, article_id, date) VALUES ('$id', '$name', '$email', '$comment', '$article_id', '$date')";
$sql.= "DELETE FROM comments_validation WHERE id = $id";

if ($conn->multi_query($sql) === TRUE) {
    header('Location: http://url.com/index.php?success');

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

And the error:

Error: INSERT INTO comments (id, name, email, comment, article_id, date) VALUES ('some values')DELETE FROM comments_validation WHERE id = 'some other value' You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELETE FROM comments_validation WHERE id = 'some other value' at line 1

Thanks in advance!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Tripduc
  • 31
  • 1
  • 11

2 Answers2

0

You have to add a ; at the end of this sql statement

$sql = "INSERT INTO comments (id, name, email, comment, article_id, date) VALUES ('$id', '$name', '$email', '$comment', '$article_id', '$date');";
                                                                                                                                               ^here                                                
EstevaoLuis
  • 2,422
  • 7
  • 33
  • 40
0

Please add semi-colon as string at the end of every query in multi query.

// data insertion
$sql = "INSERT INTO comments (id, name, email, comment, article_id, date) VALUES ('$id', '$name', '$email', '$comment', '$article_id', '$date');";
$sql.= "DELETE FROM comments_validation WHERE id = $id";

if ($conn->multi_query($sql) === TRUE) {
    header('Location: http://url.com/index.php?success');

} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Vijay Maurya
  • 99
  • 1
  • 6