skip to Main Content

Here is my array for JSON I am trying to creating a JSON array with this code and then inserting the data in my sql but it is not working with inverted comma like "The text is with inverted comma’s" the ‘s is doing issues if when I insert same text without ‘s then it is working fine.

$custom_data = array();
$custom_data['seo_options'] = array("meta-title" => "The text is with inverted comma's", "meta-description" => "some data here", "meta-keywords" => "");

$sql2 = "INSERT INTO posts_meta (post_id,meta_key,meta_value) VALUES ('".$post_id."', 'custom_data', '".json_encode($custom_data)."')";
mysqli_query($conn2,$sql2);

3

Answers


  1. Chosen as BEST ANSWER

    Error description: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use.

    Its was the only issue I just upgrade the MariaDB version and the problem is solved.

    Thank you all for the support.


  2. First of all you should write what kind of issue you are experiencing (for example the error message if there is one).

    However, probably the problem is the ‘ symbol which breaks the query. Try to escape it (addslashes should work fine) before doing the query and check if it works.

    For example:

    $custom_data['seo_options']['meta-title'] = addslashes($custom_data['seo_options']['meta-title']);
    
    Login or Signup to reply.
  3. Try with addslashes, like this:

    mysqli_query($conn2,addslashes($sql2));
    

    or only on a part if the json/array.
    But I suggest you to go with prepared statement.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search