Home > OS >  Facing some issues to inserting data in sql, Some text is with inverted commas like text's
Facing some issues to inserting data in sql, Some text is with inverted commas like text's

Time:11-05

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);

CodePudding user response:

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']);

CodePudding user response:

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.

CodePudding user response:

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.

  • Related