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!
Everah | Please use bbCode tags when posting code in the forums. It makes it a lot easier for people to read.
Good Morning,
I have the followng code in a form I am building. If certain things happen, the page redirects back to form. However I want the database to close when if a redirect happens. Right now, when someone puts in the wrong password it still writes to the database... wrong username it still writes to the database... etc.
How do I get it to stop writing to the database if it doesn't pass validation?
<?php
// Validation for password and answers
if($password != $password2){
echo "Your passwords do not match. Please re-enter.<meta http-equiv=\"refresh\"content=\"2;URL=signup.php\">";
}
elseif(strlen($_POST['password']) < 6){
echo "Password must contain at least 6 characters";
print "<meta http-equiv=\"refresh\" content=\"2;URL=signup.php\">";
}
elseif($answer != $answer1){
echo "Your answer does not match. Please re-answer";
print "<meta http-equiv=\"refresh\" content=\"2;URL=signup.php\">";
}
//end validation
mysql_close($connect) or die("Unable to Close Database<br>".mysql_error());
echo " Your Information has been successfully added to the database. Thank you for your registration.";
?>
Everah | Please use bbCode tags when posting code in the forums. It makes it a lot easier for people to read.
Last edited by RobertGonzalez on Sun Apr 27, 2008 6:33 pm, edited 1 time in total.
Reason:Added code tags
$loginErr=0;
if(isset($_POST['submit'])
{
if($password != $password2){
$loginErr=1;
}
elseif(strlen($_POST['password']) < 6){
$loginErr=2;
}
elseif($answer != $answer1){
$loginErr=3;
}
if($loginErr==0){
//LOGIN OK
}
else
{
switch($loginErr)
{
case 1:
echo"Passwords do not match!"; //redirect
break;
//etc...
}
}
}
Using one error variable only allows you to output one error message. You could declare specific error variables in order to output multiple error message.
if($loginErr==0){
//LOGIN OK
}
else
{
switch($loginErr)
{
case 1:
echo"Passwords do not match! <meta http-equiv=\"refresh\"content=\"2;URL=signup.php\">"; //redirect
break;
case 2:
echo"Passwords must contain at least 6 characters! <meta http-equiv=\"refresh\"content=\"2;URL=signup.php\">"; //redirect
break;
case 3:
echo"Your answer does not match. Please re-answer. <meta http-equiv=\"refresh\"content=\"2;URL=signup.php\">"; //redirect
break;
//etc...
}
}
}
$connect = mysql_connect('ip', 'database', 'password') or die("Unable to Connect to Database<br>".mysql_error());
mysql_select_db('askslicky') or die("Unable to Select Database<br>".mysql_error());
$name_check = "SELECT username FROM users WHERE username = '".$_POST['username']."'"; // check if username exists in database.
if (!($name_check)) print mysql_error();
$result = mysql_query($name_check);
if ($name_check == $username){
print "<meta http-equiv=\"refresh\" content=\"2;URL=signup.php\">";
die('Sorry, the username: <strong>'.$_POST['username'].'</strong> is already taken, please pick another one.<meta http-equiv="Refresh"content="2;url=signup.php"/>')
;}
mysql_close($connect) or die("Unable to Close Database<br>".mysql_error());
echo " Your Information has been successfully added to the database. Thank you for your registration.";
Thank you... that fixed that problem, however I have one more problem..
The break is still not working. No matter what I put in, the form still adds the info to the database and releases no error message. I even replaced the echo with print to see if I can get it to work.
My form has the following code in it: