...

View Full Version : unexpected ]



rafiki
03-06-2007, 07:25 PM
whats wrong with this




<?php
$submit = $_GET['submit'];
$name = $_POST['name'];
$sname = $_POST['sname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$errormsg = array("No username Entered", "No password entered or password too short", "No email address entered or incorrect email address", "You did'nt fill in your first/surname");
$error = '';
$count = "count($password)";
if (isset($submit){
if (empty($username))
{
$error .= $errormsg[0];
}
if (empty($password) || $count < 6 )
{
$error .= $errormsg[1];
}
if (empty($email) || !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
$error =. $errormsg[2];
}

if (empty($name) || empty($sname))
{
$error .= $errormsg[3];
}

if ($error == '')
{
include 'sqlprotect.php';
$datahost = localhost;
$dataname = databasename;
$datapase = databasepass;
$datauser = databaseuser;

mysql_connect($datahost, $datauser, $datapass) or die(mysql_error());
mysql_select_db($dataname) or die(mysql_error());

$check = mysql_query("SELECT username FROM users WHERE username='$username'");

$user_exist = mysql_num_rows($check);

if($user_exist > 0){
echo "I'm sorry but the username you specified has already been taken. Please pick another one.";
unset($username);
exit;
}
$query = "INSERT INTO users (name, sname, email, username, password, )
VALUES('$name', '$sname', '$email', '$username', '$password')";
mysql_query($query) or die(mysql_error());
mysql_close();

echo "You have successfully registered please click <a href =\"/login.php\" >here</a> to log in";
}else{
echo "$error";
}
}else{
include 'prereg.php';
}
?>



error is Parse error: parse error, unexpected '[' in line 20

Inigoesdr
03-06-2007, 07:36 PM
The quotation mark:


$count = "count($password);

rafiki
03-06-2007, 07:41 PM
omg shoulda spotted that, thanks, is that the only thing you can see wrong?

Inigoesdr
03-06-2007, 07:55 PM
omg shoulda spotted that, thanks, is that the only thing you can see wrong?

You don't need quotes around count() at all.
Just for simplicity you should have the errors in the places where they are going to be used. ie. $error .= 'Error message';
All of your $data* database info should be in quotes ie. $datahost = 'localhost';
While it is essentially the same result, you should use mysql_result() and SELECT COUNT(*) instead of selecting the field since you don't need the username text from the database, only to see if it exists.

With your error text you should probably add linebreaks after each error, so if there is more than one each will be on a separate line.

rafiki
03-06-2007, 08:20 PM
You don't need quotes around count() at all.
Just for simplicity you should have the errors in the places where they are going to be used. ie. $error .= 'Error message';
All of your $data* database info should be in quotes ie. $datahost = 'localhost';
While it is essentially the same result, you should use mysql_result() and SELECT COUNT(*) instead of selecting the field since you don't need the username text from the database, only to see if it exists.With your error text you should probably add linebreaks after each error, so if there is more than one each will be on a separate line.
first im going to get it working then worry about formatting
making it look pretty is the least of my worrys



<?php
$submit = $_POST['submit'];
$name = $_POST['name'];
$sname = $_POST['sname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$errormsg = array("No username Entered", "No password entered or password too short", "No email address entered or incorrect email address", "You did'nt fill in your first/surname");
$error = '';
$count = "count($password)";
if (isset($submit)
{ //line 12 <<-----
if (empty($username))
{
$error .= $errormsg[0];
}
if (empty($password) || $count < 6 )
{
$error .= $errormsg[1];
}
if (empty($email) || !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
$error =. $errormsg[2];
}

if (empty($name) || empty($sname))
{
$error .= $errormsg[3];
}

if ($error == '')
{
include 'sqlprotect.php';
$datahost = ******;
$dataname = ******;
$datapase = *****;
$datauser = *******;

mysql_connect($datahost, $datauser, $datapass) or die(mysql_error());
mysql_select_db($dataname) or die(mysql_error());

$check = mysql_query("SELECT COUNT(*) username FROM users WHERE username='$username'");
//updated that line ^^^^^^ this correct?
$user_exist = mysql_num_rows($check);

if($user_exist > 0){
echo "I'm sorry but the username you specified has already been taken. Please pick another one.";
unset($username);
exit;
}
$query = "INSERT INTO users (name, sname, email, username, password, )
VALUES('$name', '$sname', '$email', '$username', '$password')";
mysql_query($query) or die(mysql_error());
mysql_close();

echo "You have successfully registered please click <a href =\"/login.php\" >here</a> to log in";
}else{
echo "$error";
}
}else{
include 'prereg.php';
}
?>

Parse error: parse error, unexpected '{' in rafiki.freehostia.com/register.php on line 12

Inigoesdr
03-06-2007, 08:23 PM
Put a parenthesis after the previous line to close the if():


if (isset($submit))
{ //line 12 <<-----

Fumigator
03-06-2007, 08:32 PM
first im going to get it working then worry about formatting
making it look pretty is the least of my worrys

Formatting it properly to begin with would help you prevent silly syntax mistakes in the first place. It's like saying you've got to get the house built first, then worry about where you put the door.

aedrin
03-06-2007, 08:38 PM
While it is essentially the same result, you should use mysql_result() and SELECT COUNT(*) instead of selecting the field since you don't need the username text from the database, only to see if it exists.

Although it's not really that much of an issue, I'd suggest to SELECT 1 FROM users WHERE etc., and then use mysql_num_rows().

And Fumigator is correct. When you start with clean code, you won't have to redo everything. And it's not really about making it look pretty, it's about making it readable and easier to spot errors.

Indentation is one of the biggest things that improves readability. And being consistent with that indentation.

rafiki
03-06-2007, 08:45 PM
<?php
$submit = $_POST['submit'];
$name = $_POST['name'];
$sname = $_POST['sname'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];
$errormsg = array("No username Entered", "No password entered or password too short", "No email address entered or incorrect email address", "You did'nt fill in your first/surname");
$error = '';
$count = count($password);
if (isset($submit))
{
if (empty($username))
{
$error .= $errormsg[0];
}
if (empty($password) || $count < 6 )
{
$error .= $errormsg[1];
}
if (empty($email))
{
$error =. $errormsg[2];
}
if (empty($name) || empty($sname))
{
$error .= $errormsg[3];
}

if ($error == '')
{
include 'sqlprotect.php';
$datahost = *********;
$dataname = *******;
$datapase = ******;
$datauser = *******;

mysql_connect($datahost, $datauser, $datapass) or die(mysql_error());
mysql_select_db($dataname) or die(mysql_error());

$check = mysql_query("SELECT COUNT(*) username FROM users WHERE username='$username'");

$user_exist = mysql_num_rows($check);

if($user_exist > 0){
echo "I'm sorry but the username you specified has already been taken. Please pick another one.";
unset($username);
exit;
}
$query = "INSERT INTO users (name, sname, email, username, password, )
VALUES('$name', '$sname', '$email', '$username', '$password')";
mysql_query($query) or die(mysql_error());
mysql_close();

echo "You have successfully registered please click <a href =\"/login.php\" >here</a> to log in";
}else{
echo "$error";
}
}else{
include 'prereg.php';
}
?>

this is upto date code
error is unexpected '.' on line 23
but i dont see a . on line 23 :s

Inigoesdr
03-06-2007, 08:49 PM
$user_exist = mysql_result(mysql_query("SELECT COUNT(*) username FROM users WHERE username='$username'"), 0);

Nightfire
03-06-2007, 10:12 PM
You have


$error =. $errormsg[2];

Instead of


$error .= $errormsg[2];


Be careful of what you're typing

aedrin
03-06-2007, 11:13 PM
$user_exist = 0;
$user_exist = mysql_result(mysql_query("SELECT COUNT(*) username FROM users WHERE username='$username'"), 0);


Why the first line?

Inigoesdr
03-07-2007, 12:35 AM
Why the first line?
Fixed.

rafiki
03-07-2007, 12:36 AM
Formatting it properly to begin with would help you prevent silly syntax mistakes in the first place. It's like saying you've got to get the house built first, then worry about where you put the door.
how do you suggest i format the code whilst doing it? is there like a "Standard" way of doing this? \n works for new line on windows servers right?

Nightfire
03-07-2007, 03:04 AM
Use your tab key to lay the code out well. It'll take a while to get used to it, but it helps you from making silly errors as you can notice them much easier.

example


<?php
if($blah == 'foo'){
do this
if($this != 'ooh'){
dont do that
}else{
do that instead
}}else{
do that
}
if($someothervar == 'bar'){
do this
else{
do that
}
?>

If that was formatted whilst you write it, you'd notice there's a curly brace missing straight away.


<?php
if($blah == 'foo'){
do this
if($this != 'ooh'){
dont do that
}else{
do that instead
}
}else{
do that
}

if($someothervar == 'bar'){
do this
}else{
do that
}
?>

Do you see how much easier the second example is to follow?

rafiki
03-07-2007, 03:30 PM
yes a lot ill try gettin this into my practices,

so it should look like




if ($action = submit) {
do this to something
}
else {
dont do this
}


edit: cant use tabs in this text field although i will when im using notepad/editor

Nightfire
03-07-2007, 03:51 PM
Yeah that's about right. It's easier if you line up the curly braces with eachother, so you can easily notice where the loops,if's,etc start and end. Experiment with it and you'll have your own way of keeping things neat and easy to read. There's no set way in stone on how to do it

rafiki
03-08-2007, 04:01 PM
$user_exist = mysql_result(mysql_query("SELECT COUNT(*) username FROM users WHERE username='$username'"), 0);



this is the error i get whilst using this line

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/www/rafiki.freehostia.com/register.php on line 34
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 ') VALUES

ralph l mayo
03-08-2007, 04:07 PM
Can you really not debug these trivial syntax problems given the helpful error messages, or are you being lazy?

JohnDubya
03-08-2007, 04:22 PM
this is the error i get whilst using this line


It can't be the line of code you gave because ') VALUES is not in there. It's got to be from another query.

rafiki
03-08-2007, 04:34 PM
$user_exist = mysql_result(mysql_query("SELECT COUNT(*) username FROM users WHERE username='$username'"), 0);
if($user_exist > 0){
echo "I'm sorry but the username you specified has already been taken. Please pick another one.";
unset($username);
exit;
}
$query = "INSERT INTO users (name, surname, email, username, password, )
VALUES('$name', '$sname', '$email', '$username', '$password')";
mysql_query($query) or die(mysql_error());
mysql_close();
these are my only 2 query's

JohnDubya
03-08-2007, 04:39 PM
You've got an extra comma after "password" in:


INSERT INTO users (name, surname, email, username, password, )

rafiki
03-08-2007, 05:27 PM
fixed thanks
how do i add an if statement to this




<?php
include("mysqlconnect.php");

// Create a MySQL table in the selected database
mysql_query("CREATE TABLE users(
username VARCHAR(30) ,
PRIMARY KEY(username),
name VARCHAR(30),
surname VARCHAR(30),
password VARCHAR(30),
email VARCHAR(50)");

echo "Table Created!";

is this it

<?php
include("mysqlconnect.php");

$query = "CREATE TABLE users(
username VARCHAR(30) ,
PRIMARY KEY(username),
name VARCHAR(30),
surname VARCHAR(30),
password VARCHAR(30),
email VARCHAR(50);"
$result = mysql_query($query);
if (!$result){
echo "table not created";
}else{
echo "Table Created!";
}
?>
any ideas?

JohnDubya
03-08-2007, 09:16 PM
Yeah, that should work. Sometimes, you may want to output the mysql_error() as well as echoing an error message. Like so:


if (!$result) {
echo "table not created";
echo mysql_error();
} else {
echo "Table Created!";
}

Nightfire
03-08-2007, 09:36 PM
You might want to extend on that a little more



CREATE TABLE IF NOT EXISTS users(
username VARCHAR(30) ,
PRIMARY KEY(username),
name VARCHAR(30),
surname VARCHAR(30),
password VARCHAR(30),
email VARCHAR(50)
)

rafiki
03-09-2007, 12:27 AM
You might want to extend on that a little more



CREATE TABLE IF NOT EXISTS users(
username VARCHAR(30) ,
PRIMARY KEY(username),
name VARCHAR(30),
surname VARCHAR(30),
password VARCHAR(30),
email VARCHAR(50)
)

Thanks fire works perfect, ill add positive rep

Nightfire
03-09-2007, 03:23 AM
Cheers :)

aedrin
03-09-2007, 05:44 PM
Can you really not debug these trivial syntax problems given the helpful error messages, or are you being lazy?

Although a little bit harsh, I'm wondering the same.

An essential skill for debugging PHP is being able to find syntax errors. If you have to keep asking on a forum, I'm thinking that you need to refresh some basic syntax rules.

rafiki
03-09-2007, 06:30 PM
Although a little bit harsh, I'm wondering the same.

An essential skill for debugging PHP is being able to find syntax errors. If you have to keep asking on a forum, I'm thinking that you need to refresh some basic syntax rules.unlike you lt i never use colour based editors but i dloaded one today. hope this is gonna help me

aedrin
03-09-2007, 09:27 PM
I thought that most people used a decent PHP editor. Although not a requirement, it makes things a lot easier. Besides them telling you about syntax errors.

I'd recommend using Eclipse PDT. It's free and has a lot of useful features.

http://www.eclipse.org/pdt/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum