我正在尝试使用我的删除按钮,该按钮连接到一个单独的文件,该文件删除数据库中我的表中的数据。
我已经成功地在数据中的每一行都放置了一个按钮,但是当我收到此错误消息时,我无法删除该行:
DELETE FROM users WHERE id =
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
我相信它可能是非常小的东西?但我无法弄清楚是什么。
admin.php的
<?php
$servername = "localhost";
$username = "root";
$pass = "root";
$dbname = "tutor_database";
// Create connection
$conn = new mysqli($servername, $username, $pass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, FirstName, LastName, Role, Email, Username FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>First Name</th><th>Last Name</th><th>Role</th><th>Email</th><th>Username</th><th>Delete</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["FirstName"]."</td><td>".$row["LastName"].
"</td><td>".$row["Role"]."</td><td>".$row["Email"]."</td><td>".$row["Username"]."</td></tr>";
?>
<td>
<form method="get" action="delete.php">
<input type="hidden" name="rowid" value="<?php echo $row['id']; ?>">
<input class="btn-default" type="submit" name="delete" value="Delete">
</form>
</td>
<?php
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
delete.php
<?php
$servername = 'localhost';
$username = 'root';
$pass = 'root';
$database = 'tutor_database';
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//obtain the rowid from $_GET
if(isset($_GET['id'])){
$id = ($_GET['rowid']);
}
//sql to delete record.
$sql = "DELETE FROM users WHERE id = ". $id;
$conn->exec($sql);
echo "Record deleted!";
require 'adminPage.php';
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
会很欣赏这双眼睛。
谢谢。
答案 0 :(得分:0)
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//obtain the rowid from $_GET
if(isset($_GET['id'])){
$id = ($_GET['rowid']);
}
//sql to delete record.
$sql = "DELETE FROM users WHERE id = ". $id;
$conn->exec($sql);
echo "Record deleted!";
require 'adminPage.php';
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
应该是
//obtain the rowid from $_GET
if(isset($_GET['rowid'])){
$id = ($_GET['rowid']);
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//sql to delete record.
$sql = "DELETE FROM users WHERE id = ". $id;
$conn->exec($sql);
echo "Record deleted!";
require 'adminPage.php';
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
} else {
echo "error: no row id specified";
}
$conn = null;
首先,您的验证在try / catch中,即使没有指定ID,它也会尝试执行;它基本上是一个逻辑失败。在运行查询之前检查该值并确定是否存在该值,因为它会立即检查该值,然后尝试查询,无论是否存在值。其次,您使用if (isset($_GET['id']))
代替if (isset($_GET['rowid']))