/    Sign up×
Community /Pin to ProfileBookmark

How to put function in script when error

I have if sentence and would like to put in script
function ($message, $db) when there is error (ELSE is performed and function has value FALSE other TRUE).

Script is the following:

[code=php]
$result = mysql_query($query) or die(“ERROR: ” . mysql_error());
if(mysql_num_rows($result) > 0) # we found a match, so set $_SESSION flag:
{
$_SESSION[‘logged’] = TRUE;
if(isset($_SESSION[‘caller’]))
# if we got here from another page, go there
{
header(“Location: ” . $_SESSION[‘caller’]);
//*IT SHOULD PERFORM FALSE for function $message
}
else
# otherwise go to main page
{
header(“Location: home.php”);
//*IT SHOULD PERFORM FALSE for function $message
}
}
else
# invalid login, so create error message
{

//*IT SHOULD PERFORM TRUE for function $message

}
[/code]

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 27.2005 — please put your code in VB php tags

that is:

[B][ PHP ] <CODE> [/ PHP ][/B]

[U][B]without[/B][/U] the spaces
Copy linkTweet thisAlerts:
@toplisekauthorSep 27.2005 — [code=php]$result = mysql_query($query) or die("ERROR: " . mysql_error());
if(mysql_num_rows($result) > 0) # we found a match, so set $_SESSION flag:
{
$_SESSION['logged'] = TRUE;
if(isset($_SESSION['caller']))
# if we got here from another page, go there
{
header("Location: " . $_SESSION['caller']);
//*IT SHOULD PERFORM FALSE for function $message
}
else
# otherwise go to main page
{
header("Location: home.php");
//*IT SHOULD PERFORM FALSE for function $message
}
}
else
# invalid login, so create error message
{

//*IT SHOULD PERFORM TRUE for function $message


}[/code]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 28.2005 — what exactly is your question?
Copy linkTweet thisAlerts:
@toplisekauthorSep 28.2005 — I have problem, that when there is error (Not valid username and/or password

) in login page it should give function ($message) TRUE. I will than echo my error when this error will appear with if script like:

if (isset($message])) { echo "This username or password is not valid!"; }

this case is in ELSE script:

else

# invalid login, so create error message


{

//*IT SHOULD PERFORM TRUE for function $message


}
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 29.2005 — where is your function defined?
Copy linkTweet thisAlerts:
@toplisekauthorSep 29.2005 — I would like to have function on the top of my script. So the code is the following:
[code=php]
# I have defined function:
function ($message, $db)
{



$result = mysql_query($query) or die("ERROR: " . mysql_error());
if(mysql_num_rows($result) > 0) # we found a match, so set $_SESSION flag:
{
$_SESSION['logged'] = TRUE;
if(isset($_SESSION['caller']))
# if we got here from another page, go there
{
header("Location: " . $_SESSION['caller']);
//*IT SHOULD PERFORM FALSE for function $message
}
else
# otherwise go to main page
{
header("Location: home.php");
//*IT SHOULD PERFORM FALSE for function $message
}
}
else
# invalid login, so create error message
{

//*IT SHOULD PERFORM TRUE for function $message

}




} //end of function
[/code]
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYSep 29.2005 — a standard function syntax
[code=php]
function nameOfFunction(parameter1, parameter2, ...){
// Arguments
}
[/code]
Copy linkTweet thisAlerts:
@toplisekauthorSep 29.2005 — It shows me

Parse error: parse error, unexpected T_STRING, expecting '(' in ...on line 287

What do you think it is wrong?

I have put in index echo:
[code=php]
<?php


if (isset errorinput($message) )
{ echo "This username or password is not valid!";


[/code]


and in login page:
[code=php]
<?


/* Connect to MySQL-Server */
...


/* Open $db */
...


$user = $_POST['usernamelogin'];
$pswr = $_POST['passwordlogin'];


function errorinput($message, $db)
{

$query ="SELECT * FROM membership WHERE
username = '$user'
AND " ."
password= '$pswr'";


$result = mysql_query($query) or die("ERROR: " . mysql_error());
if(mysql_num_rows($result) > 0) # we found a match, so set $_SESSION flag:
{
$_SESSION['logged'] = TRUE;
if(isset($_SESSION['caller']))
# if we got here from another page, go there
{

header("Location: " . $_SESSION['caller']);return false;

}
else
# otherwise go to main page
{
header("Location: home.php");return false;
}
}
else
# invalid login, so create error message
{
if (isset($_POST['username'])&& isset($_POST['password']) )
{return true;
}

}





}
?>
[/code]
Copy linkTweet thisAlerts:
@bokehSep 29.2005 — Well this line for a start: [code=php]if (isset errorinput($message) )[/code]isset is for checking variables not functions.
Copy linkTweet thisAlerts:
@toplisekauthorSep 29.2005 — I have put:

if (errorinput($message, $db))

{ echo "This username or password is not valid!";

}

?>

and it works,

thanks for you fast reply.
Copy linkTweet thisAlerts:
@bokehSep 29.2005 — You need to better explain what your goal is.
[code=php]function errorinput($message, $db) // what are $message and $db for? Your function does not use them.

$_SESSION['logged'] == TRUE; // = would always be true. Change to ==

header("Location: " . $_SESSION['caller']); exit; // just exit here. No need to run the rest

header("Location: home.php"); exit; // just exit here. No need to run the rest
[/code]
Copy linkTweet thisAlerts:
@toplisekauthorSep 29.2005 — When error in login page shows not valid username or password, it should give function

errorinput() TRUE. it works function now if there is wrong input. Problem is that if user is

put into form LOGIN, there is message.

Fatal error: Cannot redeclare errorinput() (previously declared in login page...)

What do you think it is wrong?
Copy linkTweet thisAlerts:
@bokehSep 29.2005 — Why don't you just have the function output the error message and then exit or reprint the form? Anyway that still doesn't answer why you are sending arguments to the function and then not using them.
Copy linkTweet thisAlerts:
@toplisekauthorSep 29.2005 — I did it. Thanks for fast answer. You helped me a lot.
×

Success!

Help @toplisek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...