We help IT Professionals succeed at work.

multiple SQL insertion & form validation In php

I am trying to do multiple product add with a form using php and mysql , and i am confusing on the concept of doing these,

My expecting output is , provide a form with at least ten rows of multiple field to be fill and do the validation among these , and proceed to insertion if no error .

Here is what i understand about single form insertion so far:
$add_errors = array();

//if there is a post request
if ($_SERVER['REQUEST_METHOD'] == 'POST') {	

// do some validation
if (empty($_POST['name'])) {
		$add_errors['name'] = 'Please enter the name!';
}
	
if (empty($_POST['description'])) {
		$add_errors['description'] = 'Please enter the description!';
}

if (empty($add_errors)) { // If everything's OK.
//do the insertion
$q = 'INSERT INTO ........')';
}

}//end of form submission


echo '<form action="product_add.php" enctype="multipart/form-data" method="post" accept-charset="utf-8" class="niceform">';
echo '<input type=..... name=...... id=.....>';
echo '<input type=..... name=...... id=.....>';
echo '<input type=..... name=...... id=.....>';
echo '</form';
//this form is only a single row with multiple column(field) ,I am trying to make it into multiple column

Open in new window

Comment
Watch Question

Web Application Programmer
Commented:
With PHP you can post set of HTML input elements as an array to the server. To do this you have to add [] to end of the name attribute of the element (<input name="product[]"...). It also support multi dimensional arrays (<input name="product[0][description]"...).
If you leave [] blank, PHP will assign a numeric index to the array element automatically, otherwise the array elements will be assigned the index/key with what ever you enter in the[...] (as an associative array).

So your HTML should look something like this:
<form action="product_add.php" enctype="multipart/form-data" method="post" accept-charset="utf-8" class="niceform">

	<!-- First product -->
	<input name="product[][name]" ... />
	<input name="product[][description]" ... />
	...

	<!-- Second product -->
	<input name="product[][name]" ... />
	<input name="product[][description]" ... />
	...
	
</form>

Open in new window


And handle it in PHP like this:
<?php

//if there is a post request
if ($_SERVER['REQUEST_METHOD'] == 'POST') {     

	$add_errors = array();

	$product_list = $_POST['product'];
	//$product_list is our array posted from the HTML form
	//print_r($product_list); //See what's in our array

	if(!empty ($product_list) && is_array($product_list)) { //Make sure we have a valid array

		foreach ($product_list as $product) {

			// do some validation
			if (empty($product['name'])) {
							$add_errors['name'] = 'Please enter the name!';
			}
					
			if (empty($product['description'])) {
							$add_errors['description'] = 'Please enter the description!';
			}
		}
	}

	if (empty($add_errors)) { // If everything's OK.
	//do the insertion
	$q = 'INSERT INTO ........')';
	}

}//end of form submission

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Do you need enctype="multipart/form-data"?  If not, I would omit it.

You can test this script on my server here.
http://www.laprbass.com/RAY_temp_nazart.php

Instead of hard-coding the form with a heredoc, you might consider generating it in a PHP string, something like this:

// GENERATE THE FORM DYNAMICALLY
$num  = 0;
$form = '<form method="post">'
while ($num < 3)
{
    $str = <<<END
Name<input name="product[$num][name]" />
Desc<input name="product[$num][desc]" />
<br/>
END;
    $form .= $str;
    $num++;
}
$form .= '<input type="submit" /></form>';

echo $form;

HTH, ~Ray
<?php // RAY_temp_nazart.php
error_reporting(E_ALL);

// IF ANYTHING WAS POSTED
if (!empty($_POST))
{
    // SHOW THE POST ARRAY
    echo "<pre>";
    var_dump($_POST);
    echo "</pre>";

    // SHOW HOW TO PROCESS IT
    foreach ($_POST["product"] as $arr)
    {
        echo "<br/>{$arr["name"]} MATCHES {$arr["desc"]}" . PHP_EOL;
    }
}

// CREATE THE FORM USING HEREDOC NOTATION
$form = <<<FORM
<form method="post">

<!-- 1ST -->
Name<input name="product[0][name]" />
Desc<input name="product[0][desc]" />
<br/>

<!-- 2ND -->
Name<input name="product[1][name]" />
Desc<input name="product[1][desc]" />
<br/>

<!-- 3RD -->
Name<input name="product[2][name]" />
Desc<input name="product[2][desc]" />
<br/>

<input type="submit" />
</form>
FORM;

echo $form;

Open in new window