papaface Posted December 15, 2006 Share Posted December 15, 2006 Hello,Could someone please inform me of how I would select a specific key in this loop?[code]foreach ($_POST as $key => $value)[/code]I want to get a key named "name".Thank you. Link to comment https://forums.phpfreaks.com/topic/30806-select-a-specific-key/ Share on other sites More sharing options...
complex05 Posted December 15, 2006 Share Posted December 15, 2006 foreach($_POST as $key => $value){ if($value == "name") { $selected = $key; break; } else { continue;} Link to comment https://forums.phpfreaks.com/topic/30806-select-a-specific-key/#findComment-142027 Share on other sites More sharing options...
papaface Posted December 15, 2006 Author Share Posted December 15, 2006 Thanks for that, but it isnt what i really need.Im trying to include a specific input field that has been posted to this script in a peice of SQL coding.In order to do that i need to do something like $key[name] but that doesnt work.Any ideas? Link to comment https://forums.phpfreaks.com/topic/30806-select-a-specific-key/#findComment-142029 Share on other sites More sharing options...
complex05 Posted December 15, 2006 Share Posted December 15, 2006 [quote]Im trying to include a specific input field that has been posted to this script in a peice of SQL coding.[/quote]I don't really understand what you mean, can you rephrase? Link to comment https://forums.phpfreaks.com/topic/30806-select-a-specific-key/#findComment-142031 Share on other sites More sharing options...
papaface Posted December 15, 2006 Author Share Posted December 15, 2006 [code]if (isset($_POST["submit"])) { foreach ($_POST as $key => $value) { if ($key != empty($value)) { if ($key != "submit") { $query = mysql_query("SELECT * FROM orders WHERE order_item = '$value'"); $check = mysql_num_rows($query); if(!$check) { mysql_query("insert into orders (order_item) values ('$value')",$con); } else { $query = mysql_query("SELECT order_quantity FROM orders WHERE order_item='$value'"); $data = mysql_fetch_assoc($query); $quantity = $data["order_quantity"]; $quantity = $quantity + 1; mysql_query("UPDATE orders SET order_quantity=$quantity WHERE order_item='$value'"); } } } } echo "Thank your for your order!"; }[/code]^^ that is what I have at the moment. Presently that code will input into the database the values into the order_items table. I want all the $values to go into the items field, except for the name field (that was submitted on a previous page). I want the name value to be inserted into the same row as the order_items. Very difficult to explain, but I want to achieve this:[code]mysql_query("insert into orders (order_item,order_name) values ('$value','THEVALUEOFTHENAMEFIELDONPREVIOUSPAGESHOULDBEINSERTEDHERE')",$con);[/code]hope that clears things up a little. Link to comment https://forums.phpfreaks.com/topic/30806-select-a-specific-key/#findComment-142042 Share on other sites More sharing options...
complex05 Posted December 15, 2006 Share Posted December 15, 2006 are you sure you can't just do:mysql_query("insert into orders (order_item,order_name) values ('$value','$key')",$con);I think that will work... Link to comment https://forums.phpfreaks.com/topic/30806-select-a-specific-key/#findComment-142047 Share on other sites More sharing options...
papaface Posted December 15, 2006 Author Share Posted December 15, 2006 No, because $key is the value of all the input field names on the previous page. So I would end up getting "name,order1,order2,order3,order4,order5" all put into the order_name field. Whereas I just want the value of the "name" $key added. Link to comment https://forums.phpfreaks.com/topic/30806-select-a-specific-key/#findComment-142053 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.