【问题标题】:Query updating all records rather than record which matches input?查询更新所有记录而不是与输入匹配的记录?
【发布时间】:2017-05-06 15:35:08
【问题描述】:

为含糊的标题道歉。 我目前有一个编辑表单,可以在其中选择产品,详细信息显示在可以编辑的表单中。不幸的是,当编辑产品表中的每个产品时,而不仅仅是选择的产品。选择我正在使用的产品 where productname = productnameinput etc etc(代码如下)

数据库:http://prnt.sc/f4tf5g(编辑前)

尝试在更新查询末尾添加以下 WHERE 语句:

UPDATE product SET CategoryID = :newCatId,  ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE product.ProductName = :prodname

UPDATE product SET CategoryID = :newCatId,  ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE ProductName = :prodname

PHP:

// edit product in database
$query="
  SELECT * FROM category
";
$result = $DBH->prepare($query);
$result->execute();
$categories = $result->fetchAll();
//we need to select all products frist
$query3 = "
  SELECT * FROM product
";
$result3 = $DBH->prepare($query3);
$result3->execute();
$allProducts = $result3->fetchAll();

//When the Product is selected this function is run
  if (isset($_POST['choose'])) {
    $query2 = "
      SELECT product.*, category.* FROM product LEFT JOIN category ON category.CategoryID = product.CategoryID WHERE product.ProductName = :prodname
    ";
    $result2 = $DBH->prepare($query2);
    $result2->bindParam(':prodname', $_POST['product_name']);
    $result2->execute();
    $product = $result2->fetch();
  }
  //When the Update button is Pressed
    if (isset($_POST['update'])) {
      $query4 = "
        UPDATE product SET CategoryID = :newCatId,  ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock WHERE product.ProductName = :prodname
      ";
      $result4 = $DBH->prepare($query4);
      $result4->bindParam(':newCatId', $_POST['newcategory']);
      $result4->bindParam(':newProdName', $_POST['productName']);
      $result4->bindParam(':newProdDesc', $_POST['productDescription']);
      $result4->bindParam(':newStock', $_POST['stockCount']);
      $result4->bindParam(':prodname', $_POST['product_name']);

      $result4->execute();

    } 

【问题讨论】:

  • 您需要在update 上添加一个where 子句。
  • 现在试试,谢谢。如果不成功会更新问题,但我认为你是对的
  • @chris85 很确定你是对的,但我仍然遇到错误,我已经更新了我的问题。我应该包括 $post 产品名称等吗?
  • 错误是什么,你绑定了新的where列的值吗?
  • @chris85 我有,现在没有出现错误,但信息没有更新,我忘了在问题中添加绑定,我现在添加它(这是使用 WHERE 产品.ProductName = :prodname)

标签: php mysql pdo


【解决方案1】:

您正在更新整个表格,您的更新中没有过滤器

$query4 = "
    UPDATE product SET CategoryID = :newCatId,  ProductName = :newProdName, ProductDescription = :newProdDesc, stockCount = :newStock
  ";

【讨论】:

    猜你喜欢
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多