Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Record inserting into database even with form validation in place for empty fields

Posted on 2016-08-28
7
Medium Priority
?
73 Views
Last Modified: 2016-08-28
If I run this code and leave the password blank it will give an error and not insert anything. If I enter a password and leave the email address field empty, it still submits into the database (empty record in email column) but still shows me the error on the page that I must enter an email address. I can't figure out why this is happening. Also, the page does not redirect if signup is successful.

$error = "";
$success = "";

if (isset($_POST['submit'])) {
	
	if (!$_POST['email']) {
		
		$error .="Your email address is required<br>";
	}
	
	if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {

            $error .= "The email address is invalid.<br>";

        }
	
	if (!$_POST['password']) {
		
		$error .="Please enter a password<br>";
		
		
	} 
	
	else {
			
		
	$sql = "SELECT email FROM `users` WHERE email = '".$link->real_escape_string($_POST['email'])."' LIMIT 1";
		
			$result = $link->query($sql);
		
		if($result->num_rows > 0) {

			$error .= "The email, " .$_POST['email']. " ,is already taken";
			
		} else {
			
			$stmt = $link->prepare("INSERT INTO users (email, password) VALUES (?, ?)");
			$stmt->bind_param("ss", $email, $password);
			$email = htmlentities($_POST['email'], ENT_QUOTES);
			$password = trim(password_hash($password, PASSWORD_BCRYPT, [12]));
			$stmt->execute();
			$stmt->close();
                        header:("location: welcome.php");
		}
		
	}

}

Open in new window

0
Comment
Question by:Black Sulfur
7 Comments
 
LVL 9

Accepted Solution

by:
Mukesh Yadav earned 2000 total points
ID: 41773512
Try this ;)

Instead of else use if condition to check for errors

So, replace
} else {

Open in new window

with
if(empty($error)){

Open in new window

0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41773513
Excellent, that worked! What about the redirect though? It still doesn't redirect.
0
 
LVL 30

Expert Comment

by:Olaf Doschke
ID: 41773520
Try the full url. See https://en.wikipedia.org/wiki/HTTP_location 
It's true, that the updated HTTP 1.1 specifications...allow(ing) the use of relative URLs in Location headers, but it can't hurt to specify the full url.

Also, do you see errors in error log? Your code does not output anything, which is fine, but if there is anything, even just a space outside of <?php ?> before you send a header, you get an error.

Also things output afterwards may hinder it to work. Put an exit; after sending the header.

Bye, Olaf.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Author Comment

by:Black Sulfur
ID: 41773524
Hi Olaf,

Using exit keeps the url at register.php so it doesn't redirect but the page becomes blank. I get no errors as far as I can see. if I use the full URL (I am running on localhost) it still doesn't redirect.

Do you have a suggestion or method you would use to check for errors just in case I am missing something?
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41773599
never mind, I see what I did wrong.

I had header:("

instead of header(

:)
0
 
LVL 30

Expert Comment

by:Olaf Doschke
ID: 41773601
OK, I didn't spot that, too. It surely caused an error. You might have error display off, if this is your productive website, for development every error should be reported.

Bye, Olaf.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41773795
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question