Ron
Ron

Reputation: 391

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.

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.

Upvotes: 1

Views: 217

Answers (2)

DrinkJavaCodeJava
DrinkJavaCodeJava

Reputation: 808

You want to enter your sql query like this

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

Also any database function that begins with mysql should be replaced with mysqli. This is because PHP is phasing out functions beginning with mysql in the next edition.

Upvotes: 1

Ugo Méda
Ugo Méda

Reputation: 1215

Several mistakes here :

  • You're not sanitizing your inputs, please read about SQL Injections
  • You're not checking the output of your mysql_query. The query with SET Level 'Level+1' is invalid, you forgot a = and remove quotes
  • $action == 'rankup', not =
  • Please consider using PDO for new projects, it's a way better interface than mysql_ functions.

Upvotes: 2

Related Questions