00stuff Posted November 28, 2011 Share Posted November 28, 2011 Hi guys, I am trying to UPDATE some records on a mySQL database but can't seem to find out why it is not working. This is my code. <?php $latitude = $_POST['lat_location']; $longitude = $_POST['long_location']; $unique_ID = $_POST['unique_ID']; include('connect2.php'); $query = mysql_query("SELECT * FROM user_location WHERE unit = '$unique_ID'"); $numrows = mysql_num_rows($query); if ($numrows == 1) { $query2="UPDATE user_location SET lat = '$latitude', long = '$longitude' WHERE unit = '$unique_ID'"; mysql_query($query2); $test = "matches"; } else { mysql_query("INSERT INTO user_location VALUES ('','$unique_ID','$latitude','$longitude')"); $test = "not match"; } echo $test . "<br />"; echo $numrows; ?> The script receives the data via the POST method and assigns it to variables. Then I query the database for one of those variables and check how many results are found. If 0 results are found then a new record is created on the database, but if there is 1 record found then the record that is found has to be UPDATED. When the result is 0 the scripts creates the new record fine, but if the result is 1 it doesn't update. I just can't figure out why. Any help will be greatly appreciated. Thanks in advanced, Link to comment https://forums.phpfreaks.com/topic/251966-i-cant-update-records-on-mysql/ Share on other sites More sharing options...
00stuff Posted November 28, 2011 Author Share Posted November 28, 2011 I've been looking around and it seems like my code is correct. Does anyone see any mistakes? Link to comment https://forums.phpfreaks.com/topic/251966-i-cant-update-records-on-mysql/#findComment-1291880 Share on other sites More sharing options...
PFMaBiSmAd Posted November 28, 2011 Share Posted November 28, 2011 long is a reserved mysql keyword and is causing a sql error in your query. You should always have error checking logic in your code to test if queries work or fail with an error. To use long as a column name, you must enclose it in back-ticks `` every time you use it in a query or more simply, rename your column to something else. Link to comment https://forums.phpfreaks.com/topic/251966-i-cant-update-records-on-mysql/#findComment-1291882 Share on other sites More sharing options...
00stuff Posted November 28, 2011 Author Share Posted November 28, 2011 thank you very much, I will use error checking logic on my code from now on. Link to comment https://forums.phpfreaks.com/topic/251966-i-cant-update-records-on-mysql/#findComment-1291884 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.