Jump to content

array() help


nicangeli

Recommended Posts

Hello. Please take a look at the code below and if any of you could tell me why $error['false'] is never getting echoed that would be a great help. Basically the script is a login script and i am testing the POST variables agains things and storing the error messages in a array. I then attempt to echo the array out foreach loop but ['false'] never works.

 

<?php
session_start();
require('areatop.php');
require('includes/colleft.php');
if (isset($_POST['submit']))
{
$error = array();
if (empty($_POST['username']))
{
$error['username'] = "Please enter a username";
}
if (empty($_POST['password']))
{
$error['password'] = "Please enter a password";
}
if (count($error) == 0)
{
// FORM PROCCESSING HERE
$query = mysql_query("query here");
if (!$query)
{
echo "NO QUERY" . mysql_error();
}
else
{
$number = mysql_num_rows($query);
if ($number == "1")
{
echo "Logged In";
}
else
{
$error['false'] = "Incorrect login details";
}
}
}
else
{
echo '<div class="formerror"><img src="includes/images/triangle_error.gif" />Please correct the following errors:<ul>';
foreach ($error as $e)
{
echo "<li>$e</li><br>";
}
if (!empty($error['false']))
{
echo $error['false'];
}
echo '</ul></div><br>';
}
}
?>
<p>
<form method="post" action="<?php echo $PHP_SELF; ?>">
<label for="username">Username: </label>
<input type="text" name="username" value="<?php echo $_POST['username']; ?>" class="<?php
if (!empty($error['username']))
{
echo 'formerror';
}
elseif (!empty($error['false']))
{
echo 'formerror';
}
?>
">
<label for="password">Password: </label>
<input type="password" name="password" class="<?php
if (!empty($error['password']))
{
echo 'formerror';
} 
elseif (!empty($error['false']))
{
echo 'formerror';
}
?>
">
<br>
<input type="submit" name="submit" value="Log In" />
</form>

Link to comment
https://forums.phpfreaks.com/topic/60663-array-help/
Share on other sites

You would probably find the error a whole stack easier to spot if you intended your code:

 

<?php
session_start();
require('areatop.php');
require('includes/colleft.php');
if (isset($_POST['submit']))
{
$error = array();
if (empty($_POST['username']))
{
	$error['username'] = "Please enter a username";
}
if (empty($_POST['password']))
{
	$error['password'] = "Please enter a password";
}
if (count($error) == 0)
{
	// FORM PROCCESSING HERE
	$query = mysql_query("query here");
	if (!$query)
	{
		echo "NO QUERY" . mysql_error();
	}
	else
	{
		$number = mysql_num_rows($query);
		if ($number == "1")
		{
			echo "Logged In";
		}
		else
		{
			$error['false'] = "Incorrect login details";
		}
	}
}
else
{
	echo '<div class="formerror"><img src="includes/images/triangle_error.gif" />Please correct the following errors:<ul>';
	foreach ($error as $e)
	{
		echo "<li>$e</li><br>";
	}
	if (!empty($error['false']))
	{
		echo $error['false'];
	}
	echo '</ul></div><br>';
}
}
?>

 

Basically, $error['false'] can only ever be set if there are no errors in the username and password, since all of the database quering is contained in the codeblock:

 

<?php
if (count($error) == 0)
{
//here is all the quering stuff and here is were $error['false'] is set
}
?>

 

You then add an else statement where the errors are displayed.

 

<?php
if (count($error) == 0)
{
//here is all the quering stuff and here is were $error['false'] is set
}else{
//here is where you echo the errors
}
?>

 

So for your false error to be displayed, both parts of this would have to be executed, which is , of course, im possible( since count($error) cant be 0 and not 0).

 

Hope that makes some sense. It's kinda difficult for me to suggest the fix. It depends how you want your script to work.

Link to comment
https://forums.phpfreaks.com/topic/60663-array-help/#findComment-302302
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.