mrooks1984 Posted August 26, 2012 Share Posted August 26, 2012 hello, i am hoping someone can help, i have multiple post results that need to go one by one into a table heres the form i have: echo '<h2>Update Variation Values</h2>'; echo '<div id ="content_form">'; echo '<form method="post" action="">'; echo '<input type="hidden" name="option_id" value="' . $option_id . '">'; echo '<input type="hidden" name="variation_id" value="' . $variation_id . '">'; echo '<input type="hidden" name="product_name" value="' . $product_name . '">'; $sql = "SELECT * FROM store_option$variation_id WHERE product = '$product_name'"; $res = mysql_query($sql) or die(mysql_error()); while ($row = mysql_fetch_assoc($res)) { echo '<div id="content_title"><label for="title[]">Title:</label></div>'; echo '<div id="content_box"><input name="title[]" type="text" id="title" value="' . $row['title'] . '" /></div>'; echo '<div id="content_title"><label for="price[]">Price:</label></div>'; echo '<div id="content_box"><input name="price[]" type="text" id="price[]" value="' . $row['price'] . '" /></div>'; } echo '<div id="content_body"><input type="submit" name="post_update_variation_value" value="Save Values"></div></form>'; echo '</div>'; } heres my current code echo '<pre>',print_r($_POST,true); foreach($_POST['title'] as $key => $value){ if($value !=''){ $sql = "UPDATE store_option$variation_id SET title = '$value', price = '$price' WHERE product = '$product_name'"; $res = mysql_query($sql) or die(mysql_error()); } else { echo "Option $key is empty<br />"; } } its currently just doing the last result and filling up the database with that result, many thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/267630-post-multiple-fields-with-the-same-name-into-a-database/ Share on other sites More sharing options...
jazzman1 Posted August 26, 2012 Share Posted August 26, 2012 Does the table "store_option$variation_id", it has been updated correctly? I'm seeing a syntax error in the code. Quote Link to comment https://forums.phpfreaks.com/topic/267630-post-multiple-fields-with-the-same-name-into-a-database/#findComment-1372706 Share on other sites More sharing options...
darkfreaks Posted August 26, 2012 Share Posted August 26, 2012 why do you have a string in an array loop i read you could use the (array) type cast inside the foreach and it should be fine not sure if this is true or not to convert a string to array inside a foreach loop. normally if it was a string you would be getting "invalid argument foreach" error. also might change != to !== Quote Link to comment https://forums.phpfreaks.com/topic/267630-post-multiple-fields-with-the-same-name-into-a-database/#findComment-1372718 Share on other sites More sharing options...
mrooks1984 Posted August 27, 2012 Author Share Posted August 27, 2012 thanks for your replies, is it does update fine, but each field has the last result Quote Link to comment https://forums.phpfreaks.com/topic/267630-post-multiple-fields-with-the-same-name-into-a-database/#findComment-1372787 Share on other sites More sharing options...
mrooks1984 Posted August 27, 2012 Author Share Posted August 27, 2012 if i use this code, i get this message: Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\jubileeleather\site\admin\_class\store.php on line 654 $option_id = mysql_real_escape_string($_POST['option_id']); $variation_id = mysql_real_escape_string($_POST['variation_id']); $product_name = mysql_real_escape_string($_POST['product_name']); foreach($_POST['title'] as $key1 => $value1){ foreach($_POST['price'] as $key2 => $value2){ foreach($_POST['option_value_id'] as $key3 => $value3){ $sql = "UPDATE store_option$variation_id SET title = '$value1', price = '$value2' WHERE id = '$value3'"; $res = mysql_query($sql) or die(mysql_error()); } } } echo '<script>window.location="dashboard.php?page=store_variation"</script>'; } Quote Link to comment https://forums.phpfreaks.com/topic/267630-post-multiple-fields-with-the-same-name-into-a-database/#findComment-1372791 Share on other sites More sharing options...
trq Posted August 27, 2012 Share Posted August 27, 2012 You need to use the $key to get the index of the price. foreach ($_POST['title'] as $key => $value) { $title = $value; $price = $_POST['price'][$key]; // execute your query } Quote Link to comment https://forums.phpfreaks.com/topic/267630-post-multiple-fields-with-the-same-name-into-a-database/#findComment-1372795 Share on other sites More sharing options...
mrooks1984 Posted August 27, 2012 Author Share Posted August 27, 2012 thanks for your reply, i have tried this, but only the first field is changed in the database. function post_update_variation_value() { $option_id = mysql_real_escape_string($_POST['option_id']); $variation_id = mysql_real_escape_string($_POST['variation_id']); $product_name = mysql_real_escape_string($_POST['product_name']); foreach ($_POST['title'] as $key => $value) { $title = $value; $price = $_POST['price'][$key]; // execute your query $sql = "UPDATE store_option$variation_id SET title = '$title', price = '$price' WHERE id = '$key'"; $res = mysql_query($sql) or die(mysql_error()); } echo '<script>window.location="dashboard.php?page=store_variation"</script>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/267630-post-multiple-fields-with-the-same-name-into-a-database/#findComment-1372841 Share on other sites More sharing options...
trq Posted August 27, 2012 Share Posted August 27, 2012 Are you sure that $key matches up with your ids? Your not using your ids in your initial form creation code. Quote Link to comment https://forums.phpfreaks.com/topic/267630-post-multiple-fields-with-the-same-name-into-a-database/#findComment-1372847 Share on other sites More sharing options...
mrooks1984 Posted August 27, 2012 Author Share Posted August 27, 2012 yea realised now, tried adding this to the code now all fine, thanks so much for your help $price = $_POST['price'][$key]; $id = $_POST['option_value_id'][$key]; // execute your query $sql = "UPDATE store_option$variation_id SET title = '$title', price = '$price' WHERE id = '$id'"; $res = mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/267630-post-multiple-fields-with-the-same-name-into-a-database/#findComment-1372853 Share on other sites More sharing options...
trq Posted August 27, 2012 Share Posted August 27, 2012 I would use the $key when making your form, that way you know things are going to be held together as they should be: echo '<div id="content_title"><label for="title[' . $row['id'] . ']">Title:</label></div>'; echo '<div id="content_box"><input name="title[' . $row['id'] . ']" type="text" id="title[' . $row['id'] . ']" value="' . $row['title'] . '" /></div>'; echo '<div id="content_title"><label for="price[' . $row['id'] . ']">Price:</label></div>'; echo '<div id="content_box"><input name="price[' . $row['id'] . ']" type="text" id="price[' . $row['id'] . ']" value="' . $row['price'] . '" /></div>'; Quote Link to comment https://forums.phpfreaks.com/topic/267630-post-multiple-fields-with-the-same-name-into-a-database/#findComment-1372855 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.