I am currently trying to upload a form to our database using mySQL, but the problem is when a user enters an apostrophe in the text field it breaks and gives this error:
Error updating database: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'm interested in football ''' This is a test', resume
= '/var/www/html/' at line 6
Here is the code we are using to strip characters that are not permitted in the database, with exceptions granted to - , and ' :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$( 'textarea[name=studentTalking]').change(function() {
var txt=$('textarea[name=studentTalking]').val();
var txt=$('textarea[name=studentTalking]').val();
txt = txt.replace(/[^-,'A-Za-z0-9\s\,\-\(\)\&\/]/gi, '');
txt = txt.replace(/\&/,'and');
$('textarea[name=studentTalking]').val(txt);
});
</script>
What can I add to this code to manually escape the apostrophe so it may be submitted to the database? I have seen others solving this problem by using mysql_real_escape_string, but am unsure where to put it. If you need more code to help me let me know, and I will try to edit this post with an updated code.