Jump to content

Recommended Posts

I have the form / script below that works to add multiple rows of data with one query. One thing I did not think about is what if the first set of form fields are filled and the second is not. Currently the script insert an empty row.

 

I'm sure how I would go about checking for an empty row and if there is an empty row ignore it and do not put it in the DB.  Can someone point me in the right direction?

 

<?php
// Begin the script for this page
if (isset($_POST['submit'])) {

//Assign each array to a variable
$id = $_POST['id'];
$store = $_POST['store'];
$item = $_POST['item'];
$itemprice = $_POST['itemprice'];
$itemnumber = $_POST['itemnumber'];
$couponvalue = $_POST['couponvalue'];
$couponsused = $_POST['couponsused'];
$limit = count($id);
$values = array(); // initialize an empty array to hold the values

	for($i=0;$i<$limit;$i++){
			$store[$i] = check_input($store[$i]);
			$item[$i] = check_input($item[$i]);
			$itemprice[$i] = check_input($itemprice[$i]);
			$itemnumber[$i] = check_input($itemnumber[$i]);
			$couponvalue[$i] = check_input($couponvalue[$i]);
			$couponsused[$i] = check_input($couponsused[$i]);

			$values[$i] = "( '{$id[$i]}', '{$store[$i]}', '{$item[$i]}', '{$itemprice[$i]}', '{$itemnumber[$i]}', '{$couponvalue[$i]}', '{$couponsused[$i]}')"; // build the array of values for the query string
	}
			$query = "INSERT INTO `item` (user_id, store, item, itemprice, itemnumber, couponvalue, couponsused) VALUES " . implode( ', ', $values ); // Form the query string and add the implod()ed values



	if (!mysql_query($query,$link)){
		die('Error: ' . mysql_error());
	} else {
		$added = "Your items have been added.";
	}
}
?>


<div class="pageContent">
    <div id="main">
	<div class="container">
		<?php
		echo $added;
			if($_SESSION['id']){
			echo '<form action="" method="post">';
				echo '<table cellpadding= "4">';
					echo '<tr>';
						echo '<input type="hidden" name="id[]" id="id[]" value='.$_SESSION['id'].' />';
						echo '<td><DIV CLASS="p2"><b>Store</b></div>';
						echo '<input type="text" name="store[]" id="store[]" size="15" maxlength="255"/></td>';
						echo '<td><DIV CLASS="p2"><b>Item</b></div>';
						echo '<input type="text" name="item[]" id="item[]" size="15" maxlength="255"/></td>';
						echo '<td><DIV CLASS="p2"><b>Item Price</b></div>';
						echo '<input type="text" name="itemprice[]" id="itemprice[]" size="5" maxlength="255"/></td>';
						echo '<td><DIV CLASS="p2"><b>Item #</b></div>';
						echo '<select name="itemnumber[]" id="itemnumber[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>';
						echo '<td><DIV CLASS="p2"><b>Coupon Value</b></div>';
						echo '<input type="text" name="couponvalue[]" id="couponvalue[]" size="10" maxlength="255"/></td>';
						echo '<td><DIV CLASS="p2"><b># Coupons</b></div>';
						echo '<select style="width: 60px;" name="couponsused[]" id="couponsused[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>';
						echo '<td><DIV CLASS="p2"><b>Doubled Coupon</b></div>';
						echo '<select style="width: 75px;" name="doubledcoupon[]" id="doubledcoupon[]"><option value="1">Yes</option><option value="0">No</option></td>';
					echo '</tr>';
					echo '<tr>';
						echo '<input type="hidden" name="id[]" id="id[]" value="'.$_SESSION['id'].'" />';
						echo '<td><input type="text" name="store[]" id="store[]" size="15" maxlength="255"/></td>';
						echo '<td><input type="text" name="item[]" id="item[]" size="15" maxlength="255"/></td>';
						echo '<td><input type="text" name="itemprice[]" id="itemprice[]" size="5" maxlength="255"/></td>';
						echo '<td><select name="itemnumber[]" id="itemnumber[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>';
						echo '<td><input type="text" name="couponvalue[]" id="couponvalue[]" size="10" maxlength="255"/></td>';
						echo '<td><select style="width: 60px;" name="couponsused[]" id="couponsused[]"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option></select></td>';
						echo '<td><select style="width: 75px;" name="doubledcoupon[]" id="doubledcoupon[]"><option value="1">Yes</option><option value="0">No</option></td>';
					echo'</tr>';
					echo'<tr>';
						echo '<input type="submit" name="submit" value="Submit Item">';
					echo'</tr>';

				echo '</table>';
				echo'</form>';
			} else {
			echo '<h1>Please, <a href="index.php">login</a> and come back later!</h1>';
			}
		?>		
	</div>
