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!
Hey guys, I am rather new at php programming and have run into a problem when trying to validate a login script. The php script is ignored when the form is submitted and the form just executes its action and directs the browser to the next page. If anyone has any insight as to my errors, please let me know, it is greatly appreciated.
<?php
if (isset($_POST['submit'])){
$username=mysql_real_escape_string($_POST["username"]);
$password=$_POST["password"];
$confirm_password=$_POST["confirm_password"];
$first_query="SELECT * FROM users WHERE username ='$username'";
$result=mysql_query($first_query);
$count=mysql_num_rows($result);
if ($password==""){
echo 'please fill in all fields';
die();
}
else if ($username==""){
echo 'please fill in all fields';
die();
}
else if ($confirm_password==""){
echo 'please fill in all fields';
die();
}
else if ($count>0){
echo 'user exists';
die();
}
else if ($password!=$confirm_password){
echo 'passwords do not match';
die();
}
else{
$second_query = "INSERT INTO users (username,password) VALUES ('$username', '$password')";
mysql_query($second_query) or die('Error, insert query failed');
}
}
?>
Last edited by scarface222 on Fri Mar 27, 2009 9:01 pm, edited 1 time in total.
If your php code is on the same page then you do not want to be posting your form to that page, you want to redirect to that page when your code is complete. You would want to change the action= to the page your currently on and at the end of your code after all checks are done and the user is logged in, do a redirect. Here is an excerpt from one of my earlier login checks.
I tried changing the action to the current page but for some reason the php code does not execute. Instead the page just stays the same. I know you have the right idea and I am going to try to implicate it. I really appreciate your feedback, however do you know why my php still does not execute? Is the (isset($_POST['submit'])) command correct in an attempt to initiate the php when the form is executed?
The submit button doesn't have a value because your using an image so the php code checking if that value is set is actually working the way it should. Try using something else to test the submit,
Thank you so friggin much guys, this was a pain to overcome, but I am learning. Both of you were right and now the code is working as it should. I tried the post username and that is the correct value. Thanks again guys, really appreciate your help.