Jump to content

I can't UPDATE records on mySQL.


00stuff

Recommended Posts

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

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.

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.