0

I'm trying to update a row in my database but it's not running.

Below is the current script I have that gets values from an ajax call. I've checked the call and it is sending the right information. know that I do have the connection values at the top of the script but did not include them here.

// Opens a connection to a MySQL server
$connection = mysql_connect($host, $username, $password);
   if (!$connection) {
 die("Not connected : " . mysql_error());
}   // Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
  if (!$db_selected) {
die("Can\'t use db : " . mysql_error());
}
// Store INFORMATION

    $name = $_POST['name'];
    $website = $_POST['website'];
    $address = $_POST['address'];
    $request_url = $base_url . "&q=" . urlencode($address);
    $csv = file_get_contents($request_url) or die("url not loading");
    $csvSplit = split(",", $csv);
    $status = $csvSplit[0];
    $lat = $csvSplit[2];
    $lng = $csvSplit[3];
    $state = $_POST['state'];
    $ident = $_POST['ident'];
 $query = "UPDATE  markers SET  name =  '".$name."', website =  '".$website."',address =  '".$address."',lat =  '".$lat."',lng =  '".$lng."',state =  '".$state."'WHERE id = '" . $ident . "'";
  mysql_query($query) or die(mysql_error());

?>

I'm still very new at this, can someone explain why it's not working?

14
  • 1
    is there any mysql error showing ? Commented Mar 24, 2012 at 0:01
  • 1
    I don't see any error here, but you should escape all your input with mysql_real_escape_string Commented Mar 24, 2012 at 0:05
  • 1
    What happens when you call the php page directly (not using AJAX, but using a POST from a form?)? Does that work? Commented Mar 24, 2012 at 0:06
  • 1
    If you don't know how you can see the output of your script, you can check it using Firebug on the "network" panel and filter it by "xhr". On Ajax call, you'll see a new request here, click on the "+" button & response : voila, that's the output of your script Commented Mar 24, 2012 at 0:18
  • 1
    @poerg place echo $query; underneath where you define $query = and copy / paste the output. Thanks. Commented Mar 24, 2012 at 1:11

3 Answers 3

2

You should put the field names like name, website and so on in this quotation marks: ` And you should only use one space between the words and maybe add a space before the WHERE.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried adding ` and field name and adding the space, but it's still not updating.
thank you, and Thanks Ryan Kempt! Between the two of you I was able to find the problem.
1

you need to add space before 'where'

Comments

0

If your id field is a number, you shouldn't be quoting the value you're comparing to, here:

WHERE id = '" . $ident . "'";

Just do this:

WHERE id = " . $ident;

One often overlooked test to to have your script echo out the actual query that's being run, and then take that query string and run it directly against the database server - in your case, in phpMyAdmin against your MySQL server.

1 Comment

now I get this error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.