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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.