pdosql输入带有“and”的文本字段

yfjy0ee7  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(281)

尝试同时使用“and”进行insert查询
我有 $description = addslashes($description); 但我还是得到了:
sqlstate[42000]:语法错误或访问冲突:1064您的sql语法有错误;请查看与您的mysql服务器版本对应的手册,以获取在“27”、“产品”附近使用的正确语法 $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\" 是什么?

x9ybnkn6

x9ybnkn61#

你真的需要理清你的论点。您对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();
2j4z5cfb

2j4z5cfb2#

尝试使用参数查询。

$handler->query('INSERT INTO tm_products(title, description, member_name, category_path) VALUES
    (:title, :description, :member_name, :cat_path)', array('title'=>$apiGetListing['Title'],'description'=>$description,'member_name'=>$member_name,'cat_path'=>$apiGetListing['CategoryPath']))

或者您可以将变量放入查询中,如下所示:

"VALUES ('$apiGetListing[Title]','$description','$member_name','$apiGetListing[CategoryPath]'));"

请注意,如果这样做,则不必将“”放入数组键。

eivgtgni

eivgtgni3#

一些建议:
1-使用heredoc语法以提高可读性:
例子:

$query = <<<SQL
    INSERT INTO `tm_products`
    (`title`, `description`, `member_name`, `category_path`) VALUES 
    (
        "{$apiGetListing['Title']}", 
        "$description", 
        "$member_name", 
        "{$apiGetListing['CategoryPath']}"
    )
SQL;

对数组使用{}
2-出于安全原因使用pdo param绑定

$query = "SELECT * FROM table WHERE something = ?";
$stmt = $db->prepare($query);
$stmt->execute(array($id));

相关问题