【发布时间】:2015-03-23 11:07:03
【问题描述】:
我的桌子:
表 1 名称:filmer 字段:id(主键,AI),title,director,year,catid
表 2 名称:猫 字段:catid(主键,AI),类别
使用提交表单将电影添加到数据库可以正常工作,但我希望能够使用单选按钮添加一个类别,这里的问题是我不知道我需要做什么,我已经尝试过用 catid 之间的关系加入表格,但它不起作用,我只能得到 p>
没有结果可显示!
添加电影后,我可以在数据库中看到电影,但在浏览器中看不到,它们都在第一个表中得到 catid 0,在第二个表中,我有 6 个类别,catid 值从 1-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();
【问题讨论】:
-
"INSERT INTO filmer (titel, director, year, catid) VALUES (?, ?, ?, ?)" -
记得抓住id $catid= htmlentities($_POST['id'], ENT_QUOTES); 的帖子,然后插入(如上)
标签: php mysql button join radio