1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need help to post data into table with same name multiple rows

Discussion in 'PHP' started by MrPJH, Oct 18, 2014.

  1. #1
    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
     
    Solved! View solution.
    MrPJH, Oct 18, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    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
     
    PoPSiCLe, Oct 18, 2014 IP
  3. MrPJH

    MrPJH Well-Known Member

    Messages:
    1,066
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    155
    #3
    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]
     
    MrPJH, Oct 18, 2014 IP
  4. jslirola

    jslirola Active Member

    Messages:
    39
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    58
    #4
    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.
     
    jslirola, Oct 18, 2014 IP
  5. MrPJH

    MrPJH Well-Known Member

    Messages:
    1,066
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    155
    #5
    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):
     
    MrPJH, Oct 19, 2014 IP
  6. #6
    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.
     
    jslirola, Oct 19, 2014 IP