2016-02-03 81 views
2

我有一個包含兩列的數據庫。產品的名稱和數量。 我也有兩種形式,一種是爲用戶添加新產品的輸入,另一種是爲他添加數量的輸入。 另一種形式是更新產品銷售時的數量。如果數據存在,更新數據庫

我面臨的問題是,當用戶使用第二個表單來更新特定產品的數量時,數據庫內的所有產品數量也會被扣除。希望我解釋我的問題是正確的。

全碼:

<?php 


class TableRows extends RecursiveIteratorIterator { 
    function __construct($it) { 
     parent::__construct($it, self::LEAVES_ONLY); 
    } 
} 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 

try { 
    $conn = new PDO("mysql:host=$servername;dbname=program", $username, $password); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    echo "<script>alert('Connected successfully');</script>"; 
    } 
catch(PDOException $e) 
    { 
    echo "Connection failed: " . $e->getMessage(); 
    } 

if (isset($_POST['sub']) == 1) { 
    $id2 = $_POST['id2']; 
    $qty2 = $_POST['qty2']; 
    $to = "xxxxxx"; 
    $subject = "New sale report"; 
    $header = "From The shop"; 

    $stmt = $conn->prepare("SELECT brand_id FROM brands WHERE brand_id = :id2"); 
    $stmt->bindParam(':id2', $id2); 
    $stmt->execute(); 
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
     $r = $v; 
     $s = $r - $qty2; 
     if ($stmt->rowCount() === 1) { 
      echo $s; 
      $sql_new = "UPDATE brands SET brand_qty='$s'"; 
      $conn->exec($sql_new); 
     } 
     echo "<script>alert('New record successfully updated.')</script>"; 
    } 
    $stmt_m = $conn->prepare("SELECT brand_id FROM brands"); 
    $stmt_m->execute(); 
    $results = $stmt_m->setFetchMode(PDO::FETCH_ASSOC); 
    foreach (new TableRows(new RecursiveArrayIterator($stmt_m->fetchAll())) as $t=>$d) { 
     $body = "Item sold: ". $d ."\n\n Quantity sold: ". $s ." "; 
    } 
    //mail($to, $subject, $body, $header); 
} 

$id = $_POST['id']; 
$qty = $_POST['qty']; 

if (isset($_POST['submit']) == 1) { 
    $stmt_mt = $conn->prepare("SELECT brand_id FROM brands WHERE brand_id = :id"); 
    $stmt_mt->bindParam(':id', $id); 
    $stmt_mt->execute(); 


    if($stmt_mt->rowCount() === 1) { 
     echo "<script>alert('exists in database.')</script>"; 
    } else { 
     echo "<script>alert('adding hoodie to database.')</script>"; 
     $sql = "INSERT INTO brands (brand_id, brand_qty) VALUES ('$id', '$qty')"; 
     $conn->exec($sql);  
     echo "<script>alert('New record inserted succesfully.')</script>"; 
    } 
} 



?> 

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
<form method="post"> 
    <input type="text" placeHolder="Enter ID" name="id" /> 
    <input type="number" placeHolder="Quantity" name="qty" /> 
    <input name="submit" type="submit" value="submit" /> 
</form> 

<form method="post"> 
    <input type="text" placeHolder="Enter ID" name="id2" /> 
    <input type="number" placeHolder="Number of sales" name="qty2" /> 
    <input name="sub" type="submit" value="submit" /> 
</form> 

</body> 
</html> 

回答

0

看起來像你更新你的更新整個表 - 嘗試改變更新查詢包括brand_id

if ($stmt->rowCount() === 1) { 
 
    echo $s; 
 
    $stmt2 = $conn->prepare("UPDATE brands SET brand_qty='$s' WHERE brand_id = :id2"); 
 
    $stmt2->bindParam(':id2', $id2); 
 
    $stmt2->execute(); 
 
}