Forum Moderators: coopster
Below is a quote from a previous post you did.
[webmasterworld.com...]
I had just 1 question regarding it (it works perfectly for me...)
How do you get it not to insert if the field is blank?
I'm using this in a custom cart I have and it deals with sizes(identified as 'sub') for the same baseid. It will insert all sizes (SM-3X) including the ones where the qty was either blank or 0.
How can one 'exclude' blank or 0 fields from being inserted?
dmmh wrote====
my approach would be slightly different. not better per se, but different. I usually count the number of elements submitted first.
I assume at least one or more of those text fields have to be filled out, so not including form validation you could do something like this (assuming here 'medid' text field must be filled out):$size_array = count($_POST['medid']);//count the nr of times this field occurs in the array, so we can use this to process with a loop
//now INSERT
for ($i=0; $i<$size_array; $i++){
$query = 'INSERT into table'.
" values ('', '".mysql_real_escape_string($_POST['medid'][$i])."', '".mysql_real_escape_string($_POST['medname'][$i])."', '".mysql_real_escape_string($_POST['qty'][$i])."', '".mysql_real_escape_string($_POST['pilltype'][$i])."', '".mysql_real_escape_string($_POST['priceperpill'][$i])."', '".mysql_real_escape_string($_POST['price'][$i])."', '".mysql_real_escape_string($_POST['instructions'][$i])."')";
$result = mysql_query($query) or die ("Error in query: $query");
}