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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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));
}

 

 

Link to comment
Share on other sites

Sorry, my bad, that was just meant as an example. Should have been clearer

 

My guess is that the 1's and 0's are probably your array indexes, so figure out how to reference the value instead of the array key

Link to comment
Share on other sites

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

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.