Hello all,
I have a set of IF ELSE validation code ECHOING onto a php web page through a SWITCH staement. Everything works fine all error messages show up correctly. The problem is, after going through 3 conditons it starts to allow entry into my DB(mysql) all other entries goes straight through and stops validating.

if (empty($_POST[model]))
{                  		
  header("Location: " . $config_basedir . "/newcar.php?error=model"); 		     
}
if (empty($_POST[engine]))
{                  		
  header("Location: " . $config_basedir . "/newcar.php?error=engine"); 		     
}
if (empty($_POST[colour]))
{                  		
  header("Location: " . $config_basedir . "/newcar.php?error=colour"); 		     
}
else {
  header("Location: " . $config_basedir . "/newcar.php?error=date");
	       
	    }      
}
else { 
	require("header.php");     
              
?>  
       
<?php
	            
switch($_GET['error']) {
case "date":
         echo "<strong>Invalid date - please enter another!</strong>";
       break;
case "model":   
       echo "<strong>Please provide the vehicle model!</strong>";
      break;
case "engine":
     echo "<strong>Please provide engine type!</strong>";
      break;
case "colour":
     echo "<strong>Please provide colour of vehicle!</strong>";
      break;
		
    }	
			
	
}

?>

Can someone help please it would be much appreciated, this has never happened to me before. is it common?

Thanks

Dani AI

Generated

Brief diagnosis: the script inserts the row before running the required-field checks, and redirects are not consistently terminating execution. was right that calls to header() should be followed by exit; ’s note about mismatched if/else logic is also relevant — the posted flow validates the date first, performs the INSERT, then runs the empty-field checks, so a valid date allows an insert even when required fields are missing.

Concrete issues visible in the snippet

  • Validation order reversed: required-field checks must run before any INSERT.
  • Wrong condition: else if ($_POST['colour']) tests truthiness, not emptiness — use trim(...) === '' or empty(...).
  • INSERT lists four columns but supplies a fifth value (the date) — column/value count must match.
  • addslashes is inconsistent and unsafe; prepared statements (mysqli or PDO) avoid injection and quoting errors.
  • header() without exit() lets the script continue; multiple header() calls can conflict.

Example validation-first pattern (illustrative):

$errors = [];
if (trim($_POST['model']) === '') $errors[] = 'model';
if (trim($_POST['engine']) === '') $errors[] = 'engine';
if (trim($_POST['colour']) === '') $errors[] = 'colour';

if ($errors) {
    header('Location: '.$config_basedir.'/newcar.php?error='.$errors[0]);
    exit;
}

if (!checkdate((int)$_POST['month'], (int)$_POST['day'], (int)$_POST['year'])) {
    header('Location: '.$config_basedir.'/newcar.php?error=date');
    exit;
}

// build date and perform INSERT with a prepared statement so columns/values match

Quick troubleshooting checklist: ensure the form actually sends the named submit field (isset($_POST['submitted'])), trim inputs before testing, confirm the INSERT column list matches value count (add the date column if intended), enable full error reporting during debug, and use prepared statements rather than addslashes. These changes address the behaviour that allowed some submissions to reach the DB despite reported validation errors.

Recommended Answers

All 6 Replies

Hello all,
I have a set of IF ELSE validation code ECHOING onto a php web page through a SWITCH staement. Everything works fine all error messages show up correctly. The problem is, after going through 3 conditons it starts to allow entry into my DB(mysql) all other entries goes straight through and stops validating.

Can someone help please it would be much appreciated, this has never happened to me before. is it common?

Thanks

After each header("Location:") command you need exit, otherwise the script keeps running.

E.g.:

