1

my query is like

$insert_query = "insert into post (post_title) values ('$post_title')";

If my post_title = Ronald's bio-data, I would not be able to insert the tuple because of the special character ' in my post_title.

How should solve this problem? Thank you in advance!

Xiahao Wang
  • 45
  • 1
  • 3
  • 10

2 Answers2

3

Use Prepared Statements to escape parameters automatically in your queries

$stmt = $dbh->prepare("insert into post (post_title) values (?)";
$stmt->bindParam(1, $post_title);
$stmt->execute();
juergen d
  • 201,996
  • 37
  • 293
  • 362
0

Consider using the $mysqli->real_escape_string(String) function.

$post_title = $mysqli->real_escape_string($post_title);
$insert_query = "insert into post (post_title) values ('$post_title')";

See here for more information.

Note that using variables in SQL Strings always emits a potential vulnerability due to SQL Injection attacks. See e.g. this post for alternatives.

Community
  • 1
  • 1
sebastian_oe
  • 7,186
  • 2
  • 18
  • 20