Jump to content

Can't Add +1 to mySQL database field


shawhey

Recommended Posts

Hi, so I have a fantasy hockey website and am trying to create an application that will allow me to add points to players in the database by clicking a button. I've tried everything I can think of, but it still doesn't work. Here is a link to the application: http://moscowhockey.1free.ws/rosterupdate.php

 

Here is the piece of code for 'Add Goal':

 

 if (isset($_POST['AddGoal'])) { 

$lastname = $_POST['LastName'];

$sql = mysql_query("SELECT * FROM roster WHERE LastName = '$lastname'");

$sql[Goals]+=1;

// now we insert it into the database

mysql_query("UPDATE roster SET Goals=$sql[Goals]  WHERE LastName = '$lastname'");


}
?> 

Link to comment
https://forums.phpfreaks.com/topic/219567-cant-add-1-to-mysql-database-field/
Share on other sites

You don't need a SELECT query, and even if you did mysql_query returns a result resource, not an array.

 

if (isset($_POST['AddGoal'])) { 
    mysql_query("UPDATE roster SET Goals=Goals+1  WHERE LastName = '$lastname'");
}

 

ps: Do not use variables directly within queries like that, you are asking for trouble. Make sure you sanitize all user inputted data with mysql_real_escape_string.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.