php - '@dfaf.fa' 附近的 MySql 和 PHP 语法错误 '

标签 php mysql

我在写电子邮件时为 findID 创建了一个简单的网站。

HTML 代码

<form action="test.php" method="post">
        <input type="text" name="user_id_test" id="user_id_test">
        <br>
        <br>
        <input type="submit" value="Find ID">
</form>

PHP 代码

<?php

        //include database
        include 'include/db.inc';

        $emailUser = $_POST['user_id_test'];

        $findNewID = mysqli_query($connessione,"SELECT user_id FROM user_tmplt WHERE user_mail = $emailUser");

        if ($findNewID != "") {
            var_dump($findNewID);
            echo "$findNewID";
        } else {
            echo "Errore: " . $findNewID . "<br>" . mysqli_error($connessione) ."<br>";
        }

mysqli_error($connessione);

?>

我尝试查找电子邮件的 ID:dfaf@dfaf.fa(它在我的数据库中,ID 为 13),但出现此错误 您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“@dfaf.fa”附近使用的正确语法

最佳答案

您应该使用准备好的语句来避免此类错误并避免 SQL Inyection :

$stmt = mysqli_prepare($connessione,"SELECT user_id FROM user_tmplt WHERE user_mail = ?");
if ( !$stmt ) { someErrorHandlingHere(); }

mysqli_stmt_bind_param($stmt, "s", $emailUser);
// same here: mysqli_stmt_bind_param may fail -> returns false

mysqli_stmt_execute($stmt);
// same here: mysqli_stmt_execute may fail -> returns false

mysqli_stmt_bind_result($stmt, $userId);
// and so on and on: error handling

mysqli_stmt_fetch($stmt);

echo "The user id es: {$userId}";

关于php - '@dfaf.fa' 附近的 MySql 和 PHP 语法错误 ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28317312/

相关文章:

php - Ajax POST 请求返回 int 而不是字符串

php - 如果在表单中检查了一个或多个项目,如何将 MySQL 字段递增 1?

php - 复杂 SQL 查询的默认下拉选项

php - Apache 中的 Laravel 获取 header 值

php - 使用 Wordpress 建立数据库连接时出错

php - 不要在 X 元素中加载内容

mysql - 无法使用命令行安装 craft3。测试数据库凭据...失败:

mysql - 查找不仅仅是特定 SKU 的订单

在查询中使用子项计数时 Hibernate 生成的 MySQLSyntaxErrorException

php - 如何对 UTF-8 字符串数组进行排序?