更新MySQL错误但代码没有错误?

时间:2015-03-24 04:54:12

标签: php mysql sql

所以我收到了这个错误:

更新记录时遇到问题。 MySQL错误:您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以便在'WHERE KittenID ='2'附近使用正确的语法

然后在我的代码中:

<?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);
}
        ?>

没有意义,我已经阅读了一小时的代码行,我看不出问题。它应该运行。谁能借给我新鲜的眼睛?

2 个答案:

答案 0 :(得分:1)

删除'$comments'附近的逗号

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

答案 1 :(得分:0)

可能需要连接用户名密码。 像这样: -

$ connection = mysql_connect(&#34; localhost&#34;,&#34;用户名&#34;,&#34;密码&#34;);