【问题标题】:apostrophes or ampersand causing error in MySQL [duplicate]撇号或&符号导致MySQL中的错误[重复]
【发布时间】:2019-10-09 15:58:05
【问题描述】:

当我尝试将以下数据输入数据库时​​,这些字符会产生问题:& (\u0026) 和 ' (\u0027)

{  
    "Id":5507542,"Name":"Port \u0026 Company® - Youth 50/50 Cotton/Poly T-Shirt",
    "Description":"Cover you kids in the best threads by Port \u0026 Company®. This youth T-shirt is something they\u0027ll always run back to! It\u0027s made of 5.5 oz., 50% cotton / 50% poly (with up to 5% recycled polyester from plastic bottles). 
    They\u0027ll stay visible in the Safety Green and Safety Orange colors because they\u0027re compliant with ANSI/ISEA 107 high visibility standards for background color. Imprint your company name to this top and let the clients come to you!",
    "ShortDescription":"50/50% Cotton/Poly T-Shirt.",
}

PHP 代码:

<?php


    $host="localhost";

    $user="root";

    $password="";

    $db="db";


     $connect =  mysqli_connect($host,$user,$password,$db) or die('Database Not Connected. Please Fix the Issue! ' . mysqli_error());


     $id = $content['Id'];  

     $name = $content['Name'];

     $description = $content['Description'];

     $shortDescription = $content['ShortDescription'];


    $query = "INSERT INTO result_tab(id,name,description, shortDescription) VALUES('$id', '$name', '$description', '$shortDescription')";

     if(!mysqli_query($connect,$query))
    {
        die('Error : Query Not Executed. Please Fix the Issue!  ' . mysqli_error($connect));
    }
     else{
            echo "Data Inserted Successully!!!";
     }
?> 

错误:

Query Not Executed. Please Fix the Issue! You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use
near 'll always run back to! It's made of 5.5 oz., 50% cotton / 50% poly (with up to 5' at line 1

【问题讨论】:

    标签: php mysql json unicode


    【解决方案1】:

    不要使用这种格式:\u0026

    请务必使用“绑定”或以其他方式转义任何放入 INSERT(和其他 SQL 语句)的字符串。

    【讨论】:

    • 现在使用... $abc = mysqli_real_escape_string($connect,$xyz);
    猜你喜欢
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 2015-01-25
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多