Jump to content

Query to get data to show properly


kenwvs

Recommended Posts

I am trying to create some code so that the data inputted into a form will end up in a database, but I am running into some problems that are related to my php code.

Here are the problems. 

1.  Everytime I run the script it puts one empty row in the DB and also puts a row with the data in it.

2.  I have seven fields for the parts number and description, but thought I had the code correct so that it would check for an empty field and if it was empty it wouldn't put the empty row in the db, but it puts in empty rows, up to the total of 7 rows.  Could be 4 rows of data and 3 empty, etc.

The other issues are database related and will post those in the database section.

Here is the code I am using

[code]<?php
if (!isset($_POST['Submit']))
{

?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1572864">
<center>
[/code]

All of the form fields go here and are working properly.

this is the code for the parts fields
<div><center>
<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[]"
</div><BR>

[code]
else
{
$work = $_POST["work"];
$sched = $_POST["sched"];
$name = $_POST["tech"];
$site = $_POST["site"];
$serial = $_POST["serial"];
$hours = $_POST["hours"];
$starts = $_POST["starts"];
$issue = $_POST["issue"];
$severity = $_POST["severe"];
$resolution = $_POST["resolve"];
$assistance = $_POST["assist"];
$safety = $_POST["safe"];
    $number = $_POST["number"];
    $description = $_POST["description"];
);

{
if (!empty($_POST['number']))
  foreach($_POST['description'] as $key=>$description) {
  $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);
}

mysql_query ("Insert into `workorder`(work, sched, site, serial, hours,
starts, issue, severity, resolution, assistance, safety) VALUES
('$work', '$sched', '$site', '$serial', '$hours', '$starts',
'$issue', '$severity', '$resolution', '$assistance', '$safety')")or die(mysql_error());

mysql_query ("Insert into `employees` (name) VALUES ('$name')")or die(mysql_error());

echo "Work Order # '$work' has been Successfully Added to the System";

}
[/code]


Link to comment
Share on other sites

Hi!

Your array probably looks like this:

[code]$_POST['description'] = array(
  'part1',
  'part2',
  '',
  '',
  '',
  '',
  '',
);[/code]

So, all you need to do in that foreach loop is to check "if ($description == '') continue;"

The "continue" will skip to the next element in the array used by the foreach.  Alternatively, you can use "break" if you want to end the foreach immediately.

To verify this, try adding the code "print_r($_POST['description'])" or "var_dump($_POST['description'])".  Then you can see what the array contents are, and work out how to deal with those empty items.

Good luck :)
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.