Jump to content

How to send multiple information of the same name?


dwex

Recommended Posts

I have a page that adds name and date of birth of an actor.

 

<input type='text' id='name' name='name' value=''/>
        <input type='text' id='dob' name='dob' value=''/>

 

But I made a loop that enables the user to add multiple actor information at once, so the page will actually look like this. (if you can imagine how it would look on a website with these php codes)

 

<input type='text' id='name' name='name' value=''/>
        <input type='text' id='dob' name='dob' value=''/>

<input type='text' id='name' name='name' value=''/>
        <input type='text' id='dob' name='dob' value=''/>

<input type='text' id='name' name='name' value=''/>
        <input type='text' id='dob' name='dob' value=''/>

<input type="submit" Value="Add"/>

 

How do I send all 3 of these actor information into my saving page instead of just 1 and how do I save all 3 in that saving page?

name them differently, or use arrays

 

<input type='text' id='name' name='name[]' value=''/>
        <input type='text' id='dob' name='dob[]' value=''/>

<input type='text' id='name' name='name[]' value=''/>
        <input type='text' id='dob' name='dob[]' value=''/>

<input type='text' id='name' name='name[]' value=''/>
        <input type='text' id='dob' name='dob[]' value=''/>

 

i suggest that you not use names that are the same as existing form keys or values, for instance i wouldn't use "name" as the name of a field.

 

thx for the reply.

 

how do I save all of them up? 

 

Update page below.

 

<?php

$type = $_POST['tabledrop'];
$item = $_POST['name'];
$price = $_POST['price'];
$des = $_POST['description'];
$stk = $_POST['stock'];
$file = $_FILES['pbimage']['name'];

$target_path = "C:/xampp/htdocs/paintball/itemimages/";
			$target_path = $target_path . basename( $_FILES['pbimage']['name']); 

			if(move_uploaded_file($_FILES['pbimage']['tmp_name'], $target_path)) {
   				 echo "The file ".  basename( $_FILES['pbimage']['name']). 
    			" has been uploaded/";

			}

$query = "INSERT into $type SET {$type}_name = '$item' , {$type}_price = '$price' , {$type}_image = '$file', {$type}_description = '$des' , {$type}_stock = '$stk' , {$type}_time = NOW()" ;

$result = mysqli_query($link, $query) or die(mysqli_error($link));

mysqli_close($link);

?>

 

Sorry , But I'm not sure how do I go about using the foreach loop for this.

 

$name = $_POST['name'];
$price = $_POST['price'];

$query = "INSERT into aaa SET name = '$name' , price = '$price' " ;

$result = mysqli_query($link, $query) or die(mysqli_error($link));

I think this is what you want. It will iterate through the posted values of $name and enter them one by one in the db.

 

foreach ($name as $key => $value) {
     $query = "INSERT into aaa SET name = '$key' , price = '$value' " ;
     $result = mysqli_query($link, $query) or die(mysqli_error($link));
}

 

 

It would be better to use the standard INSERT INTO table (field1, field2) VALUES ('value1', 'value2') syntax, and form the query string in a loop, rather than execute the query in a loop.

 

Tested, and produces the values expected as long as 'name' is supposed to be a string and 'price' is supposed to be an integer. Will echo an error if the number of values in each array is not the same, also. Uncomment the three lines I have commented out to see the results.

// $_POST['name'] = range('a', 'k');
// $_POST['price'] = range(200, 210);
if( count($_POST['name']) === count($_POST['price']) ) {
$name = $_POST['name'];
$price = $_POST['price'];
$q_string = array();
for( $i = 0; $i < count($name); $i++ ) {
	$q_string[] = "( '" . mysqli_real_escape_string( $link, $name[$i]) . "', " . (int)$price[$i] . " )";
}
$query = "INSERT into aaa (`name`, `price`) VALUES " . implode(', ', $q_string);
//	echo "<br>$query";
} else {
echo "<br>Value count mismatched.";
}

Archived

This topic is now archived and is closed to further replies.

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