PDA

View Full Version : Detecting if user has already entered info


Tjk
08-31-2005, 09:47 PM
I have a simple checking system I want to use.

However I am stuck on saying if they have or have not entered information or not. Here's why.

I want to use code that basically says...


$result= mysql_query("SELECT * FROM table WHERE $row['userid']= $_SESSION['id']")
or die(mysql_error())

if ($result == "false"){
Do this;
}
else if ($result == "true"){
Do this;
}


Basically I want to say if a user hasn't got any information in that table then do something else if they haven't do this. Will my way work?

Noodles24
09-01-2005, 01:43 AM
no, your way won't work

What you need to do is something like this:


$result= mysql_query("SELECT * FROM table WHERE userid = {$_SESSION['id']}")
or die(mysql_error());

if (mysql_num_rows($result) > 0){
//user exists
Do this;
}
else{
//user doesn't exist
Do this;
}

Tjk
09-01-2005, 12:50 PM
On the subject of sign up scripts could you help me with this...

This is in my signup script.

mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("phpbbforum") or die(mysql_error());

$result = mysql_query("SELECT * FROM phpbb_users") or die(mysql_error());

$username2 = strtolower($username);
$password2 = md5($password);

while($row = mysql_fetch_array( $result )) {
$username3 = strtolower($row[username]);
if ( $username3 == $username2 ) {
$upass = 1;
if ($row[user_password] == $password2 ) {
$ppass = 1;
$_SESSION['username'] = $username2;
$_SESSION['password'] = $password;
$_SESSION['userid'] = $userid;
break;
}
}
}


Now I'm referencing userid from the phpbb forum userid which remains constant for each user no matter what.

I want to use the userid from there and use it to compare with the userid in a table so that it recognises which user information it needs to select (using the WHERE userid= line I've shown in my previous problem.)

Now when I try


mysql_connect("localhost", "usernameII", "passwordII") or die(mysql_error());
mysql_select_db("otherdatabase") or die(mysql_error());

$result= mysql_query("SELECT * FROM PCharacters WHERE userid= {$_SESSION['userid']}")
or die(mysql_error());

if (mysql_num_rows($result) > 0){
//user exists
mysql_query("UPDATE PCharacters SET username={$_SESSION['username']} WHERE userid= {$_SESSION['userid']} ")
}
else{
//user doesn't exist
}


I have created in the table 'PCharacters' a row for userid and username. But it says "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 '' at line 1". Any ideas?

Line 1 of my whole page is


<?
session_start();
?>

NancyJ
09-01-2005, 01:00 PM
line 1 refers to line 1 of your sql statement not your page.
Change this line
mysql_query("UPDATE PCharacters SET username={$_SESSION['username']} WHERE userid= {$_SESSION['userid']} ")
to
mysql_query("UPDATE PCharacters SET username='{$_SESSION['username']}' WHERE userid= {$_SESSION['userid']} ")

assuming username is a string not an integer. Strings in sql statements need to be enclosed in quotes.

Tjk
09-01-2005, 01:55 PM
Thanks for your help guys. I've got it working just fine now. :thumbsup: