Jump to content

Strange validation behaviour php


codeblock

Recommended Posts

Hello all,

This is my first post. I have a set of IF ELSE validation code ECHOING onto a php web page through a SWITCH statement. 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. The validation stops.

 

<?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");
		      
}
else if (empty($_POST['engine'])){                  		
header("Location: " . $config_basedir . "/newcar.php?error=engine"); 
	     
}  
else if ($_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;     
}	 
} 
?>  
<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>

Link to comment
https://forums.phpfreaks.com/topic/193234-strange-validation-behaviour-php/
Share on other sites

ok I think I know what you mean now.

 

The reason is you check the other form variables after the db query code block. I think you want something more like this:

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

if (empty($_POST['model'])) 
{
header("Location: " . $config_basedir . "/newcar.php?error=model");
		      
}
else if (empty($_POST['engine'])){                  		
header("Location: " . $config_basedir . "/newcar.php?error=engine"); 
	     
}  
else if ($_POST['colour']){                  		
header("Location: " . $config_basedir . "/newcar.php?error=colour"); 
	     
}  elseif($validdate == false){
header("Location: " . $config_basedir . "/newcar.php?error=date");
  
}  
elseif($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); 
}   
} 

 

I tried this, using the code you posted, but i just got an error message.

Parse error: syntax error, unexpected ';' on line 18

 

<?php

 

session_start();

 

require("config.php");

require("functions.php");

 

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

mysql_select_db($dbdatabase, $db);

 

$propertytype = $_POST['model'];

$age = $_POST['engine']; 

$grade  = $_POST['colour'];

 

 

if(isset($_POST['submitted']))

{

$validdate = checkdate($_POST['month'], $_POST['day'], $_POST['year'];

)

if (empty($_POST['model']))

{

header("Location: " . $config_basedir . "/newcar.php?error=model");      

}

else if (empty($_POST['engine']))

{

header("Location: " . $config_basedir . "/newcar.php?error=engine");

else if ($_POST['colour'])

{

header("Location: " . $config_basedir . "/newcar.php?error=colour");

else if (empty($_POST['startingprice']))

{

  header("Location: " . $config_basedir . "/newcar.php?error=startingprice");

}

elseif($validdate == false)

{

header("Location: " . $config_basedir . "/newcar.php?error=date"); 

elseif($validdate == TRUE)

{

$concatdate = $_POST['year']. "-" . sprintf("%02d", $_POST['month']). "-" . sprintf("%02d", $_POST['day']) 

. " " . $_POST['hour']

. ":" . $_POST['minute']

. ":00";

 

$itemsql = "INSERT INTO items(user_id,model,engine,colour,dateends) VALUES(". $_SESSION['USERID']. ",'" . addslashes($_POST['model']) . "', " . $_POST['engine'] . ", '" . addslashes($_POST['colour']) . "'," . $_POST['startingprice'] . ", '" . $concatdate . "');";

mysql_query($itemsql);

$itemid = mysql_insert_id(); 

 

  }   

 

}

else {

require("header.php");

?>

<table  width="447"class="abc"> 

<strong>Step 1- Add your details. </strong>

</table>

<?php

switch($_GET['error']) {               

case "date":

echo "<strong>Invalid date - please choose another!</strong>";

break;

case "model":

echo "<strong>Invalid model - please enter the model!</strong>";

break;

case "engine":

echo "<strong>Invalid engine - please enter the engine!</strong>";

break;

case "colour":

echo "<strong>Invalid colour - please enter the colour!</strong>";

break;

 

}

?>

  </p>

<form action="<?php echo pf_script_with_get($SCRIPT_NAME); ?>" method="post">

<table width="360" class="table2">

 

<tr>

<td width="190">model</td>

<td><input type="text" name="model" id="model"value="model" onfocus="clearMe(this)"></td>

            </tr>

            <tr>

<td>engine</td>

<td><input type="text" name="engine" id="engine"value="engine" onfocus="clearMe(this)"></td>

            </tr>

            <tr>

  <td>colour</td>

  <td><input type="text" name="colour" id="colour"value="colour" onfocus="clearMe(this)"></td>

            </tr>

            <tr>

</table>

 

<table  width="347" class="table37">

        <tr>

      <td width="180">Closing date</td>

      <td>

<table >

<tr >

<td>Day</td>

<td>Month</td>

<td>Year</td>

<td>Hour</td>

<td>Minute</td>

</tr>

<tr>

<td>

<select name="day">

<?php

for($i=1;$i<=31;$i++) {

echo "<option>" . $i . "</option>";

}

?>

</select>

</td>

<td>

<select name="month">

<?php

for($i=1;$i<=12;$i++) {

echo "<option>" . $i . "</option>";

}

?>

</select>

</td>

<td>

<select name="year">

<?php

for($i=2010;$i<=2020;$i++) {

echo "<option>" . $i . "</option>";

}

?>

</select>

</td>

<td>

<select name="hour">

<?php

for($i=0;$i<=23;$i++) {

echo "<option>" . sprintf("%02d",$i) . "</option>";

}

?>

</select>

</td>

<td>

<select name="minute">

<?php

for($i=0;$i<=60;$i++) {

echo "<option>" . sprintf("%02d",$i)  . "</option>";

}

?>

</select>

</td>

</tr>

</table>

</td>

</tr>

<tr>

<td>Cost</td>

<td><?php echo $config_currency; ?><input type="text" name="startingprice" id="startingprice"value="000.00"onfocus="clearMe(this)"></td>

</tr>

<tr>

<td></td>

<td><input type="submit" name="submitted" value="Post project!"></td>

</tr>

<?php

}

require("footer4.php");

?>

Archived

This topic is now archived and is closed to further replies.

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