MySQL查询的问题 - INSERT INTO

时间:2013-08-01 19:54:51

标签: php mysql

所以,我将发布以下代码。代码下面是我提出问题的地方。

if (!empty($_SESSION['username']) && !empty($_SESSION['password']))  
{  

$server=mysql_real_escape_string($_POST['server']);
$teamname=mysql_real_escape_string($_POST['teamname']);
$creator=$_SESSION['username'];
$verify = mysql_real_escape_string($_POST['verify']);
$date = date("F j, Y, g:i a");

if (!empty($teamname)) {
//  if ($verify == "wookie" ||
//  $verify == "Wookie" ||
//  $verify == "WOOKIE") 
//  {
        $sql="INSERT INTO rated_teams (server, name, creator, created, players, win, loss)
                            VALUES ('$server', '$teamname', '$creator','$date', '', '', '')";

        if (mysql_query($sql,$con))
          {
            echo "<p>Added ". $teamname . " on " . $server . " by " . $creator . " on " . $date ." <br /><a href='myprofile.php'>Return to Profile</a></p>";
          }
         else
          {
            echo $sql . "<br />";
            echo "<br /><h1>Error</h1>";  
            echo "<p><a href='myprofile.php'>Sorry, your team registration has failed. Please go back and try again.</a></p>
            <br />" . $teamname . " on " . $server . " by " . $creator . " on " . $date;
          }
    //} else { echo "That isn't how you spell Wookie!"; }
} else { echo "Team Name is empty, <a href='myprofile.php'>go back and give yourself a Team Name</a>"; }
} else { echo "You must be <a href='login.php'>logged in</a>!"; }

这个问题是“if(mysql_query($ sql,$ con))”行直接进入ELSE。我假设问题在于我的$ sql,但我无法确定它在哪里。另一双眼睛真的会有所帮助。非常感谢!

1 个答案:

答案 0 :(得分:0)

要使用mysql_query()跟踪错误,您应该使用mysql_error()。这是一个例子,受到PHP mysql_error() doc

之一的启发
    $sql="INSERT INTO rated_teams (server, name, creator, created, players, win, loss)
                        VALUES ('$server', '$teamname', '$creator','$date', '', '', '')";

    mysql_query($sql,$con);
     if (mysql_errno()) {
         $error = "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$sql\n<br>";

        // Your stuff
        echo $sql . "<br />";
        echo "<br /><h1>Error</h1>";  
        echo "<p><a href='myprofile.php'>Sorry, your team registration has failed. Please go back and try again.</a></p>
        <br />" . $teamname . " on " . $server . " by " . $creator . " on " . $date;
      }
     else {
           echo "<p>Added ". $teamname . " on " . $server . " by " . $creator . " on " . $date ." <br /><a href='myprofile.php'>Return to Profile</a></p>"; 
      }

此外,您应该使用PDOmysqli,因为自PHP 5.x后不推荐mysql_*