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(&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 > > |
|
|||
|
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 |
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|