[英]SQL UPDATE for specific user session
我可以基於會話ID向數據庫添加特定數據或從數據庫刪除特定數據,但是,當我嘗試為該特定用戶更新數據時,出現錯誤。 該重復的密鑰聲明無法正常工作,因為密鑰已設置為記錄ID與股票的代碼。 任何解決方案的想法都可以解決,因為現在當一個人嘗試兩次添加相同的股票時,他們被允許這樣做,因此這會在以后引發問題。 不知道我現在是否會使用該Update語句來進行正確的處理,而不是更新部分,因此,感謝您的反饋/幫助。 謝謝!
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
如果要防止用戶兩次添加相同的庫存,可以通過創建UNIQUE
索引來做到這一點:
ALTER TABLE `portfolio2` ADD UNIQUE `unique_idx`(`memberid`, `stocks_symbol`);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.