i need help to post data into a table using mysql and procedural php have a lot of inputs with same name like <input type = "text" name = "point[]" /> <input type = "text" name = "point[]" /> <input type = "text" name = "comment[]" /> <input type = "text" name = "comment[]" /> <input type = "text" name = "poster[]" /> <input type = "text" name = "poster[]" /> <input type = "text" name = "poster[]" /> Code (markup): All these are need to be inserted into one mysql (using mysql not mysqli or PDO) table having id => auto increment point comment poster i am really stuck on it need to post using $_POST any one to help please
First of all, a quick recap of why you have multiple same-name inputs would be good. Apart from that, this is just a for-loop running through each iteration and posting it to mysql - why you'd want to use mysql_ instead of mysqli_ or PDO is beyond me, but doesn't really have anything to do with this problem. Depending on the exact construction of inputs, and how they're posted, I'd say something like this would do: $points = $_POST['point']; $comments = $_POST['comment']; $posters = $_POST['poster']; for ($i = 0; $i <= count($comments); $i++) { //construct the outer loop with the largest group, unless they're all the same amount, then it doesn't matter $comment = (!empty($comments[$i])) ? $comments[$i] : ''; $point = (!empty($points[$i])) ? $points[$i] : ''; $poster = (!empty($posters[$i])) ? $posters[$i] : ''; mysql_query("INSERT INTO name_of_table (`point`,`comment`,`poster`) VALUES ('$point','$comment','$poster')); } PHP: Horrible code, but it should work just fine - if the number of different inputs differ (like, two points, two comments, and four posters, then you need to do a bit more within the outer loop). Doesn't cater for potential errors, nor validating/securing the inputs
Thanks for your effort though i pasted poster input 3 time to avoid been suggested for loop $my_comment = $_POST['point_comment']; $point_comment =""; foreach($my_comment as $key => $value){ $point_comment = $value; Query here } Code (markup): above code worked for a single field for as many rows as they appear but i don't know how to loop the second _POST for second row within this loop[/code]
The code of "PoPSiCLe" should work, contains every variable. With your code can't add the second $_POST within that loop because for each element of "$_POST['point_comment']" will repeats all elements of the second $_POST. How many elements exist for each type? Dynamic? If the fields are sorted and grouped the code will be easier to do.
Inputs are coming from another table for example if there are 5 questions about a product "Pen" a single form will have 5 inputs which will store results as Pen ID from current product table question id from questenair and 5 answers to questions + a check box to each pen ID remains same will insert INSERT INTO checked (product_id, question_id, yes_no, comment)VALUES() Code (markup):
I have written a example of single form: <form method="post" action="checks.php"> <div id="answer"> <p> Question 1: ........................ </p> <input type="text" name="answer[]" /> <input type="checkbox" name="yesno[]" value="1" /> <input type="hidden" name="question_id[]" value="1" /> </div> <div id="answer"> <p> Question 2: ........................ </p> <input type="text" name="answer[]" /> <input type="checkbox" name="yesno[]" value="2" /> <input type="hidden" name="question_id[]" value="2" /> </div> <div id="answer"> <p> Question 3: ........................ </p> <input type="text" name="answer[]" /> <input type="checkbox" name="yesno[]" value="3" /> <input type="hidden" name="question_id[]" value="3" /> </div> <input type="hidden" name="product_id" value="x" /> <input type="submit" /> </form> HTML: checks.php <?php $questions = $_POST["question_id"]; $answers = $_POST["answer"]; $checkbox = $_POST["yesno"]; $product_id = $_POST["product_id"]; $total = 0; echo "Product ID: ".$product_id."<br />"; foreach ($answers as $a) { $question_id = $questions[$total]; $checked = (in_array($total, $checkbox)) ? 1 : 0; // 1 = checked # Debug echo "Question ID: ".$question_id.".<br />"; $total++; echo " Answer ".$total.": ".$a."<br />"; echo ($checked==1) ? "checked" : "not checked"; echo "<br />"; # mysql_query("INSERT INTO checked (product_id, question_id, yes_no, comment) VALUES ($product_id, $question_id, $checked, '$a')"); } ?> PHP: According to your post, I understand that the code may look like this.