MySQL error when inserting data containing apostrophes (single quotes)?

前端 未结 10 1101
不知归路
不知归路 2020-11-30 13:59

When I an insert query contains a quote (e.g. Kellog\'s), it fails to insert a record.

ERROR MSG:

You have an error in your SQL s

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 14:33

    You should pass the variable or data inside mysql_real_escape_string(trim($val)), where $val is the data on which you are getting an error.

    If you enter the text, i.e., "I love Kellog's", we have a ' in the string so it will break the query. To avoid it you need to store data in a variable like this $val = "I love Kellog's".

    Now, this should work:

    $goodval = mysql_real_escape_string(trim($val));
    

提交回复
热议问题