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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
darul06
Forum Newbie
Posts: 2
Joined: Thu Oct 04, 2007 6:57 pm

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

Post 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]
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
darul06
Forum Newbie
Posts: 2
Joined: Thu Oct 04, 2007 6:57 pm

Post by darul06 »

Do i need to divide it in form of if else statement? what about the ":" symbol?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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';
}
Post Reply