Jump to content

Parse error: syntax error, unexpected T_ELSE in C:\wamp\www\test\insert.php on l


Recommended Posts

i googled it and found over 100+ similar problems but couldnt find out how to fix it...

here's the code that's giving me problems...

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
  $sql_check = "SELECT username FROM register WHERE username = '$_POST[username]'";

$check = mysql_num_rows('$sql_check');

if($check > 0) { echo "username is already in use"; } else { BLA BLA BLA, ... 


mysql_select_db("register", $con);

$sql="INSERT INTO register (username, email, password)
VALUES
('$_POST[username]','$_POST[email]','$_POST[password]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "register success!!!";

mysql_close($con)
?>

...ummm

 

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
  $sql_check = "SELECT username FROM register WHERE username = '$_POST[username]'";

$check = mysql_num_rows('$sql_check');

if($check > 0) { 
    echo "username is already in use"; 
}else {  
    mysql_select_db("register", $con);

    $sql="INSERT INTO register (username, email, password)
VALUES
('$_POST[username]','$_POST[email]','$_POST[password]')";

    if (!mysql_query($sql,$con))
     {
       die('Error: ' . mysql_error());
    }
     echo "register success!!!";
}
?>

 

Should fix it, you never ended the } the else and had that BLA BLA BLA uncommented.

 

Also mysql_close does not need to be called, it is closed automatically after script executes.

Change line 6 to this:

$sql_check = mysql_query("SELECT username FROM register WHERE username = '".addslashes($_POST['username'])."'");

 

Then the next line to this:

$check = mysql_num_rows($sql_check);

mysql_query needs to be ran. Also you need to select the DB

 

<?php
$con = mysql_connect("localhost","root","");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("register", $con);  
  $sql_check = "SELECT username FROM register WHERE username = '" . mysql_real_escape_string($_POST['username']) . "'"; // needs to be done like this or with { } around the $_POST or else it will not work right.

$check = mysql_num_rows(mysql_query($sql_check)); // mysql_query for the check

if($check > 0) { 
    echo "username is already in use"; 
}else {  
//    mysql_select_db("register", $con); move this up top

 

premiso - I've always had issues with mysql_real_escape_string but that's another topic...

 

function escapeString($string) {
	return (get_magic_quotes_gpc()) ? mysql_real_escape_string(stripslashes($string)):mysql_real_escape_string($string);
}

 

Chances are it is because you have get_magic_quotes_gpc on, try using that function instead, which should escape a string properly.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.