I am programming in a PHP, HTML and SQL and got stuck in some part of my project.
In the following code I tried to recieve an string that is meant to represent a name of a movie from a textbox after a button press. I then tried to search for ID of that film and then in everyother table where that ID is present I tried to remove all data tied to that ID then remove the data about the movie from the main table itself. Yet I run in tons of different errors I can't handle whenever I try another approach.
Could someone point me a nice way to remove all table records about a movie with ID for example 3 when the movie name is The Green Mile?
<?php
IF ($_SERVER["REQUEST_METHOD"] == "POST") {
$con=mysqli_connect("localhost","root","","bazus");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_query('SET foreign_key_checks = 0');
$tytul = mysqli_real_escape_string($con, $_POST['tytul']);
$id = "SELECT id FROM filmy WHERE tytul=$tytul";
$dana = mysql_query($id);
$film_przyznano = "DELETE FROM przyznana WHERE filmy_id='$dana'";
$premiera = "DELETE FROM premiera WHERE filmy_id='$dana'";
$obsada = "DELETE FROM obsada WHERE filmy_id='$dana'";
$film_gatunek = "DELETE FROM film_gatunek WHERE filmy_id='$dana'";
$rezyseria = "DELETE FROM rezyseria WHERE filmy_id='$dana'";
$scenariusz = "DELETE FROM scenariusz WHERE filmy_id='$dana'";
$film_producent = "DELETE FROM film_producent WHERE filmy_id='$dana'";
//mysql_query($film_przyznano);
//mysql_query($obsada);
//mysql_query($premiera);
//mysql_query($film_gatunek);
//mysql_query($rezyseria);
//mysql_query($scenariusz);
//mysql_query($film_producent);
/*$sql="DELETE FROM filmy WHERE tytul='$tytul'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record deleted";
$tytul="";*/
mysql_query('SET foreign_key_checks = 1');
}
?>
<div id="remove">
<form action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>' method='post'>
<input type="text" name="tytul">
<input type="submit">
</form>
</div>
It seems to me that you code is good. What you are lacking is error handling. If line 13 does not return a result, then $dana is equal to nothing, which will toss an error for all the result of your queries.
You should add a line after that something like this
if ($dana > 0) {
// do your delete queries
} else {
// do nothing echo error
echo "No movie found";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.