简体   繁体   English

使用php接受多个输入以更新mysql数据库

[英]Accept Multiple inputs with php to update a mysql database

I'm working on a small project: a small number crunching game. 我正在做一个小项目:一个小数字的运算游戏。

I want to have a php file that can accept inputs and interpret them into specified database updates. 我想有一个php文件,可以接受输入并将它们解释为指定的数据库更新。

Here is what I have so far. 这是我到目前为止所拥有的。 It doesn't seem to be working for me. 它似乎对我不起作用。

$name = $_GET['n'];
$action = $_GET['a'];
$result = mysql_query("SELECT * FROM players WHERE Username ='".$name."'");

while($row = mysql_fetch_array($result)) {
  if ($action = "rankup") mysql_query("UPDATE players SET Level 'Level+1' WHERE Username='".$name."'");
}

mysql_close($con);

I'm not getting any errors, but its not working, and all the database connections are fine. 我没有收到任何错误,但它没有工作,所有数据库连接都没问题。

I dont know what the problem is. 我不知道问题是什么。

Several mistakes here : 这里有几个错误:

  • You're not sanitizing your inputs, please read about SQL Injections 您没有对输入进行消毒,请阅读有关SQL注入的信息
  • You're not checking the output of your mysql_query . 你没有检查你的mysql_query的输出。 The query with SET Level 'Level+1' is invalid, you forgot a = and remove quotes SET Level 'Level+1'的查询无效,您忘记了=并删除了引号
  • $action == 'rankup' , not = $action == 'rankup' ,而不是=
  • Please consider using PDO for new projects, it's a way better interface than mysql_ functions. 请考虑将PDO用于新项目,这是一种比mysql_功能更好的接口。

You want to enter your sql query like this 您想要像这样输入您的SQL查询

'UPDATE players SET Level= (Level+1)  WHERE Username='.$name.'

Also any database function that begins with mysql should be replaced with mysqli. 任何以mysql开头的数据库函数都应该用mysqli替换。 This is because PHP is phasing out functions beginning with mysql in the next edition. 这是因为PHP将在下一版中逐步淘汰以mysql开头的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM