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

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.