register.php

Could anyone tell me whats wrong with the following code please?
Error message : Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\Website\registernext.php on line 35

[php]<?php

include(‘confff.php’);

$testname = $_POST[‘username’];

if(!preg_match(’/[^a-z0-9]/i’,$testname) ) //test to see the username is alphanumeric

    {

    $query = "SELECT * FROM users WHERE username=".$_POST["username"];   //test for dupilcate names
    $result = mysql_query($query);
    $num = mysql_num_rows($result);

    }


    if ($num == 0)

    
    {

    }

  else

{
header(“location:nameinuse.html”);

}
else

{
header(“location:invalidname.html”);
}

?>[/php]

Mistake is here:
[php]if ($num == 0)
{

}
else
{
header(“location:nameinuse.html”);
}
else
{
header(“location:invalidname.html”);
}
[/php]

you have two else in the same if block.
Replace first instance with elseif and your syntax will be correct.

I’m still recieving an error :confused:

Parse error: syntax error, unexpected '{', expecting '(' in C:\xampp\htdocs\Website\registernext.php on line 30

I’m new to PHP sorry, i tried fixing the errors but came to no prevail

[php]
if ($num == 0)

    {

    }

  elseif

{
header(“location:nameinuse.html”); //test for dupilcate names

}
else

{
header(“location:invalidname.html”);
}
[/php]

hi Daniel++, put quotes around the $_POST["username"] in $query. use below query [php] $query = "SELECT * FROM users WHERE username='".$_POST["username"]."'"; [/php] i hope this will resolve your problem reply your feedback ~~SR~~

Daniel++,
Your code is hard to read, try to format it before posting.

After the elseif statement you need to have a condition, like this:
[php]if ($num == 0)
{

}
elseif ($nameinuse == true)
{
header(“location:nameinuse.html”);
}
else
{
header(“location:invalidname.html”);
}
[/php]

Thanks for the help, I’ve been fiddling around with the code but still can’t get it working. it slightly works how I want it to when I remove all ‘{’ ‘}’. But surely this isn’t right? Again thank you for the help. Heres the code i’ve got so far, tried to tidy it up abit for you guys also. And my error message is:

Parse error: syntax error, unexpected '}' in C:\xampp\htdocs\Website\registernext.php on line 27

[php]<?php

include(‘confff.php’);

$testname = $_POST[‘username’];

if(!preg_match(’/[^a-z0-9]/i’,$testname) )

    $query = "SELECT * FROM users WHERE username='".$_POST['username']."'";
    $result = mysql_query($query);
    $num = mysql_num_rows($result);

   if ($num == 0)
   
   {
   echo "Successful";
   }


   elseif ($num == true)
   
   {
   header("location:nameinuse.html")
   }

?>

[/php]

Seems i’ve got it working now, but it doesn’t pick up on duplicate names.

I get the error

[code]Notice: Undefined variable: query in C:\xampp\htdocs\Website\registernext.php on line 13

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Website\registernext.php on line 14[/code]

[php]<?php

include(‘confff.php’);

$testname = $_POST[‘username’];

if(!preg_match(’/[^a-z0-9]/i’,$testname) )

    $query = "SELECT * FROM users WHERE username='".$_POST['username']."'";
    $result = mysql_query($query);
    $num = mysql_num_rows($result);

if ($num == 0)

 {
  echo "Success";
 }

elseif ($nameinuse == true)

{
  header("location:nameinuse.html");
}   

else
{
header(“location:invalidname.html”);
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service