if (empty($_POST[model]))
{                  		
  header("Location: " . $config_basedir . "/newcar.php?error=model"); 		     
  exit;
}
if (empty($_POST[engine]))
{                  		
  header("Location: " . $config_basedir . "/newcar.php?error=engine"); 		     
  exit;
}
if (empty($_POST[colour]))
{                  		
  header("Location: " . $config_basedir . "/newcar.php?error=colour"); 		     
  exit;
}
else {
  header("Location: " . $config_basedir . "/newcar.php?error=date");
  exit;
// etc. ......

if and else are not matching .may i see your fullpage code.

Please provide the full code for understanding your problem better

After each header("Location:") command you need exit, otherwise the script keeps running.

E.g.:

if (empty($_POST[model]))
{                  		
  header("Location: " . $config_basedir . "/newcar.php?error=model"); 		     
  exit;
}
if (empty($_POST[engine]))
{                  		
  header("Location: " . $config_basedir . "/newcar.php?error=engine"); 		     
  exit;
}
if (empty($_POST[colour]))
{                  		
  header("Location: " . $config_basedir . "/newcar.php?error=colour"); 		     
  exit;
}
else {
  header("Location: " . $config_basedir . "/newcar.php?error=date");
  exit;
// etc. ......

Adding the exit doesn't seem to work.

if and else are not matching .may i see your fullpage code.

I can't post all of the code because it is too long. I will post a bit more.

Please provide the full code for understanding your problem better

This is as much as i can post besause the code it too long.

<?php

session_start();

$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);

$model =trim($_POST['model']);   
$engine =trim($_POST['engine']);      
$colour =trim($_POST['colour']);


if(isset($_POST['submitted'])) {
$validdate = checkdate($_POST['month'], $_POST['day'], $_POST['year']); 

if($validdate == TRUE) {
$concatdate = $_POST['year']
. "-" . sprintf("%02d", $_POST['month'])
. "-" . sprintf("%02d", $_POST['day'])  			
. " " . $_POST['hour']
. ":" . $_POST['minute']
. ":00";
   
$vehiclesql = "INSERT INTO vehicle(user_id,model,engine,colour) VALUES(". $_SESSION['USERID']. ",'" . addslashes($_POST['model']). "', '" . $_POST['engine'] . "', '" . addslashes($_POST['colour']) . "','" . $concatdate	. "');";
mysql_query($vehiclesql);
$vehicleid = mysql_insert_id();
header("Location: " . $config_basedir . "/addimage.php?id=" . $vehicleid);
 
}
	      
      
 if (empty($_POST['model']))
  {                  		
header("Location: " . $config_basedir . "/newcar.php?error=model"); 
exit;		     
 }
else if (empty($_POST['engine']))
{                  		
header("Location: " . $config_basedir . "/newcar.php?error=engine"); 
exit;		     
 }  
else if ($_POST['colour'])
{                  		
header("Location: " . $config_basedir . "/newcar.php?error=colour"); 
exit;		     
}  
else{
header("Location: " . $config_basedir . "/newcar.php?error=date");
exit;
     
   }  
          
}

else { 
	require("header.php");     
              
?>  
       
<?php
	            
switch($_GET['error']) {
case "date":
         echo "<strong>Invalid date - please enter another!</strong>";
       break;
case "model":   
       echo "<strong>Please provide the vehicle model!</strong>";
      break;
case "engine":
     echo "<strong>Please provide engine type!</strong>";
      break;
case "colour":
     echo "<strong>Please provide colour of vehicle!</strong>";
      break;
		
    }	
			
	
}

?>
 

<table width="360" class="vehicle">
<tr>
<td width="190">model</td>
<td><input type="text" name="model" id="modelvehicle"value="TVR" onfocus="clearMe(this)"></td>
  </tr>
<tr>
<td>engine</td>
<td><input type="text" name="engine" id="enginevehicle"value="Rover V8" onfocus="clearMe(this)"></td>
  </tr>
<tr>
<td>coloure</td>
<td><input type="text" name="colour" id="colourvehicle"value="Black" onfocus="clearMe(this)"></td>
  </tr>

</table>

<?php

require("footer.php");

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.