我正在关注CRUD的教程。实际上我在删除功能,但是我无法使删除功能起作用。
每次按下删除按钮时都会出现错误。
如果有人帮我解决以下错误,我会很高兴:
注意:未定义的索引:第17行的C:\ wamp \ www \ member \ index.php中的id
这是我的index.php的代码
<!DOCTYPE html>
<html>
<head>
<title> Delete Record</title>
</head>
<body>
<?php
//include database configuration
include 'config.php';
//check if the action is complete, we use GET this time since we get the action data from the url
isset($_GET['action']) ? $action = $_GET['action'] : $action='';
if($action == 'delete') { //if the user clicked OK, run our delete query
$id = $_REQUEST['id'];
$query = mysqli_query($conn, "DELETE FROM user WHERE id='$id'") or die(mysqli_error($conn));
if($query){ //this will display when the query was successful
echo "<div>Record was deleted.</div>";
}
}
// selecting records
$query2 = mysqli_query($conn, "SELECT * FROM user") or die(mysqli_error($conn));
// count how many records found
$num = mysqli_num_rows($query2);
if($num > 0){ //check if more that o records found
echo "<table border='1'>"; //start table
//creating table heading
echo "<tr>";
echo "<th>First Name</th>";
echo "<th>Last Name</th>";
echo "<th>User Name</th>";
echo "<th>Action</th>"; //we're gonna add this fpr delete action
echo "<th>Edit Action</th>";
echo "</tr>";
//retrieve our table content
while($row = mysqli_fetch_array($query2)){
//extract raw
//this will make $row['firstname'] to
//just $firstname only
extract ($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$firstname}</td>";
echo "<td>{$lastname}</td>";
echo "<td>{$username}</td>";
//we have the delete link here, you can also put your edit link here
echo "<td>";
echo "<a href='#' onclick='delete_user({$id});'>Delete</a>";
echo "</td>";
echo "<td>";
echo "<a href='edit.php?id=<?php echo $id; ?>'>Edit</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>"; //end of table
}else{ //if no records found
echo "<div>No Records Found</div>";
}
?>
<script type="text/javascript">
function delete_user(id){
//this script help us to
var answer = confirm('are you sure?');
if (answer){ //if clicked OK
//redirect to url with action as delete and id recoord to be deleted
window.location = 'index.php?action=delete&id' + id;
}
}
</script>
</body>
</html>
答案 0 :(得分:1)
你有一个错字:
window.location = 'index.php?action=delete&id' + id;
应该是:
window.location = 'index.php?action=delete&id=' + id;