2016-06-09 5 views
2

오늘은 phpmyAdmin을 사용하여 가입하여 포럼을 만드는 것에 대해 this tutorial을 사용하고있었습니다. 내가 SQL에 입력해야하는이 코드 행에 도달 할 때까지 좋았습니다.phpmyAdmin errors 잘 모르겠 음

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

을 그리고 이것은

<?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'; 
?> 

내가 무슨 생각이 나는 signup.php에 사용되는 PHP 자사의 에 대해 말하다. 이 작업을 수행 할 수있는 다른 방법을 알려주세요!

+0

디버깅, 맞춤법 실수인가? 직접 phpMyAdmin의 SQL 명령 콘솔에 붙여 넣는다면 PHP가 아닌 SQL 만 엄격하게 실행하기 때문에 작동하지 않습니다. –

+0

이 숫자는 무엇입니까? – C2486

+0

@Rishi 튜토리얼의 편집기 창 복사/붙여 넣기의 줄 번호입니다. –

답변

3

phpMyAdmin에 PHP를 붙여 넣을 수 없으며 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); 

당신이 $sql 변수의 내용을 표시하는 echo 문을 추가하는 경우, 해당 값 및 붙여 넣기를 취할 수 있습니다 phpMyAdmin에 넣으십시오.

또한 mysql 확장자로 작업하지 마십시오. PHP 5.x에서는 더 이상 사용되지 않으며 PHP 7에서는 제거됩니다. 대신 mysqli 또는 PDO를 사용하십시오.

+0

감사. 당신은 많은 도움이되었습니다! –

0

SQL 출력이 PHP 코드의 일부 등을 포함하는 당신이 당신의 코드를 편집하십시오 수 있습니다

$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)"; 
관련 문제