简体   繁体   English

MySQL不会插入JSON

[英]MySQL won't Insert JSON

Something strange is happening here, normally i wouldn't have an issue, or maybe i am just tired. 这里发生了一些奇怪的事情,通常我不会有问题,或者也许我只是累了。 But MySQL won't accept my syntax to insert JSON into a table. 但是MySQL不接受将JSON插入表中的语法。

foreach ($newArray as $k => $v) {
    $query = "SELECT `id` FROM `achievements` WHERE `achieveID`=".$v['ID']." LIMIT 1";
    if ($result = mysqli_query($default, $query)) {
        $row_cnt = mysqli_num_rows($result);
        $row_cnt > 0 ? $parse = false : $parse = true;
        mysqli_free_result($result);
    }   
    $v['required'] == 'None' ? $req = $v['required'] : $req = json_encode($v['required'], JSON_FORCE_OBJECT);
    if($parse) {                        
        $result = mysqli_query($default, "INSERT INTO `achievements` VALUES (NULL, '".$v['cat']."', '".$v['ID']."', '".$v['title']."', '".$v['type']."', '".$v['factionD']."', '".$v['factionE']."', '".$v['text']."', '".$v['doneText']."', '".$v['points']."', '".$v['number']."', ".$req.", '".$v['rewards']."')") or die ('Unable to execute query. '. mysqli_error($default));
    }
}

My code inserts everything else in fine, and does other rows but when it gets to a row that required json data (ie when $v['required'] does not equal to none) it does not insert it and gives me an error. 我的代码很好地插入了其他所有内容,并执行其他行,但是当它到达需要json数据的行时(即$ v ['required']不等于none时),它不会插入并给我一个错误。

Unable to execute query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"0":"Into the Undertow","1":"Darkwater Abyss","2":"Primal Powers and Triton's Dr' at line 1

The JSON (from the json encode) JSON(来自json编码)

{"0":"Into the Undertow","1":"Darkwater Abyss","2":"Primal Powers and Triton's Dread","3":"Stirring Interstellar Waves"}

It seems like the double quotes are causing the issues? 似乎双引号引起了问题?

EDIT 编辑

Ive re-wrote my code! 我重写了我的代码! Thanks for the tips! 感谢您的提示!

$insert = $default->prepare("INSERT INTO achievements (catID, achieveID, achieveName, achieveType, factionDominion, factionExiles, achieveText, achieveCText, achievePoints, achieveNum, achieveReq, achieveRew) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$insert->bind_param("iissiissiiss", $cat, $id, $title, $type, $factionD, $factionE, $text, $doneText, $points, $number, $required, $rewards);

foreach ($newArray as $k => $v) {
    $query = "SELECT id FROM achievements WHERE achieveID=".$v['ID']." LIMIT 1";
    if ($result = $default->query($query)) {
        $row_cnt = $result->num_rows;
        $row_cnt > 0 ? $parse = false : $parse = true;
    }   
    $v['required'] == 'None' ? $req = $v['required'] : $req = json_encode($v['required'], JSON_FORCE_OBJECT);
    if($parse) {    
        $cat = $v['cat'];
        $id = $v['ID'];
        $title = $v['title'];
        $type = $v['type'];
        $factionD = $v['factionD'];
        $factionE = $v['factionE'];
        $text = $v['text'];
        $doneText = $v['doneText'];
        $points = $v['points'];
        $number = $v['number'];
        $required = $req;
        $rewards = $v['rewards'];   
        $insert->execute();
    }
}
$default->close();

And its working perfectly, it was really fast too, it added over 2000 rows in a few seconds! 而且它运行完美,速度也非常快,几秒钟内就增加了2000行! I am pretty sure there is a cleaner way to write it but first time using prepare! 我很确定有一种更简洁的编写方法,但是第一次使用prepare!

Your issue is here '".$v['number']."', ".$req.", 您的问题在这里'".$v['number']."', ".$req.",

The $req variable holds string and you need to wrap it in single quote as $req变量保存字符串,您需要将其用单引号括起来

'".$v['number']."', '".$req."',

The best option would be to use prepare statement to avoid these errors and sql injection. 最好的选择是使用prepare语句来避免这些错误和sql注入。

You need to escape the quotes and special characters. 您需要转义引号和特殊字符。 Check out the mysqli_real_escape_string 查看mysqli_real_escape_string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM