Monday 10 June 2019

Run many mySQL queries through php





I have a database with many tables. On side 1, I have a button that passes me to side 2. On side 2, I want the code to remove every row in every named table that has a specific pid that I have stored in a variable. I have a code on side 2 but it doesn't do anything.



Code on side 2:



$pid3 = $_POST['pid2'];

$del = "DELETE FROM answer_det WHERE pid='$pid3'";
$del .= "DELETE FROM project WHERE pid='$pid3'";
$del .= "DELETE FROM question WHERE pid='$pid3'";
$del .= "DELETE FROM respondent WHERE pid='$pid3'";

$del .= "DELETE FROM result WHERE pid='$pid3'";
$del .= "DELETE FROM users WHERE pid='$pid3'";

$run = mysqli_query($mysqli,$del);

if($run)
{
echo "

Project is deleted!

";
}


Answer



Use mysqli_multi_query



$pid3 = $_POST['pid2'];

$del = "DELETE FROM answer_det WHERE pid='$pid3';";
$del .= "DELETE FROM project WHERE pid='$pid3';";
$del .= "DELETE FROM question WHERE pid='$pid3';";
$del .= "DELETE FROM respondent WHERE pid='$pid3';";
$del .= "DELETE FROM result WHERE pid='$pid3';";

$del .= "DELETE FROM users WHERE pid='$pid3';";

$run = mysqli_multi_query($mysqli,$del);

if($run)
{
echo "

Project is deleted!

";
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...