Jump to content

updating multiple array daya with selection and checkbox values


mythri

Recommended Posts

Hello, 

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

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 

<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 

$row['enroll_no']

is from table student.

I have attached the screenshot for the page display with form

 

post-168283-0-19274500-1400074815_thumb.png

 

In that 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

 

 

 

 

Link to comment
Share on other sites

If the second checkbox is not checked it is not posted, so the third checkbox will be the second in your POST array and no longer corresponds to the correct items in the other arrays (which are always posted)

 

I would use the enroll_no to tie them together eg

 

<select name='class[enroll_no]' >

<select name='section[enroll_no]' >

<input name="selector[enroll_no]" ... >

Link to comment
Share on other sites

Hello,

 

I have used like this 

<select name="class[<?php echo $row['enroll_no']?>]">

It is taking class with roll_no .

But if i display

 

$class= $_POST['class'];

print_r ($class);

 

It displays unselected checkbox value also :( something like this

Array ( [2014001] => VIII [2014002] => [2014003] => XII )

I have selected only 1 and 3 :(

Link to comment
Share on other sites

Noooooo :(

 

It takes all the data (including unselected)

 

Result is like this

Array
(
    [class] => Array
        (
            [2014001] => II
            [2014002] => 
            [2014003] => XI
        )

    [section] => Array
        (
            [2014001] => B
            [2014002] => 
            [2014003] => B
        )

    [selector] => Array
        (
            [0] => 2014001
            [1] => 2014003
        )

    [update_multiple] => Transer
)
Link to comment
Share on other sites

That is what I keep telling you.

 

Notice you only have 2 items in the "selector" array. Those are the two you should process.

foreach ($_POST['selector'] as $rollno) {
    $class = $_POST['class'][$rollno];
    $section = $_POST['section'][$rollno];
    echo "update table1
        set transfer_status='yes'
        ,transfer_date=CURDATE()
        ,class='$class'
        ,section='$section'
        where enroll_no=$rollno";

}

PS Don't forget to sanitize the POST data before using it in the query

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.