Wrox Programmer Forums
|
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143
This is the forum to discuss the Wrox book Beginning PHP 6, Apache, MySQL 6 Web Development by Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz; ISBN: 9780470391143
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 12th, 2010, 09:39 PM
Authorized User
 
Join Date: Sep 2010
Posts: 20
Thanks: 0
Thanked 2 Times in 2 Posts
Default Chapter 13 - Modify User

If I login as administrator and try to modify a user's account I get this 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 'WHERE user_id = 3' at line 5

Now the code it's referring to is this, and I can't see anything wrong with it:

Code:
case 'Modify Account':
		 $user_id = (isset($_POST['user_id'])) ? $_POST['user_id'] : ' ';
		 $email = (isset($_POST['email'])) ? $_POST['email'] : ' ';
		 $name = (isset($_POST['name'])) ? $_POST['name'] : ' ';
		 $access_level = (isset($_POST['access_level'])) ? $_POST['access_level'] : ' ';
		 if (!empty($user_id) && !empty($name) && !empty($email) && !empty($access_level)) {
		     $sql = 'UPDATE cms_users SET
	             email = "' . mysql_real_escape_string($email, $db) . '",
		     name = "' . mysql_real_escape_string($name, $db) . '",
		     access_level = "' . mysql_real_escape_string($access_level, $db) . '",
		     WHERE user_id = ' . $user_id;
		     mysql_query($sql, $db) or die(mysql_error($db));
		 }
		 redirect('cms_admin.php');
		 break;
Anyone else have this problem?

Last edited by bopjo1; October 12th, 2010 at 09:41 PM..
The Following User Says Thank You to bopjo1 For This Useful Post:
Mohan Ted (June 11th, 2013)
 
Old October 13th, 2010, 05:30 PM
Authorized User
 
Join Date: Sep 2010
Posts: 20
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Ok, I got it to work using this code:

Code:
case 'Modify Account':
		 if (isset($_POST['user_id']) && isset($_POST['email']) && isset($_POST['access_level']) && isset($_POST['name'])) {
			 $user_id = mysql_real_escape_string($_POST['user_id']);
			 $email = mysql_real_escape_string($_POST['email']);
			 $name = mysql_real_escape_string($_POST['name']);
			 $access_level = mysql_real_escape_string($_POST['access_level']);		 
		 }
         if (!empty($user_id) && !empty($name) && !empty($email) && !empty($access_level)) {
            $sql = "UPDATE cms_users SET
                    email = '$email',
                    name = '$name',
                    access_level = $access_level
                WHERE
                    user_id = $user_id";
            mysql_query($sql, $db) or die(mysql_error($db));
         }
         redirect('cms_admin.php');
         break;
One obvious wrong thing in the code from the book is this line:

Code:
if (!empty($user_id) && !empty($name) && !empty($email) && !empty($access_level) && !empty($user_id)) {
If you'll notice, !empty($user_id) is in there twice. At the beginning and the end. One them should be deleted. Still, this does not fix the broken functionality of the SQL query.

Notice in my version of the query how I use double quotes to start the query, and single quotes for the variables with string values. Variables with numerical values ($access_level & $user_id) don't get quoted in SQL queries. Also, I moved all the mysql_real_escape_string's out of the query.

I hope this helps somebody.

Last edited by bopjo1; October 13th, 2010 at 05:44 PM..
 
Old February 22nd, 2013, 01:14 PM
Registered User
 
Join Date: Feb 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ok thanks

Its Works, Thanks ,You save my time





Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete User Ch. 13 djlfreak BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 0 June 3rd, 2010 09:57 AM
Chapter 13: Error in user.php pherank BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 July 4th, 2008 09:09 PM
How i can modify the User profle ssomchai BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 11 March 29th, 2008 07:43 AM
Chapter 13 bwoll BOOK: Beginning Access 2003 VBA 1 June 7th, 2007 03:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.