作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<?php
//The victim's name, taken from a textfield
$victim = $_POST['username'];
//The ammount to be withdrawn from the user
$ammount = 2000;
if (isset($_POST['submit'])) {
mysql_query("UPDATE users SET money = money-$ammount WHERE username = '$victim'");
echo "You stole $ammount from $victim";
} else {
echo "Write the username of the person you're trying to steal money from..";
}
?>
到目前为止,这是我的代码,我正在尝试找出一种方法,使金额不再是静态的 2000 美元,但可以说是用户表中“金钱”行中金额的 15% ,其中用户名与变量 $victim 相同。
我想我需要一个名为 $victimsMoney 的变量,它将保存 mysql 表>'users'>column>'money' 字段中的金额,specifield 为用户名与 $victim 相同。
如何创建“money”列的变量,其中用户名是 $victim(用户输入的用户名)?
我自己的建议,但行不通:
$victimsMoney = mysql_query("SELECT money FROM users WHERE username = '$victim'");
最佳答案
$victimsMoney = mysql_query("SELECT money FROM users WHERE username = '".$victim."'");
while($fetch = mysqli_fetch_assoc($victimsMoney)){
echo $fetch['money'];
}
确保转义任何变量。否则 php 会将其解析为字符串:)
试试这个,将来阅读手册,这非常有帮助:)
mysqli_fetch 将被弃用。这是手册。
关于PHP 和 MySQL : Making a row in table to a variable specified by a $_POST ['username' ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28301104/
我是一名优秀的程序员,十分优秀!