TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-24-2009, 04:55 AM   #1 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default Check that values aren't the same

I am playing around (locally). I want to make up a sign up form for this cms-like thing I am building. I want to make sure that the user login and e-mail aren't in the db already.

My SQL Statement is:
Code:
INSERT INTO tbluser 
 (user_loginname, 
  user_pass,
  user_firstName,
  user_lastName,
  user_email)
 VALUES(
  $userLogin,
  $userPass,
  $userFirst,
  $userLast,
  $userEmail)
 (SELECT user_loginname, 
  user_email
  FROM tbluser
  WHERE NOT ( 
  user_loginname = $userLogin AND 
  user_email = $userEmail))
I get the following error:
Quote:
Could not add: 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 '@yahoo.com)(SELECT user_loginname, user_email FROM tbluserWHERE NOT ( user_login' at line 1
Any hints?
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 06-24-2009, 07:28 AM   #2 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default

I think (off the top of my head) I've always done this with two seperate statements. Not sure if it's the best way but it does work. Something like:

PHP Code:
$r mysqli_query("SELECT * FROM db WHERE username='$u' LIMIT 1");
$rows mysqli_num_rows($r);
if(
$rows == 0) { // 0 means user doesn't already exist
//Put the new user details into the database

__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
Old 06-24-2009, 10:35 AM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Why are you wanting to know this information? Essentially, my reason for asking is that you could check this information on INSERT, if you wanted to. Therefore saving you a SELECT statement.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-24-2009, 05:46 PM   #4 (permalink)
The Contributor
 
Join Date: Jun 2009
Location: Seattle, WA
Posts: 76
Thanks: 1
rguy84 is on a distinguished road
Default

@Wildhoney, can you show me an example? All the examples I could find have the SELECT statement.
__________________
Ryan | Blog | Twitter
Send a message via AIM to rguy84 Send a message via MSN to rguy84 Send a message via Yahoo to rguy84 Send a message via Skype™ to rguy84
rguy84 is offline  
Reply With Quote
Old 06-24-2009, 09:38 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You'll need to configure the indexes on your table. So the username and email address fields would become unique indexes. When you then attempt to INSERT into the table, an error will be thrown. This can be retrieved using the MySQL error number, and then handed accordingly.

It saves on a SELECT statement to check if it is unique or not.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 06-25-2009, 01:08 AM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Put spaces between every line in the string, I noticed that the error said "tbluserWHERE NOT", it should be "tbluser WHERE NOT" with a space between tbluser and WHERE.

I've been writing queries all day, that is a common error I hit unless I make sure there was a space at the end of every line in the string.
__________________

Village Idiot is offline  
Reply With Quote
Old 06-25-2009, 10:50 AM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

It also doesn't appear that string values are wrapped with quotes.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
The most inventive way to switch 2 values Wildhoney Tips & Tricks 32 08-10-2010 05:10 AM
How to check valid license without connecting to external server Sam Granger Tips & Tricks 1 05-01-2009 02:14 PM
Sprintf format values [Reference] codefreek Absolute Beginners 0 10-26-2008 02:00 PM
MySQL Query; Counting and adding values ReSpawN Absolute Beginners 3 12-11-2007 10:07 PM


All times are GMT. The time now is 09:15 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design