如果数据存在则更新数据库

时间:2016-02-03 01:39:28

标签: php mysql pdo

我有一个包含两列的数据库。产品的名称和数量。 我还有两种形式,一种是为用户添加新产品的输入,另一种是为他添加数量的输入。 另一种形式是在产品销售时更新数量。

我面临的问题是,当用户使用第二种形式更新特定产品的数量时,数据库中的所有产品数量也会被减去。希望我能正确解释我的问题。

完整代码:

<?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>

1 个答案:

答案 0 :(得分:0)

您似乎正在使用更新更新整个表格 - 尝试更新更新查询以包含brand_id

&#13;
&#13;
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();
}
&#13;
&#13;
&#13;