Stevis2002 Posted November 23, 2010 Share Posted November 23, 2010 Hi again all, When i add text to my database through a form, it throws an error if i type 'wouldn't', but not if i type a word without an apostrophe. Is there a simple fix for this please peeps? Many Thanks Me@newbie.com.uk+vat Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/ Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 You should be sanitizing all incoming form data with mysql_real_escape_string() (if you're using MySQL, that is). Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138656 Share on other sites More sharing options...
AbraCadaver Posted November 23, 2010 Share Posted November 23, 2010 Assuming mysql use mysql_real_escape_string(). If not use the one for your DB. Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138657 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Thanks guys. So i looked up the usage of mysql_real_escape_string(), and changed my code, as below, but it has given me errors now. &_POST['customername'] = mysql_real_escape_string($_POST['customername']); &_POST['town'] = mysql_real_escape_string($_POST'[town']); &_POST['testimonial'] = mysql_real_escape_string($_POST['testimonial']); $sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images) VALUES ('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')"; Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138664 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 And what are those errors? Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138668 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Now change dto function check_input($value) { // Stripslashes if (get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote if not a number if (!is_numeric($value)) { $value = "'" . mysql_real_escape_string($value) . "'"; } return $value; } $customername = check_input($_POST['customername']); $town = check_input($_POST['town']); $testimonial = check_input($_POST['testimonial']); &_POST['customername'] = mysql_real_escape_string($customername); &_POST['town'] = mysql_real_escape_string($town); &_POST['testimonial'] = mysql_real_escape_string($testimonial); $sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images) VALUES ('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')"; Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138670 Share on other sites More sharing options...
ManiacDan Posted November 23, 2010 Share Posted November 23, 2010 You're not properly quoting your array indexes. You also added that fancy new function and you aren't using it. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138672 Share on other sites More sharing options...
BlueSkyIS Posted November 23, 2010 Share Posted November 23, 2010 &_POST???? Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138679 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 You're not properly quoting your array indexes. You also added that fancy new function and you aren't using it. -Dan Thanks Dan, but my script calls the function check_input when the info gets posted in the customer name, town and testimonial fields....doens't it? What do you mean about properly quoting the arrays? Should it be " instead of '? Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138683 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Ok, got rid of some code and now have $customername = check_input($_POST['customername']); $town = check_input($_POST['town']); $testimonial = check_input($_POST['testimonial']); $sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images) VALUES ('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')"; } but still have the error as stated before Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138685 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 // ASSUMES DB CONNECTION ALREADY ESTABLISHED . . . function check_input($value) { // Stripslashes if (get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote if not a number if (!is_numeric($value)) { $value = "'" . mysql_real_escape_string($value) . "'"; } return $value; } $_POST = array_map('check_input', $_POST); $sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images) VALUES ('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')"; Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138689 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Many Thanks for that mod! 1: still got the errors function check_input($value) { // Stripslashes if (get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote if not a number if (!is_numeric($value)) { $value = "'" . mysql_real_escape_string($value) . "'"; } return $value; } $_POST = array_map('check_input', $_POST); $sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images) VALUES ('$_POST[customername]','$_POST[town]','$_POST[testimonial]','$_POST[sort_order]','$imgname')"; 2. How do you get to know about all of these arrays? I look at php.net and others but i can never get anything to stick in my head. I can learn by being shown, but never out of a 'book' as such Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138693 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 Exactly what errors are they? Paste 'em in. Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138695 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Exactly what errors are they? Paste 'em in. Sorry mate.....still the same one caused by the ' Error: 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 'Steve'',''Brum'',''fsdhewty\'f'','12','uploaded_images/transparent.gif')' at line 3 EDIT: Sorry, not the ' causing errors now, but not sure what is Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138699 Share on other sites More sharing options...
ManiacDan Posted November 23, 2010 Share Posted November 23, 2010 Debugging tips: 1) Turn on error reporting. You had & instead of $, error reporting would have told you this. 2) Print your variables. This means printing $sql and looking at it. See where the problem is. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138705 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 I see what's going on here now. The function encloses string values in single quotes before returning them, so they're getting double quoted. Rewrite the query string as: $sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images) VALUES ({$_POST['customername']}, {$_POST['town']'}, {$_POST['testimonial']}, {$_POST['sort_order']}, $imgname)"; Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138706 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Thanks for help Pika. Now getting error saying..... Error: Unknown column 'uploaded_images' in 'field list' function check_input($value) { // Stripslashes if (get_magic_quotes_gpc()) { $value = stripslashes($value); } // Quote if not a number if (!is_numeric($value)) { $value = "'" . mysql_real_escape_string($value) . "'"; } return $value; } $_POST = array_map('check_input', $_POST); $sql="INSERT INTO testimonials (CustomerName, Town, Testimonial, SortOrder, Images) VALUES ({$_POST['customername']}, {$_POST['town']}, {$_POST['testimonial']}, {$_POST['sort_order']}, $imgname)"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "<p align=center><b>1 testimonial added</b></p>"; mysql_close($con); Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138714 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 Change this line: die('Error: ' . mysql_error()); To this: die("<br>Query: $sql<br>Error: " . mysql_error() . '<br>'); And see what the query string looks like now. EDIT: Fixed typo. Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138718 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Change this line: die('Error: ' . mysql_error()); To this: die("<br>Query: $sql<br>Error: " . mysql_error() '<br>'); And see what the query string looks like now. Just getting a blank page now Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138721 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 Nevermind, I removed one too many sets of quotes. Re-enclose $imgname in single quotes in the query string . . . '$imgname')"; Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138722 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Nevermind, I removed one too many sets of quotes. Re-enclose $imgname in single quotes in the query string . . . '$imgname')"; Still blank page mate Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138728 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 And fixed typo in post above with die() modification. (Stupiod fingerts) Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138731 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Cheers mate, All error messages gone and it says 1 record added, but there is no record added Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138735 Share on other sites More sharing options...
Stevis2002 Posted November 23, 2010 Author Share Posted November 23, 2010 Cheers mate, All error messages gone and it says 1 record added, but there is no record added No, sorry mate,.....working fine. my mistake, i had to change the max records from 10, to 100, as the record i put in was 11 lol Many, many thanks mate. U have slayed my oncoming headache lol Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138738 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 Well, I seem to have given myself one in the process. LOL. Shouldn't have been as complicated as I managed to make it, but what can ya do? Quote Link to comment https://forums.phpfreaks.com/topic/219624-what-must-be-a-simple-answerjust-not-for-me/#findComment-1138751 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.