Newbie question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Trip1
Forum Newbie
Posts: 23
Joined: Sat Jan 25, 2003 7:36 pm

Newbie question

Post by Trip1 »

I'm making a registration script for my website, and i have it so the form adds the info into a mysql database. I get this error when i submit the information:

Parse error: parse error, unexpected T_ELSE in d:\inetpub\wwwroot\register.php on line 23

I have no idea what that means, can anyone help me out?
The code is here:
http://24.157.207.35/register.txt

and that includes a file called common_db.inc
which is at http://24.157.207.35/common_db.inc for your viewing.

Any help appreciated, thanks!
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post by Rob the R »

You can't have an "else" without an "if" that precedes it. Try removing the "else" from your first "else-if" statement.
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

ummm ... this is going to give you problems :

Code: Select all

$result = mysql_query("INSERT INTO user (userid)
                       VALUES('$nickname')");
$result = mysql_query("INSERT INTO user (userpassword)
                       VALUES('$password')");
$result = mysql_query("INSERT INTO user (username)
                       VALUES('$realname')");
$result = mysql_query("INSERT INTO user (usercountry)
                       VALUES('$country')");
$result = mysql_query("INSERT INTO user (useremail)
                       VALUES('$email')");
$result = mysql_query("INSERT INTO user (userprofile)
                       VALUES('$profile')");
$result = mysql_query("INSERT INTO user (registerdate)
                       VALUES('date()')");
you either need to have
"WHERE USERID=$nickname" on every one after the first as so :

Code: Select all

$result = mysql_query("INSERT INTO user (userid)
                       VALUES('$nickname')");
$result = mysql_query("INSERT INTO user (userpassword)
                       VALUES('$password') WHERE USERID=$nickname");
etc .....
or add all the info at once .. which would be better - such as in:

Code: Select all

$result = mysql_query("INSERT INTO user (userid, userpassword, username, usercountry, useremail, userprofile, registerdayt) VALUES('$nickname','$password','$realname','$country','$email','$profile','date()')");
Trip1
Forum Newbie
Posts: 23
Joined: Sat Jan 25, 2003 7:36 pm

RE

Post by Trip1 »

Ok thanks for your help, i took out the first else if and changed it to an if, and i made it so that it adds everything to $result right away.

Now i get an error
Parse error: parse error, unexpected T_STRING in d:\inetpub\wwwroot\register.php on line 24

Line 24 looks like this:

Code: Select all

else if(!$nickname) die("You must enter a nickname");
Also, just another quick question, im storing everything to a variable $result which is supposed to input the information into the mysql database, however do i have to do anything other than:

Code: Select all

$result = mysql_query("INSERT INTO user (userid, userpassword, username, usercountry, useremail, userprofile, registerdayt) VALUES('$nickname','$password','$realname','$country','$email','$profile','date()')");
Will that automatically insert it into the mysql database, or do i have to do something with the variable $result ?
Trip1
Forum Newbie
Posts: 23
Joined: Sat Jan 25, 2003 7:36 pm

Post by Trip1 »

Ok i fixed everything, except for one thing:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\inetpub\wwwroot\register.php on line 43

The block of code that generates that error is the thing that checks to see if the username they have submitted is already in the database..

Code: Select all

Line 42:$result = mysql_query("SELECT * FROM user WHERE username = '$username'");
Line43:if(mysql_num_rows($result) != 0) die("That username is already in use. Please choose another");
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try changing this:

Code: Select all

$result = mysql_query("SELECT * FROM user WHERE username = '$username'");
to

Code: Select all

$sql = "SELECT * FROM user WHERE username = '$username'";
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
and you should hopefully get a more useful error message.

Mac
Trip1
Forum Newbie
Posts: 23
Joined: Sat Jan 25, 2003 7:36 pm

Post by Trip1 »

When i substituted that in for line 42/43 it worked fine-but it didnt check to see if the username is already in use.
I have a table called users, which has values for user_name and password. I just want to check the username that the person submits through the form against what is in the table user_name.. if they submitted 'frank' and frank was already in use, i want it to return an error message saying so.

like i said, i was using this:

Code: Select all

$result = mysql_query("SELECT * FROM users WHERE user_name = '$username'); 
if(mysql_num_rows($result) != 0) die("That username is already in use");
But i get an error: T_STRING or something.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The reason for the T_STRING error is because you're missing a closing double quotes on your SQL statement. You should have only substitued line 42 with the two lines of code I gave you, if you haven't already, try:

Code: Select all

$sql = "SELECT * FROM user WHERE username = '$username'"; 
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
if (mysql_num_rows($result) != 0) {
    die ('That username is already in use.');
} else {
    echo 'The username you have chosen is '.$username.', and it is unique.';
}
Getting into the habit of separating the SQL statement out of the mysql_query() call is a good habit to get into as it can save you a lot of headaches when your queries get more complicated and you pass more variables into them.

Mac
Trip1
Forum Newbie
Posts: 23
Joined: Sat Jan 25, 2003 7:36 pm

Post by Trip1 »

Great guys thanks for all your help, i got it to work now ! :)
Post Reply