Jump to content


Photo

Ignore Empty Fields


  • Please log in to reply
4 replies to this topic

#1 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 07 September 2006 - 05:25 PM

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:
<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>

This is the code that I have put together.


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);
}


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 September 2006 - 05:34 PM

With the below code:
$_POST['description'] = array(
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    );

	$_POST['number'] = array(
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    );

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.

#3 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 07 September 2006 - 05:52 PM

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

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 September 2006 - 05:56 PM

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[].

#5 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 07 September 2006 - 05:58 PM

That did solve the problem.  I actually thought the descriptions were ok as it is part number and part description.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users