dongzheng4556 2018-03-25 14:59
浏览 78
已采纳

特定用户会话的SQL UPDATE

I can add and delete the data I want to/from a db for a specific user, based on the session ID however, when I try to update the data for that specific user, I get an error. That duplicate key statement doesn't work properly either because the key is set to record ID vs stock's symbol. Any idea how to resolve that because now when a person tries to add the same stock twice, they are allowed to do so and so this leads to issues later on. Not sure if I'm going about this the correct way now with that Update statement instead, for the update part, so I'd appreciate any feedback/help. Thanks!

Code for 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);
}

Table structure: 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条回答 默认 最新

  • dongyi1215 2018-03-25 17:29
    关注

    If you want to prevent users from adding the same stock twice, you can do it by creating a UNIQUE index:

    ALTER TABLE `portfolio2` ADD UNIQUE `unique_idx`(`memberid`, `stocks_symbol`);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了