2016-06-09 138 views
2

今天我使用this tutorial關於使用phpmyAdmin註冊創建論壇。直到我達到這一行代碼,我本來應該輸入到SQL中,這一切都很順利。phpmyAdmin錯誤我不明白

1. INSERT INTO 
2.  users(user_name, user_pass, user_email ,user_date, user_level) 
3. VALUES('" . mysql_real_escape_string($_POST['user_name']) . "', 
4.  '" . sha1($_POST['user_pass']) . "', 
5.  '" . mysql_real_escape_string($_POST['user_email']) . "', 
6.  NOW(), 
7.  0); 

所以我進入它在phpMyAdmin和我得到這個錯誤:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'user_name']) . "', '" . sha1($_POST['user_pass']) . "', '" . mys' at line 3

這是我用來signup.php的PHP

<?php 
//signup.php 
include 'connect.php'; 
include 'header.php'; 

echo '<h3>Sign up</h3>'; 

if($_SERVER['REQUEST_METHOD'] != 'POST') 
{ 
    /*the form hasn't been posted yet, display it 
     note that the action="" will cause the form to post to the same page it is on */ 
    echo '<form method="post" action=""> 
     Username: <input type="text" name="user_name" /> 
     Password: <input type="password" name="user_pass"> 
     Password again: <input type="password" name="user_pass_check"> 
     E-mail: <input type="email" name="user_email"> 
     <input type="submit" value="Add category" /> 
    </form>'; 
} 
else 
{ 
    /* so, the form has been posted, we'll process the data in three steps: 
     1. Check the data 
     2. Let the user refill the wrong fields (if necessary) 
     3. Save the data 
    */ 
    $errors = array(); /* declare the array for later use */ 

    if(isset($_POST['user_name'])) 
    { 
     //the user name exists 
     if(!ctype_alnum($_POST['user_name'])) 
     { 
      $errors[] = 'The username can only contain letters and digits.'; 
     } 
     if(strlen($_POST['user_name']) > 30) 
     { 
      $errors[] = 'The username cannot be longer than 30 characters.'; 
     } 
    } 
    else 
    { 
     $errors[] = 'The username field must not be empty.'; 
    } 


    if(isset($_POST['user_pass'])) 
    { 
     if($_POST['user_pass'] != $_POST['user_pass_check']) 
     { 
      $errors[] = 'The two passwords did not match.'; 
     } 
    } 
    else 
    { 
     $errors[] = 'The password field cannot be empty.'; 
    } 

    if(!empty($errors)) /*check for an empty array, if there are errors, they're in this array (note the ! operator)*/ 
    { 
     echo 'Uh-oh.. a couple of fields are not filled in correctly..'; 
     echo '<ul>'; 
     foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */ 
     { 
      echo '<li>' . $value . '</li>'; /* this generates a nice error list */ 
     } 
     echo '</ul>'; 
    } 
    else 
    { 
     //the form has been posted without, so save it 
     //notice the use of mysql_real_escape_string, keep everything safe! 
     //also notice the sha1 function which hashes the password 
     $sql = "INSERT INTO 
        users(user_name, user_pass, user_email ,user_date, user_level) 
       VALUES('" . mysql_real_escape_string($_POST['user_name']) . "', 
         '" . sha1($_POST['user_pass']) . "', 
         '" . mysql_real_escape_string($_POST['user_email']) . "', 
         NOW(), 
         0)"; 

     $result = mysql_query($sql); 
     if(!$result) 
     { 
      //something went wrong, display the error 
      echo 'Something went wrong while registering. Please try again later.'; 
      //echo mysql_error(); //debugging purposes, uncomment when needed 
     } 
     else 
     { 
      echo 'Successfully registered. You can now <a href="signin.php">sign in</a> and start posting! :-)'; 
     } 
    } 
} 

include 'footer.php'; 
?> 

我不知道它是什麼談論。請給我一個替代方法來做到這一點!

+0

你可以請編輯你的代碼,包括一些更多的PHP代碼?如果您直接將其粘貼到phpMyAdmin的SQL命令控制檯中,那麼它將無法正常工作,因爲它僅嚴格運行SQL,而不是PHP。 –

+0

這些數字是什麼? – C2486

+0

@Rishi他們是從編輯器窗口複製/粘貼教程中的行號。 –

回答

3

您不能將PHP粘貼到phpMyAdmin中,phpMyAdmin只能與SQL一起使用。

您可以測試出與phpMyAdmin下硬編碼數據的SQL,例如:

INSERT INTO 
    users(user_name, user_pass, user_email ,user_date, user_level) 
VALUES('admin', 
    sha1('password'), 
    '[email protected]', 
    NOW(), 
    0); 

如果添加echo聲明顯示$sql變量的內容,你可以把這個值和粘貼它進入phpMyAdmin。

另外,不要使用mysql擴展名 - 它在PHP 5.x中被棄用,並從PHP 7中刪除。請改用mysqli或PDO。

+0

謝謝。你幫了很多! –

0

SQL是拼寫錯誤,輸出調試

$sql = "INSERT INTO 
    users(user_name, user_pass, user_email ,user_date, user_level) 
    VALUES('" . mysql_real_escape_string($_POST['user_name']) . "', 
    '" . sha1($_POST['user_pass']) . "', 
    '" . mysql_real_escape_string($_POST['user_email']) . "', 
    NOW(), 
    0)";