kenwvs Posted September 7, 2006 Share Posted September 7, 2006 I have a form that has seven fields for part numbers and seven fields for descriptions. In some cases all seven fields will be filled in, and in other cases, some of the fields will be empty. I need to create some code so that if the part number and description fields are empty, they get ignored. In the end, I don't want empty rows being added to the database table.here is what I have, and it is putting seven rows in the database. It is also not putting the form field data in the DB. In the number field it is just putting a zero and in the description field it is putting description[]Here is what I have, and I would appreciate any help in putting together some code to eliminate the empty fields from posting.This is the form fields:[code]<input type="text" size="10" maxlength="10" name="number[]"> <input type="text" size="93" maxlength="93" name="description[]"><BR> <input type="text" size="10" maxlength="10" name="number[]"> <input type="text" size="93" maxlength="93" name="description[]"><BR> <input type="text" size="10" maxlength="10" name="number[]"> <input type="text" size="93" maxlength="93" name="description[]"><BR> <input type="text" size="10" maxlength="10" name="number[]"> <input type="text" size="93" maxlength="93" name="description[]"><BR> <input type="text" size="10" maxlength="10" name="number[]"> <input type="text" size="93" maxlength="93" name="description[]"><BR> <input type="text" size="10" maxlength="10" name="number[]"> <input type="text" size="93" maxlength="93" name="description[]"><BR> <input type="text" size="10" maxlength="10" name="number[]"> <input type="text" size="93" maxlength="93" name="description[]"><BR>[/code]This is the code that I have put together.[code]if (!isset($_POST['Submit'])) {$_POST['description'] = array( 'description[]', 'description[]', 'description[]', 'description[]', 'description[]', 'description[]', 'description[]', ); $_POST['number'] = array( 'number[]', 'number[]', 'number[]', 'number[]', 'number[]', 'number[]', 'number[]', );{ if (!empty($_POST['number'])) foreach($_POST['description'] as $key=>$description) { if ($description == '') continue; $description_esc = mysql_real_escape_string($description); $number = intval($_POST['number'][$key]); $query = "INSERT INTO parts (description,number) VALUES (" . "'{$description_esc}', " . "{$number} )"; mysql_query($query) or die(mysql_error().$query);}[/code] Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 7, 2006 Share Posted September 7, 2006 With the below code:[code=php:0]$_POST['description'] = array( 'description[]', 'description[]', 'description[]', 'description[]', 'description[]', 'description[]', 'description[]', ); $_POST['number'] = array( 'number[]', 'number[]', 'number[]', 'number[]', 'number[]', 'number[]', 'number[]', );[/code]You are crearting a new array called _POST['description'], which holds the string 'description[]' seven times in the array, and thus you are getting decription[] added to the database. The same applies $_POST['number'].With the code above you are overwriting the POST'd data being sent from the form. Quote Link to comment Share on other sites More sharing options...
kenwvs Posted September 7, 2006 Author Share Posted September 7, 2006 so, if I understand correctly, if I remove those two arrays, it will post the proper data. But I was trying it without them, and it would still post empty rows. Am I missing something to get it to not post the empty lines Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 7, 2006 Share Posted September 7, 2006 If a form is field is empty it will not create a post var for that form field. Also you should try to name your fileds properly by giving them names more discriptively rather than just description[] and number[]. Quote Link to comment Share on other sites More sharing options...
kenwvs Posted September 7, 2006 Author Share Posted September 7, 2006 That did solve the problem. I actually thought the descriptions were ok as it is part number and part description. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.