特定用户会话的SQL UPDATE

时间:2018-03-25 14:59:20

标签: php mysql database session

我可以根据会话ID为特定用户添加和删除数据库中的数据,但是当我尝试更新该特定用户的数据时,我收到错误消息。由于密钥设置为记录ID与股票的符号,因此该重复密钥语句无法正常工作。任何想法如何解决这个问题,因为现在当一个人试图两次添加相同的股票时,他们被允许这样做,所以这会导致以后出现问题。我不确定我现在是否正确使用该更新语句来处理此更新部分,因此我非常感谢任何反馈/帮助。谢谢!

INSERT&的代码UPDATE:

// When the Buy button is pressed, specific action will be triggered according to the input given.
    if(isset($_POST['Buy']))
    { 
        // Checking whether first line is completely filled.
        if(empty($_POST['sym1']) or empty($_POST['pri1']) or empty($_POST['q1']))
        {
            ?><h2><center>To add values, please fill out at least the first row completely.</center></h2><?php
        // die();
    }
    // Loop through the form to allow for an appropriate db update.
    for($x=1;$x<=4;$x++)
    {
        $sym = [];
        $pri = [];
        $q = [];
        // If input provided is correct then update the db.
        if (!empty($_POST['sym'.$x]) and !empty($_POST['pri'.$x]) and !empty($_POST['q'.$x])) 
        {
            $sym[$x] = $_POST['sym'.$x];
            $pri[$x] = $_POST['pri'.$x];
            $q[$x] = $_POST['q'.$x];
            $memberid = $_SESSION['memberID'];
            $sql = "INSERT INTO portfolio2 
                (stocks_symbol, price, quantity, memberID)
                VALUES ('$sym[$x]', $pri[$x], $q[$x], $memberid)
                ON DUPLICATE KEY UPDATE
                price=$pri[$x], quantity=$q[$x]";

            // Check if values are added successfully and if so, then display a message to the user.
            if(mysqli_query($conn, $sql))
            {
                ?><h2><center><?php
                echo "Stocks added successfully!";
                ?></h2><center><?php
            }
            else
            {
                ?><h2><center><?php
                echo "Error- Stocks weren't added!". "<br>". $sql.
                "<br>". $conn->error;
                ?></h2><center><?php
            }
        }
    }
    mysqli_close($conn);
}
// UPDATE 
    elseif(isset($_POST['Update']))
{
    // Check to see whether the stock symbol has been provided
    if(empty($_POST['sym1']))
    {
        ?><h2><center>To update values, please enter the symbol of the stock to be updated.</center></h2><?php
        // die();
    }

    // Loop through the form to allow for an appropriate db update.
    for($x=1;$x<=4;$x++)
    {
        $sym = [];
        $pri = [];
        $q = [];

        // When all three values to be updated are given and are correct, update the db accordingly.
        if (!empty($_POST['sym'.$x]) and !empty($_POST['pri'.$x]) and !empty($_POST['q'.$x])) 
        {
            $sym[$x] = $_POST['sym'.$x];
            $pri[$x] = $_POST['pri'.$x];
            $q[$x] = $_POST['q'.$x];
            $memberid = $_SESSION['memberID'];
            $sql = "UPDATE portfolio2 SET price=$pri[$x] and quantity=$q[$x] WHERE stocks_symbol='$sym[$x]' and memberid=$memberid";

            // Check to see whether the values are updated successfully and if so, then display a message to the user.
            if(mysqli_query($conn, $sql))
            {
                ?><h2><center><?php
                echo "Stocks updated successfully!";
                ?></h2><center><?php
            }
            else
            {
                ?><h2><center><?php
                echo "Error- Couldn't update stocks from the table". "<br>". $sql.
                "<br>". $conn->error;
                ?></h2><center><?php
            }
        }   
    }
    mysqli_close($conn);
}

表格结构: portfolio2

CREATE TABLE `portfolio2` (
 `stockID` int(11) NOT NULL AUTO_INCREMENT,
 `stocks_symbol` varchar(30) NOT NULL,
 `price` decimal(30,2) DEFAULT NULL,
 `quantity` int(30) DEFAULT NULL,
 `memberid` int(11) NOT NULL,
 PRIMARY KEY (`stockID`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1

1 个答案:

答案 0 :(得分:1)

如果您想阻止用户两次添加相同的库存,可以通过创建./索引来实现:

UNIQUE