Jump to content

Ignore Empty Fields


kenwvs

Recommended Posts

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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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