I have MySQL table I want to insert data into and wanted to escape quotes and double quotes my current SQL query looks like this
/*a function to escape strings*/
function escape_string($string){
global $connection;
return mysqli_real_escape_string($connection, trim($string));
}
$my_post = [
"post_title" => escape_string("Title Here"),
"post_content" => escape_string("content")
"post_excerpt" => substr(escape_string("Summary"),0, 155),
"post_status" => "publish",
"post_name" => normal_slugify(escape_string($Title)),
"post_date" => date("Y-m-d h:i:s"),
"post_date_gmt" => date("Y-m-d h:i:s"),
"post_modified" => date("Y-m-d h:i:s"),
"post_modified_gmt" => date("Y-m-d h:i:s")
];
$s_columns = implode(", ",array_keys($post_array));
$s_values = "'". implode("', '", array_values($post_array)) . "'";
$query = query("INSERT INTO siri_posts($s_columns) VALUES ($s_values)");
Some content have quotes and double quotes so I need to escape them before I insert them to database right?
I tried Using mysqli_real_escape_string()
but it's not working and I am getting error when I try to enter title with quotes into the database also the content is user generated so I want I function to escape string before inserting them to database.
I went around the forum and tried a lot of functions but some are depricated
and some are not working.