Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 08-08-2005, 06:02 AM   #1
Member (10 bit)
 
Designer's Avatar
 
Join Date: Mar 2004
Location: Essex, UK
Posts: 982
Send a message via AIM to Designer Send a message via MSN to Designer
MySQL query question

Hi guys,

I'm trying to determine if a row or multiple rows in a database exist with specific data in them.

What i've got is a DB with 4 fields, one of which is username, i want to check to see if the username exists already in the database to prevent the name being added multiple times, (im doing this in PHP) so the script should add the row's if the username isnt in the DB already or should amend them if they already exist.

Here's my code so far, could you look over it and see what im doing wrong
PHP Code:
$query=" SELECT * FROM quartertime WHERE user='$user'";   //Query the DB to check if user exists
$result=mysql_query($query);
$num=mysql_numrows($result);
if (
$num !="0")
{                                    
//if record exists

$query "UPDATE quartertime SET user = '$user', car = '$car', qmtime = '$quartertime' WHERE user = '$user'";
mysql_query($query);
$imdoing="Updating existing record";

} else {    
                                                    
//if record doesnt exist
$query "INSERT INTO quartertime ( user, car, qmtime );   
values ( '$user', '$car', '$quartertime')"
;
@
mysql_query$query);
$imdoing="Adding new record";                                            

When that page loads it seems to always try and amend non-existant records, i wasnt overly sure what i should be looking for in the first query to determine if the record(s) exist or not. the field i want to examine is 'user' and compare it to the variable $user.

Thanks
__________________
Intel Core i7 920 2.66GHz | Asus P6T SE| 6GB Corsair ram | XFX Radeon HD 4780 | 2 x 250 Gb Maxtor SATA II (in RAID 0), 1 x 250GB SATA II | Creative XFI soundcard | Coolermaster 650W PSU | ThermalTake Tsunami case

My new website -Carp Fishing Forums
Designer is offline   Reply With Quote
Old 08-09-2005, 07:35 AM   #2
Member (6 bit)
 
Deitel's Avatar
 
Join Date: Apr 2004
Location: Malta
Posts: 41
I don't know how to use php but i think you may have a mistake in you sql code.

When you are comparing with a string, u must make sure that the string is in " ' ", i.e.

Code:
select * from users where users_name='deitel';
so by the look of your code, the sql is searching for a user called $user. Now as I said, I dont know how to use php, but in delphi i add to the string, i.e.

Code:
sql := "select * from users where users_name='" + usernamevariable + "';";
if you catch my drift. That may be your problem
Deitel is offline   Reply With Quote
Old 08-09-2005, 04:48 PM   #3
Member (10 bit)
 
Designer's Avatar
 
Join Date: Mar 2004
Location: Essex, UK
Posts: 982
Send a message via AIM to Designer Send a message via MSN to Designer
I've just found the mistake
changed this
PHP Code:
mysql_query($query); 
to this
PHP Code:
@mysql_query($query); 
and missed a ; later on

By the way the $user is a variable, it's value is obtained higher up the script and is imported from the forum database

Anyways, ran into one other PHP problem and that's how do I validate the user's input for the $quartertime variable, this is taken from a text entry field on a different page.
I am expecting to see values greater than 3 but not greater than 30, it can also be values to 3 decimal places i.e 10.345 would be ok but not to 4 decimal places, i want to reject anything outside of these conditions (mainly to prevent people typing into the box and submitting it )

Thanks
Designer is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes 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 On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 04:55 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2