0

So I get this error:

Problem updating record. MySQL 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 'WHERE KittenID = '2''

But then in my code:

<?php
if(isset($_POST['Modify']))
{	
            $connection = mysql_connect("Deleted the login info");

            // Check connection
            if (!$connection)
	    {
	        echo "Connection failed: " . mysql_connect_error();
	    }
            else
            {
                //select a database
                $dbName="Katz";
                $db_selected = mysql_select_db($dbName, $connection); 
        
                //confirm connection to database
                if (!$db_selected)
                {
                    die ('Can\'t use $dbName : ' . mysql_error());
                }
                else
                {

                $KittenID = $_POST["KittenID"];
				$KittenAge = $_POST['KittenAge'];
				$Name = $_POST['Name'];
				$Email = $_POST['Email'];
				$Gender = $_POST['Gender'];
				$Personality = $_POST['Personality'];
				$Activity = $_POST['Activity'];
				$Comments = $_POST['Comments'];

                $query = "UPDATE Kittenzz 
                          SET KittenID = '$KittenID', 
                              KittenAge = '$KittenAge',
							  Name = '$Name',
							  Email = '$Email',
							  Gender = '$Gender',
							  Personality = '$Personality',
							  Activity = '$Activity',
							  Comments = '$Comments',
                          WHERE KittenID = '$KittenID'";

                $res = mysql_query($query);
                   
                if ($res)
                {
                   echo "<p>Record Updated<p>";
                }   
					else
					{
					   echo "Problem updating record. MySQL Error: " . mysql_error();
					}
				}
            }
            mysql_close($connection);
}
        ?>

It makes no sense, I've read those lines of code for an hour, I cannot see the problem. It should run. Can anyone lend me fresh eyes?

3
  • remove comma after $Comments on your update query Commented Mar 24, 2015 at 5:01
  • Try your query in phpmyadmin if possible Commented Mar 24, 2015 at 5:02
  • You should never use user input directly in sql queries without at least using an escaping function first, eg mysql_real_escape_string(). Ideally you should not be using mysql_* functions any more for new code as they have been deprecated for years and should instead look at using either mysqli_* functions or PDO Commented Mar 24, 2015 at 8:28

2 Answers 2

1

Remove the comma near '$comments'

 $query = "UPDATE Kittenzz 
                          SET KittenID = '$KittenID', 
                              KittenAge = '$KittenAge',
                              Name = '$Name',
                              Email = '$Email',
                              Gender = '$Gender',
                              Personality = '$Personality',
                              Activity = '$Activity',
                              Comments = '$Comments'
                          WHERE KittenID = '$KittenID'";
Sign up to request clarification or add additional context in comments.

Comments

0

it may possible that in connection username and password in required. like this :-

$connection = mysql_connect("localhost","username","password");

1 Comment

It is, but i deleted that info, in fact the problem was a comma missplaced. Problem solved, see answer up there.

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.