Simple bug, from a beginner! ;)

hey, im trying to get the code below to echo error messages if the variables in the top letvel if statements are incorrect, but its only outputting the first else statement 'e.g. ‘please enter your email address’.

Also, my database details are wrong, but the ‘or die’ doesnt seem to do anything or output any error message…

—SCRIPT----

if(isset ($_POST['submit'])){
	
	//check required fields//
	if(!empty($email)){
		if(!empty($event)){
			if(!empty ($description)){
				//grab variables//
				$email=mysqli_real_escape_string($dbc,trim($_POST['email']));
				$event=mysqli_real_escape_string($dbc,trim($_POST['eventname']));
				$datefrom=mysqli_real_escape_string($dbc,trim($_POST['datefrom']));
				$dateto= mysqli_real_escape_string($dbc,trim($_POST['dateto']));
				$description=$$_POST['eventdes'];
				$website=$_POST['website'];
				$industry=$_POST['industry'];
				//connect to database//
				$dbc= mysqli_connect('localhost','asds','aSTY3','moew23eokle');
				$query="INSERT INTO mookle_tables VALUES('$email','$eventname','$date_from','$date_to','$description','$website','$industry')"
				or die('mysql error has ocurred!');
				mysqli_query($dbc,$query);
				echo ('your event is now in our system!');
				}
			else{echo'please enter the description';}
			}
		else{echo'please enter the event name';}
		}
	else{echo'please enter your email address';}
}

Can you show us your form? Make sure that you’re actually getting the data into the script…

If a user doesn’t fill anything in a field the form sends those values as empty strings. Since empty(‘’) returns true you do indeed see the errors.

Try this instead


if (trim($some_var) == '')
{
   // add error
}

That also prevents users from filling in spaces only (as per the trim() function).

thanks for your help! I tried the script, but it outputs all error message even if the fields are not empty…?

Ahh… sorry in my last post i just forgot one line to move up. Please move the following on top of the script (before you get form data in the variables)


$dbc= mysqli_connect('localhost','asds','aSTY3','moew23eokle');

Then it should work well enough.

Er… correction…
You need to get your information BEFORE your ifs - if you dont set $email before you do if(empty($email)), it -will- be empty, because it doesnt exist :stuck_out_tongue:


    $email=mysqli_real_escape_string($dbc,trim($_POST['email']));
    $event=mysqli_real_escape_string($dbc,trim($_POST['eventname']));
    $datefrom=mysqli_real_escape_string($dbc,trim($_POST['datefrom']));
    $dateto= mysqli_real_escape_string($dbc,trim($_POST['dateto']));
    $description = $_POST['eventdes'];
    $website = $_POST['website'];
    $industry = $_POST['industry'];

all that needs to go before you start checking.

And the fix on your own code will be like this i think:


if(isset ($_POST['submit'])){
    //grab variables//
    $email=mysqli_real_escape_string($dbc,trim($_POST['email']));
    $event=mysqli_real_escape_string($dbc,trim($_POST['eventname']));
    $datefrom=mysqli_real_escape_string($dbc,trim($_POST['datefrom']));
    $dateto= mysqli_real_escape_string($dbc,trim($_POST['dateto']));
    $description = $_POST['eventdes'];
    $website = $_POST['website'];
    $industry = $_POST['industry'];
    //check required fields//
    if(!empty($email)){
        if(!empty($event)){
            if(!empty ($description)){
                //connect to database//
                $dbc= mysqli_connect('localhost','asds','aSTY3','moew23eokle');
                $query="INSERT INTO mookle_tables VALUES('$email','$eventname','$date_from','$date_to','$description','$website' ,'$industry')";
                mysqli_query($dbc,$query) or die('mysql error has ocurred!');
                echo ('your event is now in our system!');
            }
            else{echo'please enter the description';}
        }
        else{echo'please enter the event name';}
    }
    else{echo'please enter your email address';}
}

All your mysqli escape functions should be placed AFTER a connection has been made (and verified).

You are declaring a variable variable, which I think you missed type it in this situation


$description=$$_POST['eventdes'];

Should just be one dollar sign ($)

$description=$$_POST[‘eventdes’];

Are you trying to do dynamic referencing here? or was this a typo?

Try combining the errors into an array.


$errors = array();
if(empty($email)) {
  $errors[] = "Please fill in an email!";
}
if(empty($event)) {
  $errors[] = "Please fill in an event name!";
}
if(empty($description)) {
  $errors[] = "Please fill in a description!";
}
if(count($errors) == 0) {
  //Do your insert here
} else {
  echo "<b>".implode('<br />',$errors)."</b>";
}

wrt the connection error, you shouldn’t do this


$dbc= mysqli_connect('localhost','123','123','123');
$query="INSERT INTO mookle_tables VALUES('$email','$eventname','$date_from','$date_to','$description','$website' ,'$industry')"
or die('mysql error has ocurred!');

but this:


$dbc= mysqli_connect('localhost','123','123','123') 
or die('mysql error has ocurred!');
$query="INSERT INTO mookle_tables VALUES('$email','$eventname','$date_from','$date_to','$description','$website' ,'$industry')";

Setting a variable never fails (well at least I’ve never seen that and it would be shocking if it did), but mysql_connect can fail, and it will if you provide the wrong credentials, so “or die(…)” should be connected with mysql_connect, and not with setting the variable.