dwex Posted January 20, 2011 Share Posted January 20, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/ Share on other sites More sharing options...
BlueSkyIS Posted January 20, 2011 Share Posted January 20, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1162511 Share on other sites More sharing options...
dwex Posted January 20, 2011 Author Share Posted January 20, 2011 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1162554 Share on other sites More sharing options...
dwex Posted January 21, 2011 Author Share Posted January 21, 2011 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1162842 Share on other sites More sharing options...
PFMaBiSmAd Posted January 21, 2011 Share Posted January 21, 2011 $_POST['name'] and $_POST['dob'] will both be arrays and you can iterate over them using php's array functions, such as foreach Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1162990 Share on other sites More sharing options...
dwex Posted January 21, 2011 Author Share Posted January 21, 2011 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)); Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1163123 Share on other sites More sharing options...
TOA Posted January 21, 2011 Share Posted January 21, 2011 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)); } Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1163126 Share on other sites More sharing options...
dwex Posted January 21, 2011 Author Share Posted January 21, 2011 thx for the reply. It did insert more than 1 row into my database but the fields , name and price didnt quite make it in correctly. They turned out to be either 0 or 1. Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1163141 Share on other sites More sharing options...
TOA Posted January 21, 2011 Share Posted January 21, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1163148 Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2011 Share Posted January 21, 2011 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."; } Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1163159 Share on other sites More sharing options...
dwex Posted January 21, 2011 Author Share Posted January 21, 2011 wow, that is some crazy a codes there .. gonna need to take some time to understand but anyway thanks for everyone! Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1163171 Share on other sites More sharing options...
Pikachu2000 Posted January 21, 2011 Share Posted January 21, 2011 In a nutshell, all it really does is for every pair of name/price values in the $_POST array, it sanitizes and formats them then adds them to the query string. Quote Link to comment https://forums.phpfreaks.com/topic/225071-how-to-send-multiple-information-of-the-same-name/#findComment-1163174 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.