Jump to content

I swear Im doing everything right but obviously im not, plz revise my code


jdock1

Recommended Posts

Ok I am just quickly coding a registration form.

 

My problem, im using !empty to validate the form. Then use if and else statements to execute further code if all fields are cool. But when I load the page, the errors display. I thought using the code in this form would solve that;

 

<?php
$first = @$_POST['first'];
$last = @$_POST['last'];
$username = @$_POST['username'];
$password = @$_POST['password'];
$db = mysqli_connect('localhost', 'root', '', 'users')
or die('Could Not Connect!');
$query = "INSERT INTO users (first, last, username, password) VALUES ('$first', '$last', '$username', SHA('$password'))";
if (isset($_POST['submit']))
			 {
				 if (!empty($first) || !empty($last) || !empty($username) || !empty($password))
				 {
					 mysqli_query($db, $query)
					 or die('Could Not Query the Database.');
					 echo "Signup complete.";
				 }
			 }
			 else
			 {
				 echo "Error: Not all fields were filled out.";
			 }
?>

 

I have been coding all day & Im sure its probly something small & stupid im missing. But can somebody please tell me wtf I did wrong here?

 

Thanks!!

Link to comment
Share on other sites

Akward....

 

try this:

 

if ((!empty($first)) || (!empty($last)) || (!empty($username)) || (!empty($password)))

Nope! :(

 

I really dont understand this either... it shouldnt be displaying on page load if isset is being used at all

Link to comment
Share on other sites

Im not getting any errors now, just same thing, the server side errors are displaying on page load. When I used your first code I was just getting a parse error on that line. it didnt say what, just said parse error: parse error on line 11

Link to comment
Share on other sites

What does "server side error" mean to you?  Post that error, whatever it is.

 

I meant my custom errors. Ugh I am really losing my mind tonight with all this shit.

 

And I probly should be using and too huh. Im thinking backwards today.

Link to comment
Share on other sites

What does "server side error" mean to you?  Post that error, whatever it is.

 

I meant my custom errors. Ugh I am really losing my mind tonight with all this shit.

 

And I probly should be using and too huh. Im thinking backwards today.

 

No problem, Happens to me too

Link to comment
Share on other sites

You still didn't post which error.  I'm going to assume: "Error: Not all fields were filled out."?

 

In which case that has nothing to do with your $_POST fields and everything to do with your submit button.  That's the way you have your conditions laid out, anyways.

 

Regardless, throw me a bone.  Post the exact error or we you could be here all night.

Link to comment
Share on other sites

I rewrote you code. Take a look at this:

<?php
$db = mysqli_connect('localhost', 'root', '', 'users')
or die('Could Not Connect!');
if (isset($_POST['submit']))
{
$err = false;
foreach ($_POST as $field => $val) {
	switch ($field) {
		case 'first':
		case 'last':
		case 'username':
		case 'password':
		if (strlen(trim($val)) == 0) {
			$err = true;
		} else {
			$$field = $val;
		}
		break;
	}
}
if (!$err) {
	$query = "INSERT INTO users (first, last, username, password) VALUES ('$first', '$last', '$username', SHA($password))";
	mysqli_query($db, $query)
	or die('Could Not Query the Database.');
	echo "Signup complete.";
}	else {
	echo "Error: Not all fields were filled out.";
}
}
?>

 

Ken

Link to comment
Share on other sites

I rewrote you code. Take a look at this:

<?php
$db = mysqli_connect('localhost', 'root', '', 'users')
or die('Could Not Connect!');
if (isset($_POST['submit']))
{
$err = false;
foreach ($_POST as $field => $val) {
	switch ($field) {
		case 'first':
		case 'last':
		case 'username':
		case 'password':
		if (strlen(trim($val)) == 0) {
			$err = true;
		} else {
			$$field = $val;
		}
		break;
	}
}
if (!$err) {
	$query = "INSERT INTO users (first, last, username, password) VALUES ('$first', '$last', '$username', SHA($password))";
	mysqli_query($db, $query)
	or die('Could Not Query the Database.');
	echo "Signup complete.";
}	else {
	echo "Error: Not all fields were filled out.";
}
}
?>

 

Ken

 

Not bad

Link to comment
Share on other sites

I rewrote you code. Take a look at this:

<?php
$db = mysqli_connect('localhost', 'root', '', 'users')
or die('Could Not Connect!');
if (isset($_POST['submit']))
{
$err = false;
foreach ($_POST as $field => $val) {
	switch ($field) {
		case 'first':
		case 'last':
		case 'username':
		case 'password':
		if (strlen(trim($val)) == 0) {
			$err = true;
		} else {
			$$field = $val;
		}
		break;
	}
}
if (!$err) {
	$query = "INSERT INTO users (first, last, username, password) VALUES ('$first', '$last', '$username', SHA($password))";
	mysqli_query($db, $query)
	or die('Could Not Query the Database.');
	echo "Signup complete.";
}	else {
	echo "Error: Not all fields were filled out.";
}
}
?>

Very good, thanks.. I try not to get into loops I am still new at all this. Im taking it one step at a time but the code does make sense to me. I just need to drill all that into my brain before I can code it from scratch. Thanks again.

 

Ken

Link to comment
Share on other sites

Take my advice. Learn about foreach loops and arrays, as well as switch statements. They can make your coding much easier.  Avoid using the "@" to suppress errors and learn how to avoid the errors in the first place.

 

Ken

Link to comment
Share on other sites

I want to be like kenrbnsn someday when I grow up...

 

I hope that's soon since I'm already 41....

 

Gee, thanks.. :)  I won't tell you my age, but let's just say that when I learned PHP (over 10 years ago) I was older than you are now. When I first saw the Internet it was called the ARPAnet and had about 50 computers attached to it, plus I started programming using punch cards...

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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