【问题标题】:phpmyAdmin errors I don't understandphpmyAdmin 错误我不明白
【发布时间】:2016-10-09 12:17:42
【问题描述】:

今天我使用this tutorial 来创建一个使用phpmyAdmin 注册的论坛。在我到达这行代码之前一切顺利,我应该在 SQL 中输入。

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 - 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以了解在 'user_name']) 附近使用的正确语法。 “','”。 sha1($_POST['user_pass']) 。 “','”。 mys' 在第 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';
?>

我不知道它在说什么。请给我另一种方法来做到这一点!

【问题讨论】:

  • 您能否编辑您的代码以包含更多 PHP 代码?如果您将其直接粘贴到 phpMyAdmin 的 SQL 命令控制台中,那么它将不起作用,因为它严格运行 SQL,而不是 PHP。
  • 这些数字是多少?
  • @Rishi 它们是来自教程的编辑器窗口复制/粘贴的行号。
  • 我已经编辑了帖子
  • 我实际上添加了数字,以便您可以看到它正在谈论的行

标签: php mysql sql phpmyadmin


【解决方案1】:

您不能将 PHP 粘贴到 phpMyAdmin 中,phpMyAdmin 仅适用于 SQL。

您可以在phpMyAdmin下用硬编码数据测试SQL,例如:

INSERT INTO
    users(user_name, user_pass, user_email ,user_date, user_level)
VALUES('admin',
    sha1('password'),
   'admin@example.com',
   NOW(),   
   0);

如果您添加一个echo 语句来显示$sql 变量的内容,您可以获取该值并将其粘贴到phpMyAdmin 中。

另外,不要使用 mysql 扩展 - 它在 PHP 5.x 中已弃用并从 PHP 7 中删除。请改用 mysqli 或 PDO。

【讨论】:

  • 谢谢。你帮了大忙!
【解决方案2】:

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)";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多