</div>
</div>

Link to comment
https://forums.phpfreaks.com/topic/235147-check-for-empty-array/
Share on other sites

guilygear,

 

Tried both of those. Maybe I'm not articulating what I'm trying to do.

 

I have an HTML form with say 10 rows each with the following user filled form fields store, item, itemprice, itemnumber, couponvalue, couponsused, doubledcoupon. One row of fields is an item and inserted into the DB in the item table. Well, what if a user only has 5 items to enter in the DB? In this case I would need the script to evaluate the 5 rows of data and ignore the last five that have nothing in them. That is the human logic behind what I need to do.

 

Both the scripts above will still insert empty fields in the DB which obviously is a bad thing. 

You need to check if the fields are not empty within the for loop when building the query.

				if(!empty($store[$i]) && !empty($item[$i]) && !empty($itemprice[$i]))
			    $values[$i] = "( '{$id[$i]}', '{$store[$i]}', '{$item[$i]}', '{$itemprice[$i]}', '{$itemnumber[$i]}', '{$couponvalue[$i]}', '{$couponsused[$i]}')"; // build the array of values for the query string

That if statement will check to make sure the fields store, item, and price have not been left empty. It'll only add the rows that have these three fields filled out.

Thanks for the reply wildteen.

 

My rows look like this (store, item, itemprice, itemnumber, etc...), I think what I want to do is make the store required and if a value for store is not entered then drop the corresponding (item, itemprice, itemnumber, etc...) associated with that row.

 

The if statement above will just check the individual form fields.

 

 

Change the if statement to just

				if(!empty($store[$i]))
			    $values[$i] = "( '{$id[$i]}', '{$store[$i]}', '{$item[$i]}', '{$itemprice[$i]}', '{$itemnumber[$i]}', '{$couponvalue[$i]}', '{$couponsused[$i]}')"; // build the array of values for the query string

That will make just the store field require. The others will be optional.

 

As your table stands you have 7 fields in your table (user_id, store, item, itemprice, itemnumber, couponvalue, couponsused). Each of those fields will require a value of some sort (be it an empty value or what is entered in your form). If you only want the store name inserting into one row, then on the next row you add in the item, itemprice, itemnumber, couponvalue, couponsused values then that is bad database design.

 

A better way would be to enter all your stores within your stores table, then just have the store id of the store that the product belongs to within your item table.

This is my DB design.

 

CREATE TABLE `item` (
  `item_id` int(11) NOT NULL auto_increment,
  `user_id` int(20) NOT NULL,
  `store` varchar(30) collate latin1_general_ci NOT NULL,
  `item` varchar(30) collate latin1_general_ci NOT NULL,
  `itemprice` decimal(10,2) unsigned NOT NULL,
  `itemnumber` tinyint(20) NOT NULL,
  `couponvalue` decimal(10,2) unsigned NOT NULL,
  `couponsused` tinyint(20) NOT NULL,
  `doubledcoupon` tinyint(2) NOT NULL,
  PRIMARY KEY  (`item_id`)
) ENGINE=MyISAM AUTO_INCREMENT=50 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=50 ;

 

I need a way to identify that a user has not entered a store in my multi row html. If the user did not select a store the content of the other form fields in that particular row or is that even possible? Can I only evaluate the form fields individually? I guess I really need to evaluate them as a set for each row.

This is what I did to get it to work.

 

if(empty($store[$i])){
		} else {
			$values[$i] = "( '{$id[$i]}', '{$store[$i]}', '{$item[$i]}', '{$itemprice[$i]}', '{$itemnumber[$i]}', '{$couponvalue[$i]}', '{$couponsused[$i]}', '{$doubledcoupon[$i]}')";
		}

If the store field is not set then drop that row, if it is move on to the array.

 

Thanks for all the help.

That is what my original code did! Did you even try my code example?

if(!empty($store[$i]))
			    $values[$i] = "( '{$id[$i]}', '{$store[$i]}', '{$item[$i]}', '{$itemprice[$i]}', '{$itemnumber[$i]}', '{$couponvalue[$i]}', '{$couponsused[$i]}')"; // build the array of values for the query string

 

The ! symbol means NOT. The if statment means this is. If the store field for the current row IS NOT empty them add the store, item, price etc into the database.

I did try the original example. It was still inserting empty rows, it would only eval the first row correctly. If the first row was not set and the second was it would not insert either.

 

What I used I look for empties and if it finds one do nothing and move to the next row, else insert into the DB.

 

It seems to work.

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.