Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-17-2012, 11:16 PM   PM User | #1
thelung
New Coder

 
Join Date: Sep 2010
Posts: 34
Thanks: 22
Thanked 0 Times in 0 Posts
thelung is an unknown quantity at this point
I cannot escape the strings!!

For some reason, no matter what I do, I cannot seem to escape this string that is causing error code when I try to insert record into databse.

Firstly, I must mention that the form which displays the problem variable echoes it's options from a database into a select form. Like this:

PHP Code:
                <select name='group'>
                    <option value='public'>Public</option>
            <?php   include '../globalFunctions.php';
                    
con("netBase"); //connect to mysql and select database
                        
$resultmysql_query("SELECT * FROM groups");
                        while(
$row mysql_fetch_array($result)){
                          echo 
"<option value='";
                          echo 
mysql_real_escape_string($row['name'])."'>";
                          echo 
$row['name']."</option>";
                         }
                    
                    
?>
                </select>

I have the variable escaped when it is declared. During the course of trying to debug this, I've even went as far as stripping the tags as well as escaping the string.

PHP Code:
$group mysql_real_escape_string(strip_tags($_POST['group']));
$address strip_tags($_POST['address']);
$address2 strip_tags($_POST['address2']);
$city strip_tags($_POST['city']);
$state strip_tags($_POST['state']);
$zip strip_tags($_POST['zip']);
$date date("Y-m-d"); 
I have a typical insertion query

PHP Code:
$query="INSERT INTO users (id, fname, lname, email, username, password, title, status, group, address, address2, city, state, zip, date)VALUES ('', '".$fname."', '".$lname."', '".$email."', '".$username."', '".$password."', '".$title."', '".$status."', '".$group."', '".$address."', '".$address2."', '".$city."', '".$state."', '".$zip."', '".$date."')"
But, I keep getting this error message instead of the "Inserted, way to go" message that is supposed to display after the fields have been successfully inserted into the database.

Code:
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 'group, address, address2, city, state, zip, date)VALUES ('', 'Harry', 'Harrison'' at line 1
I'm just assuming that this is a string issue. Considering that the "group" names which are echoed out into the form select boxes have apostrophes in them. Like "Andy's", or "Joseph's", etc.
I've even tried escaping the string before it is echoed into the form's select box, then in turn it being re-escaped as it is being posted from the form. Still no luck. Can anybody provide any insight, I'm T-totally stumped here.

Last edited by thelung; 02-18-2012 at 12:20 AM.. Reason: used wrong code/php tags
thelung is offline   Reply With Quote
Old 02-18-2012, 01:35 AM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
mysql_real_escape_string is the old way of escaping data to be jumbled in with SQL in a query request (from before PDO and mysqli made it easy to keep the SQL and data separate using prepare/bind in place of query). There is no reason to use it in generating the HTML as you do in your first piece of code - you'd use html_specialchars() to escape code you want to output as part of the HTML.

With the other code you have there you are only escaping one field. Presumably one of the others can also contain characters that need to be escaped and that is what is causing the error. See if escaping the other fields as well makes a difference or alternatively use PDO to do away with the need to escape data being inserted into the database completely.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
thelung (02-19-2012)
Old 02-18-2012, 01:47 PM   PM User | #3
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
The error lies at
Code:
$query="INSERT INTO users (id, fname, lname, email, username, password, title, status, group,
GROUP is a reserved word of mysql, so you can't use them in the queries as such as the name of a table/field. You may enclose it using backticks (`), though it's better to avoid using them. See http://dev.mysql.com/doc/refman/5.5/...ved-words.html
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
thelung (02-19-2012)
Old 02-19-2012, 02:36 AM   PM User | #4
thelung
New Coder

 
Join Date: Sep 2010
Posts: 34
Thanks: 22
Thanked 0 Times in 0 Posts
thelung is an unknown quantity at this point
Thanks abduraooft!
That was it! I can't believe all the modifications I've done to this code and it ends up being a reserved word. But, I've learned that one simple mistake can result in a whole lot of improvement in some code. Thanks guys!!
thelung is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:30 PM.


Advertisement
Log in to turn off these ads.