我正在尝试使用HTML表单从数据库中删除条目。但是,当它被提交时,该条目不会从数据库中删除,也不会显示错误代码来说明原因。
如果我错过了我的代码中的某些内容,有人可能会告诉我,以防万一我过度看了吗?
谢谢。
<form method="post" action="./removeBook.inc.php">
<select name="books">
<option>Please select the Book you wish to delete:</option>
<?php
try {
$dsn = "mysql:host=csdm-mysql;dbname=db1001550_book_management";
$username = "1001550";
$password = "1001550";
// try connecting to the database
$con = new PDO($dsn, $username, $password);
// turn on PDO exception handling
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
// enter catch block in event of error in preceding try block
echo "Connection failed: " . $e->getMessage();
}
try {
$sql=("SELECT * FROM books");
$results = $con->query($sql);
if ($results->rowcount() == 0) {
echo "<p>No books found. </p><br />";
} else {
foreach ($results as $row) {
$book_id=$row['book_id'];
echo "<option value=\"".$book_id."\">".$book_id."</option>";
}
}
} catch (PDOException $e) {
echo "Query failed: " . $e->getMessage();
}
mysql_close();
?>
</select>
<input type="submit" name="submit" value="Remove Book" style="width:auto;">
</form>
以下是表单提交页面中的代码:
<?php
$host="csdm-mysql"; // Host name
$username="1001550"; // Mysql username
$password="1001550"; // Mysql password
$db_name="db1001550_book_management"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("Cannot select DB");
if (isset($_POST['books'])) {
$books=$_POST['books'];
}
// Remove data from database
$result=mysql_query("DELETE FROM books WHERE book_id=$books");
// if successfully deleted from database, prompt will say book was deleted.
if ($result) {
print "<script type=\"text/javascript\">";
print "alert('Book has been deleted successfully!')";
print "</script>";
} else {
print "<script type=\"text/javascript\">";
print "alert('Book was not deleted')";
print "</script>";
}
// close connection
mysql_close();
?>
<form method="post" action="./removeBook.inc.php">
<select name="books">
<option>Please select the Book you wish to delete:</option>
<option value='B0001'>B0001</option><option value='B0002'>B0002</option><option value='B0003'>B0003</option><option value='B0004'>B0004</option><option value='B0005'>B0005</option><option value='B0006'>B0006</option><option value='B0007'>B0007</option><option value='B0008'>B0008</option><option value='B0009'>B0009</option><option value='B0010'>B0010</option><option value='B0011'>B0011</option><option value='B0012'>B0012</option><option value='B0013'>B0013</option><option value='B0014'>B0014</option><option value='B0015'>B0015</option><option value='B0016'>B0016</option><option value='B0017'>B0017</option><option value='B0018'>B0018</option><option value='B0019'>B0019</option><option value='B0020'>B0020</option>
</select>
<input type="submit" name="submit" value="Remove Book" style="width:auto;">
</form>
提前感谢您的帮助!
答案 0 :(得分:1)
尝试打印并查看您是否获得所选的图书ID。 每当你在查询中得到概率打印并看到。
添加此行
print "DELETE FROM books WHERE book_id=$books";
前
$result=mysql_query("DELETE FROM books WHERE book_id=$books");
我认为您没有收到适当格式的图书ID。
更改行
echo "<option value=\"".$book_id."\">".$book_id."</option>";
作为
echo "<option value='".$book_id."'>".$book_id."</option>";
并且还使用单引号来封装值。 改变
$result=mysql_query("DELETE FROM books WHERE book_id=$books");
到
$result=mysql_query("DELETE FROM books WHERE book_id='".$books."'");
答案 1 :(得分:0)
如果您的book_id字段是varchar或text,则应在$ books
之间添加引号...WHERE book_id='$books'