need help with an IF statement

This is a discussion on need help with an IF statement within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I have this code: if (isset($_GET['un']) AND isset($_GET['ln'])){ $un = $_GET['un']; $ln = $_GET['ln']; $query = mysql_query(&...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-21-2004
Vinny Gullotta
 
Posts: n/a
Default need help with an IF statement

I have this code:


if (isset($_GET['un']) AND isset($_GET['ln'])){

$un = $_GET['un'];
$ln = $_GET['ln'];

$query = mysql_query("INSERT INTO users ( username, link ) VALUES ('$un',
'$ln')");

if(!$query) die("Invalid query: " . mysql_error());

$result = mysql_query("SELECT link FROM links WHERE ID=$ln") or
die("Invalid query: " . mysql_error());

$y = mysql_fetch_array($result);

$go_link = $y['link'];

header("Location: $go_link");

exit();

}else{
echo "The required criteria was not found in the link.";

}




I need to write an IF statement that will only add $un and $ln to the table
users if the last one added was not the same username as this one (if it's
the same link, that's ok). For the ELSE part, it should still forward them
on to $go_link. Can anyone help? Everything I have tried seems to break
everything, and it's getting late and I'm not thinking too clearly.
Thanks in advance,
Vinny


Reply With Quote
  #2 (permalink)  
Old 01-25-2004
Bryan Chan
 
Posts: n/a
Default Re: need help with an IF statement

If you want to make sure the username is unique in users table there are
several ways to achieve that:
1) Make the username column a unique key and try to catch error number 1062
(Duplicate entry for key #) when inserting new record.

$query = mysql_query("INSERT INTO users ( username, link ) VALUES
('$un','$ln')");
if (mysql_errno() == 1062) {
die("User already exists.");
} elseif (mysql_error()) {
die("Invalid query: " . mysql_error());
}

2) Do a select first and compare with the input username, then do insert.
$num = mysql_query("select users where username='$un'");
if (mysql_num_rows($num) > 0) {
die("User already exists.");
}
$query = mysql_query("INSERT INTO users ( username, link ) VALUES
('$un','$ln')");

Method #1 saves one query, and it sounds quite a practical way to manage
users with unique username.

> $result = mysql_query("SELECT link FROM links WHERE ID=$ln") or
> die("Invalid query: " . mysql_error());

Since I don't see you have insert new record for links table before this
statement, I assume you won't get anything from it unless you did insertion
somewhere else.

Bryan

"Vinny Gullotta" <vinny@wifidirect.com> wrote in message
news:fYoPb.49990$OM2.12074421@news4.srv.hcvlny.cv. net...
> I have this code:
>
>
> if (isset($_GET['un']) AND isset($_GET['ln'])){
>
> $un = $_GET['un'];
> $ln = $_GET['ln'];
>
> $query = mysql_query("INSERT INTO users ( username, link ) VALUES

('$un',
> '$ln')");
>
> if(!$query) die("Invalid query: " . mysql_error());
>
> $result = mysql_query("SELECT link FROM links WHERE ID=$ln") or
> die("Invalid query: " . mysql_error());
>
> $y = mysql_fetch_array($result);
>
> $go_link = $y['link'];
>
> header("Location: $go_link");
>
> exit();
>
> }else{
> echo "The required criteria was not found in the link.";
>
> }
>
>
>
>
> I need to write an IF statement that will only add $un and $ln to the

table
> users if the last one added was not the same username as this one (if it's
> the same link, that's ok). For the ELSE part, it should still forward them
> on to $go_link. Can anyone help? Everything I have tried seems to break
> everything, and it's getting late and I'm not thinking too clearly.
> Thanks in advance,
> Vinny
>
>



Reply With Quote
  #3 (permalink)  
Old 01-25-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: need help with an IF statement

On 2004-01-25, Bryan Chan <lestatc@go.com> wrote:

> If you want to make sure the username is unique in users table there are
> several ways to achieve that:


> Do a select first and compare with the input username, then do insert.
> $num = mysql_query("select users where username='$un'");
> if (mysql_num_rows($num) > 0) {
> die("User already exists.");
> }
> $query = mysql_query("INSERT INTO users ( username, link ) VALUES
> ('$un','$ln')");


This solution has concurrency problems.


--
http://home.mysth.be/~timvw
Reply With Quote
  #4 (permalink)  
Old 01-25-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: need help with an IF statement

On 2004-01-21, Vinny Gullotta <vinny@wifidirect.com> wrote:
> I need to write an IF statement that will only add $un and $ln to the table
> users if the last one added was not the same username as this one (if it's
> the same link, that's ok).


Point your browser at the manual at mysql.com

Lookup how to lock a table / do transactions
Lookup what LAST_INSERT_ID returns.


--
http://home.mysth.be/~timvw
Reply With Quote
Reply


Thread Tools
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:22 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0