使用单选按钮和连接表获取 MySQL 数据
Fetch MySQL data using radio buttons and joining tables
我的tables:
Table1个姓名:拍摄者
字段:id(主键,AI),title,director,year,catid
Table2 姓名:猫
字段:catid(主键,AI),category
将电影添加到数据库可以通过提交表单正常工作,但我希望能够添加一个类别以及单选按钮这里是问题所在,我不知道我需要做什么,我尝试加入 tables 与 catid 之间的关系,但它不会工作,我只得到
没有可显示的结果!
添加电影后,我可以在数据库中看到电影,但在浏览器中看不到,它们在第一个 table 中都获得 catid 0,在第二个 table 中我有catid 值为 1-6 的 6 个类别。
我是这方面的新手,所以非常感谢任何帮助!
这是我的 HTML 表单和单选按钮
<form action="movies.php" method="post">
<input type="radio" name="id" value="1" checked />Action<br>
<br> <input type="radio" name="id" value="2" />Comedy<br>
<br> <input type="radio" name="id" value="3" />Drama<br>
<br> <input type="radio" name="id" value="4" />Horror<br>
<br> <input type="radio" name="id" value="5" />Romantic<br>
<br> <input type="radio" name="id" value="6" />Animated<br><br>
<pre>
Title<input type="text" name="titel">
Director<input type="text" name="director">
Year <input type="text" name="year">
<input type="submit" name="submit" value="Submit" />
</pre>
</form>
这是我的 PHP 文件,用于将电影添加到数据库中。
//Post data
if (isset($_POST['submit'])){
$title = htmlentities($_POST['titel'], ENT_QUOTES);
$director = htmlentities($_POST['director'], ENT_QUOTES);
$year = htmlentities($_POST['year'], ENT_QUOTES);
// empty form = error
if ($title == '' || $director == '' || $year == ''){
$error = 'ERROR: Please fill in all required fields!';
echo $error;
} else {
//inserts movie to database
if ($stmt = $mysqlic->prepare("INSERT INTO filmer
(titel, director, year) VALUES (?, ?, ?)")){
$stmt->bind_param("ssi", $title, $director, $year);
$stmt->execute();
$stmt->close();
// show an error if the query has an error
} else {
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: view.php");
}
}
这里是 PHP 显示数据库中电影的文件
// get movies from the database
if ($result = $mysqlic->query("SELECT cat.catid FROM cat
INNER JOIN filmer ON cat.catid=filmer.catid")){
// display records if there are records to display
if ($result->num_rows > 0){
// display records in a table
echo "<table border='1' cellpadding='10'>";
// set table headers
echo "<tr><th>Title</th>
<th>Director</th>
<th>Year</th>
<th>Category</th>";
while ($row = $result->fetch_object()){
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->titel . "</td>";
echo "<td>" . $row->director . "</td>";
echo "<td>" . $row->year . "</td>";
echo "<td>" . $row->Category . "</td>";
echo "</tr>";
}
echo "</table>";
// if there are no records in the database, display an alert message
} else {
echo "No results to display!";
}
// show an error if there is an issue with the database query
} else {
echo "Error: " . $mysqlic->error;
}
// close database connection
$mysqlic->close();
将新电影添加到 table 时,您没有指定它的 类别 。
谁会猜出您需要什么类别?
所以你需要使用这个查询来澄清这一点:
INSERT INTO filmer (titel, director, year, catid) VALUES (?, ?, ?, ?)
我添加了字段名称 catid
,如您的问题中所述以及额外的 ?
接下来,您必须获取类别 ID。这可以通过以下方式完成:
$cat_id = intval($_POST['id']);
绑定参数后:
$stmt->bind_param("ssii", $title, $director, $year, $cat_id);
如果我没理解错的话,您想要 post 猫 ID 以及其他字段。我已经添加了猫 ID 的名称,因此它被捕获在 $_POST 中。然后放入插入查询。
//Post data
if (isset($_POST['submit'])){
$title = htmlentities($_POST['titel'], ENT_QUOTES);
$director = htmlentities($_POST['director'], ENT_QUOTES);
$year = htmlentities($_POST['year'], ENT_QUOTES);
$catid= htmlentities($_POST['id'], ENT_QUOTES); //gets the catid
// empty form = error
if ($title == '' || $director == '' || $year == ''){
$error = 'ERROR: Please fill in all required fields!';
echo $error;
} else {
//inserts movie to database
if ($stmt = $mysqlic->prepare("INSERT INTO filmer
(titel, director, year. catid) VALUES (?, ?, ?, ?)")){
$stmt->bind_param("ssi", $title, $director, $year, $catid);
$stmt->execute();
$stmt->close();
// show an error if the query has an error
} else {
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: view.php");
}
}
我的tables:
Table1个姓名:拍摄者 字段:id(主键,AI),title,director,year,catid
Table2 姓名:猫 字段:catid(主键,AI),category
将电影添加到数据库可以通过提交表单正常工作,但我希望能够添加一个类别以及单选按钮这里是问题所在,我不知道我需要做什么,我尝试加入 tables 与 catid 之间的关系,但它不会工作,我只得到
没有可显示的结果!
添加电影后,我可以在数据库中看到电影,但在浏览器中看不到,它们在第一个 table 中都获得 catid 0,在第二个 table 中我有catid 值为 1-6 的 6 个类别。
我是这方面的新手,所以非常感谢任何帮助!
这是我的 HTML 表单和单选按钮
<form action="movies.php" method="post">
<input type="radio" name="id" value="1" checked />Action<br>
<br> <input type="radio" name="id" value="2" />Comedy<br>
<br> <input type="radio" name="id" value="3" />Drama<br>
<br> <input type="radio" name="id" value="4" />Horror<br>
<br> <input type="radio" name="id" value="5" />Romantic<br>
<br> <input type="radio" name="id" value="6" />Animated<br><br>
<pre>
Title<input type="text" name="titel">
Director<input type="text" name="director">
Year <input type="text" name="year">
<input type="submit" name="submit" value="Submit" />
</pre>
</form>
这是我的 PHP 文件,用于将电影添加到数据库中。
//Post data
if (isset($_POST['submit'])){
$title = htmlentities($_POST['titel'], ENT_QUOTES);
$director = htmlentities($_POST['director'], ENT_QUOTES);
$year = htmlentities($_POST['year'], ENT_QUOTES);
// empty form = error
if ($title == '' || $director == '' || $year == ''){
$error = 'ERROR: Please fill in all required fields!';
echo $error;
} else {
//inserts movie to database
if ($stmt = $mysqlic->prepare("INSERT INTO filmer
(titel, director, year) VALUES (?, ?, ?)")){
$stmt->bind_param("ssi", $title, $director, $year);
$stmt->execute();
$stmt->close();
// show an error if the query has an error
} else {
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: view.php");
}
}
这里是 PHP 显示数据库中电影的文件
// get movies from the database
if ($result = $mysqlic->query("SELECT cat.catid FROM cat
INNER JOIN filmer ON cat.catid=filmer.catid")){
// display records if there are records to display
if ($result->num_rows > 0){
// display records in a table
echo "<table border='1' cellpadding='10'>";
// set table headers
echo "<tr><th>Title</th>
<th>Director</th>
<th>Year</th>
<th>Category</th>";
while ($row = $result->fetch_object()){
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->titel . "</td>";
echo "<td>" . $row->director . "</td>";
echo "<td>" . $row->year . "</td>";
echo "<td>" . $row->Category . "</td>";
echo "</tr>";
}
echo "</table>";
// if there are no records in the database, display an alert message
} else {
echo "No results to display!";
}
// show an error if there is an issue with the database query
} else {
echo "Error: " . $mysqlic->error;
}
// close database connection
$mysqlic->close();
将新电影添加到 table 时,您没有指定它的 类别 。 谁会猜出您需要什么类别? 所以你需要使用这个查询来澄清这一点:
INSERT INTO filmer (titel, director, year, catid) VALUES (?, ?, ?, ?)
我添加了字段名称 catid
,如您的问题中所述以及额外的 ?
接下来,您必须获取类别 ID。这可以通过以下方式完成:
$cat_id = intval($_POST['id']);
绑定参数后:
$stmt->bind_param("ssii", $title, $director, $year, $cat_id);
如果我没理解错的话,您想要 post 猫 ID 以及其他字段。我已经添加了猫 ID 的名称,因此它被捕获在 $_POST 中。然后放入插入查询。
//Post data
if (isset($_POST['submit'])){
$title = htmlentities($_POST['titel'], ENT_QUOTES);
$director = htmlentities($_POST['director'], ENT_QUOTES);
$year = htmlentities($_POST['year'], ENT_QUOTES);
$catid= htmlentities($_POST['id'], ENT_QUOTES); //gets the catid
// empty form = error
if ($title == '' || $director == '' || $year == ''){
$error = 'ERROR: Please fill in all required fields!';
echo $error;
} else {
//inserts movie to database
if ($stmt = $mysqlic->prepare("INSERT INTO filmer
(titel, director, year. catid) VALUES (?, ?, ?, ?)")){
$stmt->bind_param("ssi", $title, $director, $year, $catid);
$stmt->execute();
$stmt->close();
// show an error if the query has an error
} else {
echo "ERROR: Could not prepare SQL statement.";
}
// redirec the user
header("Location: view.php");
}
}