Why this simple query doesn't work ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Why this simple query doesn't work ??

Post by diseman »

Hi Everyone,

I need some help on this please. The query below will not work unless I physically add a record into the database with my email address FIRST. It's driving me crazy. Doesn't this query say IF the $requestor exists UPDATE and if he/she doesn't the INSERT into the database?? $requestor, in this case, is my email address. I have printed the $requestor and do see my email address at the top of the screen. What am I don't wrong?

Code: Select all

if ($_POST['save'])	{

	$query = "SELECT * FROM banking WHERE username = '".$requestor."' " ;

	$result = mysql_query($query) or die("query: $query<br>" . mysql_error());

if (mysql_num_rows($result) >= 1) 	{

	$sql = "UPDATE banking SET ".$fields1." WHERE username = '".$requestor."' " ;

	} else {

	$sql = "INSERT banking SET ".$fields1." WHERE username = '".$requestor."' " ;

}
					
	mysql_query($sql,$con) or die("query: $query<br>" . mysql_error());
					
}
The SAVE button name is 'save'

Like I said, everything works perfectly as long as I pre-populate the db with my email address in the 'banking' table first...

Thank you in advance for any help.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Why this simple query doesn't work ??

Post by Celauran »

At a glance, everything looks fine. What error(s) are you getting?
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: Why this simple query doesn't work ??

Post by diseman »

Good Morning and thanks for your help...

Here's the message I'm getting:

[text]query: SELECT * FROM banking WHERE username = 'myemailaddress@gmail.com'
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 username = 'myemailaddress@gmail.com'' at line 77[/text]

I changed the email address for obvious reasons.

Usually the line number is a good starting point, but this time it's not much help as line 77 is just:

[text]$credit_account8 = mysql_real_escape_string($_POST['credit_account8']);[/text]

I'm going to keep looking. Any ideas?
Post Reply