Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    99
    Thanks
    8
    Thanked 0 Times in 0 Posts

    updating multiple array daya with selection and checkbox values

    Hello,

    I am trying to update my database with multiple array values like this.

    Code:
    if (isset($_POST['submit_multiple']))
    {
    
    $id=$_POST['selector'];
    $class=$_POST['class'];
    $section=$_POST['section'];
    
    $N = count($id);
    
    for($i=0;$i<$N;$i++)
    {
    
    echo "update table1 set transfer_status='yes',transfer_date='".date('Y-m-d')."',class='".$class[$i]."',section='".$section[$i]."' where enroll_no='".$id[$i]."'";
    }
    and in the form

    Code:
    <form action="transfer_mul_student.php" method="post"><select name="class[]">
    <option value="">--SELECT CLASS--</option>
    <option value="Nursery">Nursery</option>
    <option value="LKG">LKG</option>
    <option value="UKG">UKG</option>
    <option value="I">I</option>
    <option value="II">II</option>
    <option value="III">III</option>
    <option value="IV">IV</option>
    
    
    </select>
    <select name="section[]"><option value="">--SELECT SECTION--</option>
    <option value="A">A</option>
    <option value="B">B</option>
    <option value="C">C</option>
    </select><input name="selector[]" id="selector" type="checkbox" value="<?php echo $row['enroll_no']; ?>" />
    
    
    <input type="submit" class="delete_multiple" value="Transer" name="submit_multiple"  />

    Here
    Code:
    $row['enroll_no']
    is from table student.

    Page with form looks like this

    updating multiple array daya with selection and checkbox values-err1.png

    In this, If i select checkbox1, checkbox2 checkbox3.. it takes the value for class and section properly, but if i select checkbox1, checkbox3, it takes value for 1st record properly and for 2nd one it takes blank value for class and section.

    How to overcome this? Please suggest

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,714
    Thanks
    25
    Thanked 660 Times in 659 Posts
    I know that $row['enroll_no'] is generated by a DB read. I used two( $row["enroll_no"] and $row["enroll"] ) so I could show two students.
    This is the form A PHP file:
    PHP Code:
    <?php
    $row
    ["enroll_no"] = 1234;
    $row["enroll"] = 9876;
    ?>

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="UTF-8" />
    <title>Title of the document</title>

    </head>

    <body>
    <form action="test_2.php" method="post">

    <select name="class<?php echo $row['enroll_no']; ?>">
        <option value="">--SELECT CLASS--</option>
        <option value="Nursery">Nursery</option>
        <option value="LKG">LKG</option>
        <option value="UKG">UKG</option>
        <option value="I">I</option>
        <option value="II">II</option>
        <option value="III">III</option>
        <option value="IV">IV</option>
    </select>

    <select name="section<?php echo $row['enroll_no']; ?>"><option value="">--SELECT SECTION--</option>
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
    </select>

    <input name="selector[]" id="selector" type="checkbox" value="<?php echo $row['enroll_no']; ?>" />

    <br />
    <select name="class<?php echo $row['enroll']; ?>">
        <option value="">--SELECT CLASS--</option>
        <option value="Nursery">Nursery</option>
        <option value="LKG">LKG</option>
        <option value="UKG">UKG</option>
        <option value="I">I</option>
        <option value="II">II</option>
        <option value="III">III</option>
        <option value="IV">IV</option>
    </select>

    <select name="section<?php echo $row['enroll']; ?>"><option value="">--SELECT SECTION--</option>
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
    </select>

    <input name="selector[]" id="selector" type="checkbox" value="<?php echo $row['enroll']; ?>" />

    <input type="submit" class="delete_multiple" value="Transer" name="submit_multiple"  />
    </form>
    </body>

    </html>
    Please notice the name attrs. As the next file uses them to collect information.

    The file the form calls:
    Also a PHP:
    PHP Code:
    <?php
    if (isset($_POST['submit_multiple'])){
        if(isset(
    $_POST['selector'])){
            foreach(
    $_POST['selector'] as $select){
                
    $id $select;
                
    $class $_POST["class$select"];
                
    $section $_POST["section$select"];
                echo 
    "update table1 set transfer_status='yes', transfer_date='".date('Y-m-d')."', class='".$class."', section='".$section."' where enroll_no='".$id."'";
            }
        }
    }
    ?>
    Hope this helps.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Posts
    376
    Thanks
    0
    Thanked 25 Times in 25 Posts
    What is the purpose of the checkbox? You UPDATE whether it's selected or not.

    You could give each set/row a unique key when you output the form, eg.
    Code:
    <select name="class[0]">
    ...
    <select name="section[0]">
    ...
    <input name="selector[0]" ...
    
    <!-- second row -->
    
    <select name="class[1]">
    ...
    <select name="section[1]">
    ...
    <input name="selector[1]" ...
    Then you get
    Code:
    Array
    (
        [class] => Array
            (
                [1] => Nursery
                [2] => LKG
                [3] => UKG
            )
    
        [section] => Array
            (
                [1] => A
                [2] => B
                [3] => C
            )
    
        [selector] => Array
            (
                [1] => 1234
                [3] => 9012
            )
    
        ...
    )
    Then you can foreach through the $_POST[selector] array and the rows will have the same key
    PHP Code:
    <?php

    foreach($_POST['selector'] as $n => $selector)
    {
        
    $class $_POST['class'][$n];
        
    $section $_POST['section'][$n];
    }
    You could use the $row['enroll_no'] if it will not be repeated in the <form>.
    Last edited by schleppel; 05-14-2014 at 08:52 PM. Reason: $row['enroll_no']

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,714
    Thanks
    25
    Thanked 660 Times in 659 Posts
    @ schleppel, Are you talking to me or the OP?
    You UPDATE whether it's selected or not.
    The OP doesn't because hes code don't work and he came here to fix it. My code doesn't it uses an statement "if(isset($_POST['selector']))" to only process the students that were checked as updating or changing their subjects.

    I admit the needed verifications are not included here, but that wasn't the question asked.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    Regular Coder
    Join Date
    Oct 2004
    Posts
    376
    Thanks
    0
    Thanked 25 Times in 25 Posts
    The OP. My mistake he count()s $_POST['selector'].

    I admit the needed verifications are not included here, but that wasn't the question asked.
    It was to help me understand the situation, not a criticism of the code.
    Last edited by schleppel; 05-14-2014 at 10:59 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •