2017-05-06 88 views
-2

模糊标题的道歉。 我目前有一个编辑表单,可以在其中选择产品,并以可编辑的形式显示细节。不幸的是,编辑产品表中的每个产品时,都会编辑而不是仅选择产品。要选择我使用的产品,其中产品名称= productnameinput等等等等(代码如下所示)查询更新所有记录而不是与输入相匹配的记录?

数据库: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(); 

    } 
+2

你需要一个'where'在'update'条款。 – chris85

+0

现在就试试,谢谢。将更新问题,如果不成功,但我认为你是对的 – Xander

+0

@ chris85很确定你是对的,但我仍然收到错误,我已更新我的问题。我应该包括$ post产品名称等吗? – Xander

回答

-1

您的更新查询需要隐式定义WHERE以进行更新。

当前,您只是更新每个条目的列,因为没有定义特定条目。

事情是这样的:

UPDATE product 
SET categoryid = :newCatId, 
     productname = :newProdName, 
     productdescription = :newProdDesc, 
     stockcount = :newStock 
WHERE id = :product_id_to_edit; 
-1

要更新整个表,有没有在您的更新过滤

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