简体   繁体   English

注册表格SQL语法错误

[英]Registration form SQL syntax error

I'm trying to make a very simple registration form. 我正在尝试制作一个非常简单的注册表格。 But when I get an error that says that I have an error in my SQL syntax. 但是,当我收到一条错误消息,表明我的SQL语法错误。

But it says that I have the problem on the line where I wrote: 但是它说我写的那行有问题:

$stmt->execute();

I guess it's because it can not execute the SQL question? 我猜是因为它无法执行SQL问题?

My code: 我的代码:

<?php
require 'anslut.php';

$submit = filter_input(INPUT_POST, "submit", FILTER_SANITIZE_SPECIAL_CHARS);

$output = "";
if (isset($submit)) {
    $name = filter_input(INPUT_POST, "name", FILTER_SANITIZE_SPECIAL_CHARS);
    $username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_SPECIAL_CHARS);
    $password = filter_input(INPUT_POST, "password", FILTER_SANITIZE_SPECIAL_CHARS);


if ($name == "" || $username == "" || $password == "") {
    $output .= "All fields must be entered";
} else {
    $sql = "INSERT INTO users(username, password, namn) VALUES :username, :password, :name";
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(":username", $username, PDO::PARAM_STR);
    $stmt->bindParam(":password", $password, PDO::PARAM_STR);
    $stmt->bindParam(":name", $name, PDO::PARAM_STR);
    $stmt->execute();

    $output .= "Registration successful";
    $output .= "<a href='index.php'>Follow the link to log in</a>";
    }
}
?>
<html>
    <head>
        <title>Register here</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <form method="POST">
            <table>
                <tr>
                    <td><label>Your name:</label></td>
                    <td><input type="text" name="name" placeholder="Your name"></td>
                </tr>
                <tr>
                    <td><label>Username:</label></td>
                    <td><input type="text" name="username" placeholder="Username"></td>
                </tr>
                <tr>
                    <td><label>Password:</label></td>
                    <td><input type="password" name="password" placeholder="Password"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><button type="submit" name="submit">Register</button></td>
                </tr>
            </table>
        </form>
        <?php print($output); ?>
    </body>

</html>

Looking at your SQL query: 查看您的SQL查询:

INSERT INTO users(username, password, namn) VALUES :username, :password, :name
                          spelling? -----^         ^------ parentheses ------^

The spelling one is mostly a guess, but at the very least the parentheses are a good idea. 拼写大都是猜测,但至少括号是个好主意。 So maybe you meant this?: 所以也许你是这个意思?:

INSERT INTO users(username, password, name) VALUES (:username, :password, :name)

Also, as a side note, never store user passwords in plain text . 另外,请注意, 请勿将用户密码存储为纯文本格式 This is very irresponsible handling of user data. 这是对用户数据的非常不负责任的处理。 PHP has a lot of built-in functionality to assist in properly obscuring user passwords behind a one-way hash. PHP具有许多内置功能 ,可帮助适当隐藏单向哈希后面的用户密码。 (As well as a compatibility pack for older PHP versions.) (以及旧PHP版本的兼容性包 。)

语法错误是因为您需要在值周围加上括号:

INSERT INTO MyTable (Column1, Column2) VALUES (Value1, Value2)

enclose the variables in single quotes because these are string and strings cannot be directly inserted in database. 将变量用单引号引起来,因为它们是字符串,并且字符串不能直接插入数据库中。 eg 例如

INSERT INTO talbe_name (col1,col2) VALUES ('string1','string2');

also draw {} around variables inside the string in php code eg 也用php代码在字符串内的变量周围绘制{}

 <?php
     $str = "Value={$variable}";
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM