...

View Full Version : Insert a loop... insert only data



brentc73
02-08-2010, 08:23 PM
Hi Guys,

I am pretty new to PHP and need some help.

I have a form with the following fields in my html form...



<form method="post" action="process.php" enctype="multipart/form-data">
<select class="dropper" name="brand[]" onChange="getDescription('findDescription.php?brand='+this.value)">
</select>
<select class="dropper" name="descr[]" onChange="showUser(this.value);">
</select>
<select class="dropper" name="units[]" >
</select>
<select class="dropper" name="cases[]" >
</select>
<input TYPE="submit" name="submit" title="Add Post" value="Add Post"/>


I have my process.php file that inserts the array into the my db with the following code...



<?

$brand = $_POST['brand'];
$descr = $_POST['descr'];
$units = $_POST['units'];
$cases = $_POST['cases'];

$limit = count($brand);

for($i=0;$i<$limit;$i++) {
$brand[$i] = mysql_real_escape_string($brand[$i]);
$descr[$i] = mysql_real_escape_string($descr[$i]);
$units[$i] = mysql_real_escape_string($units[$i]);
$cases[$i] = mysql_real_escape_string($cases[$i]);
$query2 = "INSERT INTO sample_request_items (brand, descr, units, cases, srid) VALUES ('".$brand[$i]."','".$descr[$i]."','".$units[$i]."','".$cases[$i]."','".$srid."')";
mysql_query($query2) or die(mysql_error());
}
?>


Which almost works great, the problem I am having is that it is inserting blank records... Does anyone know how I would be able to tell the script not to insert the blank records?

Any help would be appreciated.

Fumigator
02-08-2010, 09:20 PM
You can check the values you're inserting and if they are empty, don't insert. You can use the empty() function...

http://us3.php.net/manual/en/function.empty.php

But keep in mind with empty(), values of "0" (zero) will return true. So if the value of zero is valid, you'll want to add that as an exception. Or, just check like this: if ($var == "") {}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum