gpt4 book ai didi

PHP 和 MySQL : Making a row in table to a variable specified by a $_POST ['username' ]

转载 作者:行者123 更新时间:2023-11-29 23:05:39 25 4
gpt4 key购买 nike

<?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 将被弃用。这是手册。

http://php.net/manual/en/function.mysql-fetch-assoc.php

关于PHP 和 MySQL : Making a row in table to a variable specified by a $_POST ['username' ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28301104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com