MySQL UPDATE在phpMyAdmin中工作,但不在PHP中

问题描述:

我需要从PHP更新数据库记录。MySQL UPDATE在phpMyAdmin中工作,但不在PHP中

这里是我的PHP代码:

if(isset($_FILES["fileEdit"])){ 
    if($_FILES["fileEdit"]["error"] == 0){ 

     $id = $_POST["id"]; 
     $productName = $_POST["name"]; 
     $productDesc = $_POST["desc"]; 
     $productPrice = $_POST["price"]; 
     $productPath = "images/" . $_FILES["fileEdit"]["name"]; 

     move_uploaded_file($_FILES["fileEdit"]["tmp_name"], $path . $_FILES["fileEdit"]["name"]); 

     $sql = "UPDATE products SET name='$productName', description='$productDesc', price='$productPrice' img_src='$productPath' WHERE id='$id'"; 

     $result = $dbCon->query($sql); 

     echo "edit--> " . $_FILES["fileEdit"]["name"] . " -> no errors, " . $productName . " desc: " . $productDesc . " price: " . $productPrice . "id: ".$id; 
    }else{ 
     echo $_POST["name"]." -> error"; 
    } 
} 

这里是我的JavaScript:

$("#submitEditBtn").click(function(){ 

     var file_data = $('#editImg').prop('files')[0]; 
     var form_data = new FormData(); 

     if(file_data) 
      form_data.append('fileEdit', file_data); 
     else 
      form_data.append('noImgChanges', "true"); 

     form_data.append('name', $("#editP_name").val()); 
     form_data.append('desc', $("#editP_desc").val()); 
     form_data.append('price', $("#editP_price").val()); 
     form_data.append('id', $("#productEditing").attr("name")); 

     $.ajax({ 
      url: 'imgParser.php', 
      dataType: 'text', 
      cache: false, 
      contentType: false, 
      processData: false, 
      data: form_data, 
      type: 'POST', 
      success: function (response) { 
       console.log(response); 
      }, 
      error: function (response) { 
       console.log("ERROR OCCURED"); 
      } 
     }); 

    }); 

那么,什么情况是,我填写的信息(输入,图像等),然后按下按钮,它发送一个带有项目id的请求作为数据。 PHP应该用新的信息更新记录,但它不会,但如果我在SQL控制台中输入完全相同的命令并用一些数据替换变量,它将成功更新它。同样在chrome控制台中,它输出所有数据(正在被回显)。

任何意见/建议表示赞赏

编辑:

我打印的错误(如果有任何)在控制台中,我得到了

你在你的SQL语法错误;检查对应于你的MySQL服务器版本使用附近的“img_src =”图像/ 80sKIDTee.png正确的语法手册“WHERE ID ='1行

+1

您是否尝试'回声$ SQL;'确保你已经正确的查询? –

+0

你有没有得到任何错误? – Amir

+0

是的显然有 –

我觉得你从问题中,如果它不因为你的工作被保存”不要清理输入,而其他人可能会在某些情况下清理数据库点,但

更新您的SQL代码如下:

$sql = "UPDATE products SET name='$productName', description='$productDesc', price='$productPrice', img_src='$productPath' WHERE id='$id'"; 

price='$productPrice'后缺少逗号。

+0

哇不能相信我错过了这个逗号,你是不是指“不清理输入”的意思? –

+2

@VadimTatarnikov您直接输入'$ _POST'数据到查询中。你需要看看SQL注入预防(https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – DomLip

+0

干杯我会研究它,即使这个接口纯粹是为了管理员使用,所以SQL注入不是问题。 –

1”添加漏掉您的SQL声明:

$sql = "UPDATE products SET name='$productName', description='$productDesc', price='$productPrice', img_src='$productPath' WHERE id='$id'"; 

使表名称为简单的行情或尝试是error.log SQL查询,并在phpMyAdmin运行

您在查询失踪,附近img_src = '$ productPath'

$sql = "UPDATE products SET name='$productName', description='$productDesc', price='$productPrice',img_src='$productPath' WHERE id='$id'";