【发布时间】:2018-09-03 18:11:26
【问题描述】:
我可以根据会话 ID 在特定用户的数据库中添加和删除我想要/从数据库中删除的数据,但是,当我尝试更新该特定用户的数据时,我会收到错误消息。该重复键语句也无法正常工作,因为该键设置为记录 ID 与股票代码。知道如何解决这个问题,因为现在当一个人尝试两次添加相同的库存时,他们被允许这样做,因此这会导致以后出现问题。不确定我现在是否使用该 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);
}
表结构: 投资组合2
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
【问题讨论】:
-
错误信息是什么? (将消息附加到问题的底部,不是作为评论)
-
实际上,我设法摆脱了错误,更新似乎通过了,但它没有更新任何东西。在数据库中,无论我输入什么,价格都会变为 0,并且数量保持不变。我从 var_dump($sql)- string(84) "UPDATE portfolio2 SET price=3 and quantity=3 WHERE stock_symbol='f' and memberid=22" 股票更新成功!
-
SET语句中的UPDATE子句不正确。SET的字段应该用逗号分隔,而不是和,所以:SET price=$pri[$x] and quantity=$q[$x]... 应该是:SET price=$pri[$x], quantity=$q[$x] -
这可能会在表格级别涵盖,但您需要将表格结构附加到问题的底部。 (不要添加为其他评论)您可以通过运行
SHOW CREATE TABLE portfolio2查询来获取此信息。将该输出复制/粘贴到问题的末尾。 -
看起来@Jacobian 已经提供了我的下一个回复作为答案。 (继续接受,如果它适合你)
标签: php mysql database session