0

我正在尝试删除数据库中的特定字段(不是整行)。但现在,它并没有删除我数据库中的任何内容。

当用户单击删除按钮时,我想清空保存图像路径的字段。

HTML部分:

<div class="delete_image"><a href="#" id="<?php echo $recipe_path ?>"   
class="delete_image">Remove!</a></div> 

AJAX 部分:

$('.delete_image').on("click",function() 
{
var ID = $(this).attr("id");
var dataString = '$recipe_path='+ ID;
if(confirm("Are you sure you want to delete this image ?"))
  {
   $.ajax({
   type: "POST",
   url: "remove.php",
   data: dataString,
   cache: false,  
   });
}
return false;
});

PHP部分(remove.php):

<?php
include "includes/db.php";

if ($_POST['recipe_path'])
  {
   $recipe_path = $_POST['recipe_path'];
   $sql = query("DELETE FROM recipes WHERE $recipe_path = '$recipe_path' ");
  }
?>

我对 SQL 查询做错了吗?

4

2 回答 2

2

那不应该是

$sql = query("DELETE FROM recipes WHERE recipe_path = '$recipe_path' ");

为什么$recipe_path

还要确保页面准备就绪后您正在运行该功能。

$(function(){
     $('.delete_image').on("click",function() 
    {
    var ID = $(this).attr("id");
    var dataString = '$recipe_path='+ ID;
    if(confirm("Are you sure you want to delete this image ?"))
      {
       $.ajax({
       type: "POST",
       url: "remove.php",
       data: dataString,
       cache: false,  
       });
    }
    return false;
    });
});

正如已经指出的那样,DELETE 将删除整行。

于 2013-07-09T21:06:22.547 回答
1

您的 dataString 没有正确的序列化语法,这就是代码不起作用的原因,请尝试将其更改为:

var dataString = 'recipe_path='+ ID;

现在对于 UPDATE 代码,假设ID上面引用了配方路径,您需要执行以下操作:

$sql = query("UPDATE recipes SET recipe_path = NULL WHERE recipe_path = '$recipe_path'");
于 2013-07-09T21:44:35.960 回答