Page 1 of 1

": " symbol and Parse error: parse error, unexpect

Posted: Thu Oct 04, 2007 7:09 pm
by darul06
scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi fellow forumer,

Need your help as I new in php. First thing is what the " : " symbol means? I cannot find in post or other search in google. Second, I wrote a php code and when I run, it produces error:

[b]Parse error: parse error, unexpected ':' in C:\Inetpub\wwwroot\formtest2.php on line 12[/b]
 
what this error mean? Can anybody explain what was the error indicate. Below is the code:

Code: Select all

<?php
echo '<html><head><basefont face="Arial"></head><body>';
if (!isset($_POST['submit']) )
{
// form not submitted
  echo '<form action="formtest2.php" method="post" onsubmit="redirect()">Username: <input type="text" name="username"><br />
        Password: <input type="password" name="password"><br /><br /><input type="submit" name="submit" value="Sign Up"></form>';
}
else
{
  if(
    $username=(!isset($_POST['username']) || trim($_POST['username']) == "") or die('ERROR: Enter a username') : mysql_escape_string(trim($_POST['username']));
	$password = (!isset($_POST['password']) || trim($_POST['password'] == "")) or die ('ERROR: Enter a password') : mysql_escape_string(trim($_POST['password']));
	// connect to database
    // open connection
    /*$connection = mysql_connect('localhost', ' ', ' ') 
    or die ('Unable to connect!');*/
    $connection = mysql_connect('localhost','','') or die ('Unable to connect!');;
    // select database
     mysql_select_db('ebcaptive') or die ('Unable to select database!');
    // create query
    $query = "INSERT INTO user (username, password) 
    VALUES ('$username', '$password')";
    // execute query
    $result = mysql_query($query) 
    or die ("Error in query: $query. " . mysql_error());
    // close connection
    mysql_close($connection);
   )
echo 'You have successfully registered!';
echo '</body></html>';
}
?>

thanks.
.


scottayy | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Oct 04, 2007 7:17 pm
by s.dot
Well, it's in this line:

Code: Select all

$username=(!isset($_POST['username']) || trim($_POST['username']) == "") or die('ERROR: Enter a username') : mysql_escape_string(trim($_POST['username']));
I don't think you're using the ternary operator syntax correctly. I believe it accepts only one condition to evaluate in the if()

Posted: Thu Oct 04, 2007 7:26 pm
by darul06
Do i need to divide it in form of if else statement? what about the ":" symbol?

Posted: Thu Oct 04, 2007 10:13 pm
by superdezign
The colon means nothing on it's own. In combination with a question mark, it's the ternary operator. i.e.

Code: Select all

$someValue = false;
echo ($someValue == true) ? '$someValue is true' : '$someValue is false';
That will echo "$someValue is false."

The way the ternary operator works is:

Code: Select all

condition ? what to do if the condition is met : what to do if the condition is not met
So, these are equal:

Code: Select all

echo $someValue == true ? '$someValue is true' : '$someValue is false';

// OR

if ($someValue == true) {
    echo '$someValue is true';
} else {
    echo '$someValue is false';
}