1

My code is:

$description = $_POST['description'];
$description = htmlspecialchars($description);

I use it to insert some description into a table:

$insertBillIndexQuery = "INSERT INTO $billIndexTableName (type, exp_category, shopping_date, shop, description, total_amount, paid, due, mode_of_payment) VALUES ('Expense', '$exp_category', '$billDate', '$shop', '$description', '$total_amount', '$paid', '$due', '$modeOfPayment')";

This works fine usually. However, when I type a special character such as a single quote, the system breaks, and I get an Error Querying Database error. I'm sure that the single quotes are causing the problem. Am I using htmlspecialchars wrong?

Somenath Sinha
  • 1,174
  • 3
  • 16
  • 35

1 Answers1

1

You need to do the conversion using ENT_HTML401 for converting ' into '. According to the manual:

' (single quote)
' (for ENT_HTML401) or ' (for ENT_XML1, ENT_XHTML or ENT_HTML5), but only when ENT_QUOTES is set

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252