我对 php 和 mysql 很陌生,所以请多多包涵。我编写了这段代码,但是当我尝试从数据库中删除记录时出现以下错误:
<?php
require_once 'login_rankings.php';
$mysqli = new mysqli($db_hostname,$db_username,$db_password,$db_database);
if ($mysqli->connect_errno) {
echo "Failed to connect to database: " . $mysqli->connect_errno . ": " .
$mysqli->connect_error;
}
$query = "SELECT * FROM nfl";
$stmt = $mysqli->prepare($query);
$stmt->execute();
$stmt->store_result();
printf("Number of rows: %d.\n", $stmt->num_rows); echo "<br /><br /><br />";
if (isset($_POST['delete']) && isset($_POST['id']))
{
// $id = get_post('id');
$query = "DELETE FROM nfl WHERE id= :id LIMIT 1";
try{
$stmt = $mysqli->prepare($query);
$stmt->execute(array(':id' => $_POST['id']));
}catch(Exception $e){
error_log($e->getMessage() . '\r\n',3, errors.log);
}
/* not working either
$stmt = $mysqli->prepare($query);
$stmt->bind_param("id", $id);
$stmt->execute(); */
}
$rows = $stmt->num_rows;
$res = $mysqli->query($query);
for ($j = 0; $j < $rows; ++$j)
{
$row = $res->fetch_assoc();
require_once 'abbrs.php';
echo <<<_END
<pre>
Last name: $row[lastname]
First name: $row[firstname]
Team: $row[team]
Position: $row[position]
Number: $row[number]
Age: $row[age]
Id: $row[id]</pre>
<form action="ranking_nfl.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="id" value="$row[id]" />
<input type="submit" value="DELETE" />
</form>
_END;
}
$stmt->close();
$mysqli->close();
?>
致命错误:在第 24 行的 C:\web\project2\ranking_nfl.php 中的非对象上调用成员函数 execute()
我猜我的代码缺少从表单中获取“id”结果的函数?我不知道我应该写什么。