Jump to content


Photo

Query to get data to show properly


  • Please log in to reply
1 reply to this topic

#1 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 07 September 2006 - 05:46 AM

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

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

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>

	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";

	}




#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 07 September 2006 - 07:06 AM

Hi!

Your array probably looks like this:

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

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users