首页 > 解决方案 > PDO SQL 使用 " 和 ' 输入文本字段

问题描述

尝试同时使用 " 和 ' 进行插入查询

我有$description = addslashes($description);

但我仍然得到:

SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在 '27"","Product 附近使用的正确语法

$description"Capacity:26\",27\" and 700C Wheel Sizes"之后addslashes()

询问:

$handler->query('INSERT INTO `tm_products`
            (`title`, `description`, `member_name`, `category_path`) 
    VALUES ("'.$apiGetListing['Title'].'",
            "'.$description.'","'.$member_name.'",
            "'.$apiGetListing['CategoryPath'].'")');

我认为它试图在26\"“?

标签: phpmysqlsqlpdo

解决方案


你真的需要清理你的论点。您对 SQL 注入持开放态度。

    <?php
    $servername = "localhost";
    $username = "";
    $password = "";
    $dbname = "";

    $conn = new mysqli($servername, $username, $password, $dbname);

    $stmt = $conn->prepare("INSERT INTO tm_products(title, description, member_name, category_path) VALUES(?, ?, ?, ?))");
    $stmt->bind_param("ssss", $title, $description, $member_name, $category_path);

    $title= $apiGetListing['Title'];
    $description= $description;
    $member_name= $member_name;
    $category_path = $apiGetListing['CategoryPath'];

    $stmt->execute();

    $stmt->close();
    $conn->close();

推荐阅读