Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 注册表SQL语法错误_Php_Sql - Fatal编程技术网

Php 注册表SQL语法错误

Php 注册表SQL语法错误,php,sql,Php,Sql,我想做一份非常简单的登记表。但是当我得到一个错误,说我的SQL语法有一个错误 但它说我在写的那行有问题: $stmt->execute(); 我猜这是因为它不能执行SQL问题 我的代码: <?php require 'anslut.php'; $submit = filter_input(INPUT_POST, "submit", FILTER_SANITIZE_SPECIAL_CHARS); $output = ""; if (isset($submit)) { $n

我想做一份非常简单的登记表。但是当我得到一个错误,说我的SQL语法有一个错误

但它说我在写的那行有问题:

$stmt->execute();
我猜这是因为它不能执行SQL问题

我的代码:

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

在这里注册
你的名字:
用户名:
密码:
登记

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

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

查看您的SQL查询:

INSERT INTO users(username, password, namn) VALUES :username, :password, :name
                          spelling? -----^         ^------ parentheses ------^
拼写一大部分是猜测,但至少括号是个好主意。也许你是这个意思

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

另外,作为补充说明,永远不要以纯文本形式存储用户密码。这是对用户数据非常不负责任的处理。PHP有很多方法可以帮助正确隐藏单向散列后面的用户密码。(对于较早的PHP版本也是如此。)

将变量用单引号括起来,因为这些变量是字符串,不能直接插入数据库中。e、 g

INSERT INTO talbe_name (col1,col2) VALUES ('string1','string2');
还可以在php代码中的字符串内的变量周围绘制
{}
,例如

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


请使用PHP处理密码安全问题。如果您使用的PHP版本低于5.5,您可以使用
password\u hash()
。我认为INSERT-INTO-users(username、password、namn)namn应该是名称您在查询中拼写错误的namn
VALUES:username、:password、:name
应该是
值(:username、:password、:name)
。“namn”是瑞典语中“name”的意思,因此可能是数据库中正确的字段名。它只是一个输入错误,请关闭它。请删除逗号(Column1,Column2),:)双引号字符串中的标量变量不需要
{}
。在使用数组和对象属性时使用此选项。PHP变量与问题中的SQL查询有什么关系?你真的建议OP不要使用预处理语句,而是使用用户输入值的字符串插值吗?我告诉过你php语法,当你用php生成sql查询时,然后使用{}@ZahidIqbal:你所做的是建议OP用你的SQL注入漏洞替换一个完全准备好的语句。你的代码不仅不能回答这个问题,而且还推荐了一些糟糕的做法。很抱歉拼写错误,我是瑞典人,“namn”是瑞典人的名字。。我不小心在我的数据库表中放了“namn”,opsie@Addeuz当前位置这就是为什么它主要是猜测。这似乎是一个合理的拼写,取决于语言,而非英语语言经常在软件系统中与英语混合,因为工具可能非常以英语为中心。不过,很高兴我能在SQL语法方面提供帮助!