简体   繁体   English

尝试将mysql表ID行放入php变量以从中获取更多mysql查询

[英]Trying to get a mysql table id row into php variable to base futher mysql queries from it

I am programming in a PHP, HTML and SQL and got stuck in some part of my project. 我正在使用PHP,HTML和SQL进行编程,并且陷入了项目的某些部分。

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. 然后,我尝试搜索该电影的ID,然后在存在该ID的每个其他表中尝试删除与该ID关联的所有数据,然后从主表本身中删除有关电影的数据。 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? 当电影名称为“绿色英里”时,有人可以指出一种删除ID为3的电影的所有表记录的好方法吗?

<?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. 如果第13行未返回结果,则$ dana不等于任何值,这将对您的所有查询结果抛出错误。

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM