我不知道我在哪里收到错误但我无法通过id删除该行。这是我的代码。我只删除了脚本而不是编辑脚本所以请忽略它。
<?php include "includes/connection.php";?>
<?php include "includes/header.php";?>
<?php
if(isset($_POST['menu_submit'])){
$item_name = mysql_real_escape_string($_POST['item_name']);
$item_link = mysql_real_escape_string($_POST['item_link']);
$insert_query = "INSERT INTO navbar (navbar_title, navbar_link) values('$item_name', '$item_link')";
if($item_name=="" || $item_link==""){
$required = '<div class="error">All Fields are Required</div>';
}
else{
if(mysql_query($insert_query)){
$added = '<div class="success">Menu Item Added Successfully</div>';
}
else{
$failed = '<div class="error">Failed to Add Menu Item</div>';
}
}
}
?>
<div id="content">
<h2>Add Menu Item</h2>
<?php if(isset($added)){echo $added;} ?>
<?php if(isset($failed)){echo $failed;} ?>
<?php if(isset($required)){echo $required;} ?>
<form method="post" action="add_menu.php">
Menu item Name: <input type="text" name="item_name">
Menu item Link: <input type="text" name="item_link">
<input type="submit" name="menu_submit" value="Add Item Now">
</form>
</br>
<table>
<tr>
<th>Item Name</th>
<th>Item Link</th>
<th colspan="2">Options</th>
</tr>
<?php
$run_query = "SELECT * From navbar";
$get_query = mysql_query($run_query);
while($rows=mysql_fetch_array($get_query)){
$get_item_id = $rows['navbar_id'];
$get_item_name = $rows['navbar_title'];
$get_item_link = $rows['navbar_link'];
?>
<tr>
<td><?php echo $get_item_name; ?></td>
<td><?php echo $get_item_link; ?></td>
<td><a href="add_menu.php?edit=<?php echo $get_item_id; ?>">Edit</a></td>
<td><a href="add_menu.php?delete=<?php echo $get_item_id; ?>">Delete</a></td>
</tr>
<?php }?>
</table>
</div>
<?php include "includes/footer.php";?>
这是我的删除脚本,请忽略我正在处理的编辑脚本。
<?php
if(isset($_GET['edit'])){$edit_item_id = $_GET['edit'];}
if(isset($_GET['delete'])){$delete_item_id = $_GET['delete'];}
$delete_query = 'DELETE FROM navbar where id="$delete_item_id"';
if(mysql_query($delete_query)){
$item_deleted = '<div class="success">Item has been Deleted</div>';
}
else{
$item_not_deleted = '<div class="error">Item did not Deleted Successfully</div>';
}
?>
答案 0 :(得分:1)
如果设置了
,您将检查错误的变量更改此
if(isset($edit_item_id)){$edit_item_id = $_GET['edit'];}
if(isset($delete_item_id)){$delete_item_id = $_GET['delete'];}
到此
if(isset($_GET['edit'])) $edit_item_id = $_GET['edit'];
if(isset($_GET['delete'])) $delete_item_id = $_GET['delete'];
此外,您很容易受到SQL注入攻击 请确保验证,清理变量并开始使用MySQLi
答案 1 :(得分:0)
查询错误:
$delete_query = 'DELETE FROM navbar where id="$delete_item_id"';
改为使用:
$delete_query = "DELETE FROM navbar WHERE id='$delete_item_id'";
编辑:
改变这两行:
if(isset($_GET['edit'])){$edit_item_id = $_GET['edit'];}
if(isset($_GET['delete'])){$delete_item_id = $_GET['delete'];}
答案 2 :(得分:0)
我不确定您为什么要检查$ delete_item_id,我认为您打算检查并查看是否已使用$ _GET传递了值。
其次,您需要确保创建的字符串允许变量。我个人更喜欢将它们连接起来。
<?php
if(!isset($_GET['delete'])){$delete_item_id = $_GET['delete'];}
$delete_query = "DELETE FROM navbar where id=" . $delete_item_id ."";
if(mysql_query($delete_query)){
$item_deleted = '<div class="success">Item has been Deleted</div>';
}
else{
$item_not_deleted = '<div class="error">Item did not Deleted Successfully</div>';
}
?>
答案 3 :(得分:-2)
怎么样?
$delete_query = "DELETE FROM navbar WHERE id='{$delete_item_id}'";
也:
if(!isset($delete_item_id)){$delete_item_id = $_GET['delete'];}
在这里注意一点变化......