如何在mysql命令中放置变量?PHP

eivnm1vs  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(218)

这个问题在这里已经有答案了

如何在mysql语句中包含php变量(6个答案)
11个月前关门了。
php有点小问题。我得到了一个简单的计算系统,其中有字段balance、trtype(长版本事务类型)、tramt(特定事务的金额)和finalbalance(操作后的余额)。它显示为一个表,它有一个用户拥有的所有事务的列表。所以我做了一个表格,“雇员”(只有我),可以填写表格,如日期转换类型等。为了避免任何类型的误判,我不希望“雇员”把finalbalance字段放在自己身上。我要电脑来做。最终余额总是和交易后发生的一样。当新的事务发生时,它会创建一个新的数字并保留旧的数字。这是一种混乱,我99%肯定这不是最好的办法,但会感谢任何帮助。

$AS = $CONNECT->prepare('UPDATE acc balance = CONCAT(balance + ?,"<br>"), WHERE username = ? '))
$AS->bind_param('ss', $_POST['balance'],$_POST['ac']);
$AS->execute();
$AS->store_result();
$AS->close();
$AS2 = $CONNECT->prepare('SELECT finalbalance FROM accounts WHERE username = ?');
$AS2->bind_param('s', $_POST['acc']);
$AS2->execute();
$AS2->bind_result($fnb);
$AS2->store_result();
$AS3 = $CONNECT->prepare('UPDATE accounts SET finalbalance = CONCAT(finalbalance+.$fnb.,"<br>") WHERE username = ?');
$AS3->bind_param('s', $_POST['acc']);
$AS3->execute();
$AS3->store_result();
$AS3->close();

我编写了这段代码,但这给了我一个抛出的错误堆栈跟踪{main},这通常意味着我把mysql命令搞砸了,所以我猜它没有得到变量。谢谢你的阅读和帮助。

3qpi33ja

3qpi33ja1#

也许这会有帮助。


# query modified, removed `concat` and kept data as numeric rather than a string.

# The `update` statement needs the `SET` keyword.

$AS = $CONNECT->prepare('UPDATE acc set balance = balance + ? WHERE username = ?');
$AS->bind_param('ss', $_POST['balance'], $_POST['ac'] );
$AS->execute();
$AS->close();

# No recordset is generated, no need to `store` the result

# 

$AS2 = $CONNECT->prepare('SELECT finalbalance FROM accounts WHERE username = ?');
$AS2->bind_param('s', $_POST['acc'] );
$AS2->execute();
$AS2->bind_result($fnb);
$AS2->store_result();
$AS2->fetch(); # need to fetch the recordset!

# again - removed the `concat` and maintain as numeric. Bind the variable to a placeholder rather than embedding

$AS3 = $CONNECT->prepare('UPDATE accounts SET finalbalance = finalbalance + ? WHERE username = ?');
$AS3->bind_param('ss', $fnb, $_POST['acc']);
$AS3->execute();
$AS3->close();

# No recordset is generated, no need to `store` the result
pgpifvop

pgpifvop2#

试试这个:

$AS3 = $CONNECT->prepare('UPDATE accounts SET finalbalance = CONCAT(finalbalance+".$fnb.","<br>") WHERE username = ?');

相关问题