PHP:限制MySQL输入项

时间:2018-08-15 08:35:26

标签: php mysql

因此,我制作了一个PHP页面,该页面将允许用户选择产品,直到其计数达到特定最大值为止。

表1

----------------------------
| productId | count | max |
----------------------------
| Pid1      |   0   |  10 |
----------------------------
| Pid2      |   0   |  15 |
----------------------------
| Pid3      |   0   |  15 |
----------------------------

每当用户选择产品时,相应的计数就会增加。并将userid和productid添加到另一个表。此外,用户可以再次登录并更改其订单。

表2

----------------------
| userId | productId |
----------------------
|  U01   |   Pid1    |
----------------------
|  U02   |   Pid3    |
----------------------

这是我使用的代码。

$userId = $_POST['userId'];
$productId = $_POST['productId'];
$query = "SELECT * FROM productDetails WHERE productId = '$productId' ";
$result = mysqli_query($conn,$query);
if (mysqli_num_rows($result) < 1) { 
    //Taking new orders here
    $query2 = "SELECT count,max from Table1 where productId = '$productId' ";
    $result2 = mysqli_query($conn, $query2);
    if (mysqli_num_rows($result2) < 1) {
        echo "<script>alert('no result');</script>";
    }
    else{
        $row = mysqli_fetch_array($result2);
        if ($row[0] < $row[1]) {
            $query3 = "INSERT INTO Table2(`userId`,`productId`) VALUES('$userId','$productId') ";
            $query4 = "UPDATE Table1 SET count = count + 1 WHERE productId = '$productId'";
            $result3 = mysqli_query($conn, $query3);
            if (mysqli_error($conn) == "") {
                mysqli_query($conn, $query4);
                echo '<script>alert("Your response has been saved.");</script>';
            }
        }
        else{
            echo "<script>alert('Sorry, the selected workshop is at maximum capacity.');</script>";
        }
    }
}
else{       
    //Modifying existing orders here.
    $row = mysqli_fetch_array($result);
    $oldProductId = $row[1];
    $query5 = "UPDATE Table1 SET count = count - 1 WHERE productId = '$oldProductId' ";
    $result5 = mysqli_query($conn,$query5);
    if (mysqli_error($conn) == "") {
        $query6 = "UPDATE Table1 SET count = count + 1 WHERE productId = '$productId' ";
        $result6 = mysqli_query($conn,$query6);
        if (mysqli_error($conn) == "") {
            $query7 = "UPDATE Table2 SET productId = '$productId' WHERE userId = '$userId' ";
            $result7 = mysqli_query($conn,$query7);
            if (mysqli_error($conn) == "") {
                echo '<script>alert("Your response has been saved.");</script>';
            }
            else{
                //error
                $query5c = "UPDATE Table1 SET count = count + 1 WHERE productId = '$oldProductId' ";
                $result5c = mysqli_query($conn,$query5c);
                $query6b = "UPDATE Table2 SET count = count - 1 WHERE productId = '$productId' ";
                $result6b = mysqli_query($conn,$query6b);
                echo '<script>alert("Sorry, some error occured. ERR_CODE 256.");</script>';
            }  
        }
        else{
            //error
            $query5b = "UPDATE Table2 SET count = count + 1 WHERE productId = '$oldProductId' ";
            $result5b = mysqli_query($conn,$query5b);
            echo '<script>alert("Sorry, some error occured. ERR_CODE 257.");</script>';
        }

    }
    else{
        //error
        echo '<script>alert("Sorry, some error occured. ERR_CODE 258.");</script>';
    }


}

问题是我发现两个Table1计数都比其最大值大1。我不知道这是不是一个并发问题,还是有人弄乱了它。

我也知道我的代码和技术可以改进。如果有人可以指出并告诉我原因,那将是巨大的帮助。例如:我现在认为我不应该使用count列,而应该从第二个表中计数productId。同意吗?

任何改进我的代码的建议都将不胜感激。谢谢。

0 个答案:

没有答案