Maryam Nabilah Maryam Nabilah - 4 years ago 59
PHP Question

UPDATE MULTI ROW

I get an error like this...

Notice: Undefined offset: 0 on line 93


My php code is look like this below code..

$i=0;
while ($ww=mysqli_fetch_array($query))
{
if ($i%2==0)
$class="evenRow";
else
$class="oddRow";

$id=$ww[0];
$studentid=$ww[1];
$name=$ww[2];
$kelompok=$ww[8];
$block=$ww[9];
$level=$ww[10];
$house=$ww[11];
$status=$ww[14];



echo "<tr>
<input type=hidden name=applyid[] value=".$id."/>
<td>$studentid</td>
<td>$name</td>
<td>$kelompok</td>
<td>$block</a></td>
<td>$level</td>
<td>$house</td>
<td>
<input type=checkbox name=status approved checked> APPROVED <br>
</td>
</tr>";
}
$i++;
echo "</table>";


this is the error on line 93: $checkbox[] .= $_POST['applyid'][$i];}

and the SQL Query to update the status is look like this...

<?php

include("connection.php");

$checkbox = array();
if(isset($_POST['applyid']))
{
$check = count($_POST['applyid']);
for($i=0;$i<$check;$i++){
$checkbox[] .= $_POST['applyid'][$i];}

$check = "('" . implode( "','", $checkbox ) . "');" ;
$sql="UPDATE application SET apply_status = 'APPROVED' WHERE apply_id IN $check" ;
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
}


?>


I want to update multiple row selected by checkbox. This is the table output
Click Here

..............................................................................

VIEW THE PENDING STATUS:

this is my code if only the apply_status = 'PENDING' will only view.
...
I add the if else statement... but is not working. if there is several apply_status = approved. It will not showed the pending one. But if there is no apply_status = aprroved. It will view all the application.

<?php
include("connection.php");
$sql="SELECT * FROM application";
$record = mysqli_query($con, $sql) or die ("error".mysqli_error($con));
$apply = mysqli_fetch_assoc($record);

$status1 = $apply["apply_status"];

if ($status1 == "APPROVED") {
echo "<br>";
echo "No application from student yet.<br>";
echo "<br>";
}

else {

echo "<table border='1'><tr>

<td><strong>Student ID</strong></td>
<td><strong>Student Name</strong></td>
<td><strong>Kelompok</strong></td>
<td><strong>Block</strong></td>
<td><strong>Level</strong></td>
<td><strong>House</strong></td>
<td><strong>Status</strong></td>


</tr>";
$i=0;
while ($ww=mysqli_fetch_array($query))
{
if ($i%2==0)
$class="evenRow";
else
$class="oddRow";

$id=$ww[0];
$studentid=$ww[1];
$name=$ww[2];
$kelompok=$ww[8];
$block=$ww[9];
$level=$ww[10];
$house=$ww[11];
$status=$ww[14];



echo '<tr>
<input type="hidden" name="applyid['.$i.']" value="'.$id.'"/>
<td>'.$studentid.'</td>
<td>'.$name.'</td>
<td>'.$kelompok.'</td>
<td>'.$block.'</a></td>
<td>'.$level.'</td>
<td>'.$house.'</td>
<td>
<input type="checkbox" name="status['.$i.']" value="approved" checked> APPROVED <br>
</td>
</tr>';
$i++;
}

echo '</table>';

}
?>

Answer Source

Try changing :

 $check = count($_POST['applyid']);
 for($i=0;$i<$check;$i++){
    $checkbox[] .= $_POST['applyid'][$i];
 }

TO

 foreach($_POST['applyid'] as $index=>$idValue){
     $checkbox[] .= $idValue;
 }

EDIT :

use the index of the loop to index the input's so that you can associate them with each other in the receiving page :

    $i = 0; // $i used to determain if it is odd or even, also used as the index in the html inputs
            // comments are your friend
    while ($ww=mysqli_fetch_array($query))
    {
        if ($i%2==0){ // best practice for readable code is to use the braces
            $class="evenRow";
        }
        else{
            $class="oddRow";
        }
        // easier to read when spaced equally
        $id         = $ww[0];
        $studentid  = $ww[1];
        $name       = $ww[2];
        $kelompok   = $ww[8];
        $block      = $ww[9];
        $level      = $ww[10];
        $house      = $ww[11];
        $status     = $ww[14];

        // single quotes are faster to proccess in PHP
        // use $i to force the array index
        // place quotation marks arrount html attribute values
        echo '<tr>
                <input type="hidden" name="applyid['.$i.']" value="'.$id.'"/>
                <td>'.$studentid.'</td>
                <td>'.$name.'</td>
                <td>'.$kelompok.'</td>
                <td>'.$block.'</td> <!-- removed a closing "a" tag, as it wasn\'t closeing anything -->
                <td>'.$level.'</td>
                <td>'.$house.'</td>
                <td>
                    <input type="checkbox" name="status['.$i.']" value="approved" checked> APPROVED <br>
                </td>
                </tr>'; 
        $i++; // increment $i inside the loop, else it will never change until the loop is completed
    }
    // single quotes are faster to proccess in PHP
    echo '</table>'; 

On the recieving page, use this,

 include("connection.php");

if(isset($_POST['applyid']))
{
    $allIDs = ''; // using this in the single SQL query, remove it if you not going to use it
    $approvedIds = ''; // we will add all the approved Ids here for using in the SQL query
    $unapprovedIds = ''; // we will add all the un-approved Ids here for using in the SQL query
    // if the single SQL query works, remove the $unapprovedIds
    foreach($_POST['applyid'] as $index=>$idValue){
        if(isset($_POST['status'][$index])){ // if the status for this ID was posted, it was selected ( ony selected checkboxes get posted )
          $approvedIds .= ($approvedIds === '' ? '' : ', ').$idValue; // we add it to the string that will be used in the "IN" 
                                                                     // if $approvedIds is not blank, add a comma to format corrctly for SQL
        }else{ // if the single SQL query works, remove this entire else
            $unapprovedIds .= ($unapprovedIds === '' ? '' : ', ').$idValue; // we add it to the string that will be used in the "IN" 
                                                                     // if $unapprovedIds is not blank, add a comma to format corrctly for SQL

        }
        $allIDs .= ($allIDs === '' ? '' : ', ').$idValue; // using this in the single SQL query, remove it if you not going to use it
    } 
    // update all the approved ones
    // single quotes 
    // format you SQL in a easy to read way
    $sql = 'UPDATE application 
            SET apply_status = \'APPROVED\' 
            WHERE apply_id IN ('.$approvedIds.')' ;
    $result = mysqli_query($con, $sql) or die(mysqli_error($con));
    // update all the unaproved ones
    // format you SQL in a easy to read way
    $sql = 'UPDATE application 
            SET apply_status = \'UNAPROVED\' 
            WHERE apply_id IN ('.$unapprovedIds.')' ;
    $result = mysqli_query($con, $sql) or die(mysqli_error($con));

    /////////////////
    //
    // worth a try as a single query UNTESTED :
    //
    //////////////////////////////////////////////
    $sql='UPDATE application 
            SET apply_status = 
                                CASE WHEN apply_id IN ('.$approvedIds.') THEN \'APPROVED\'
                                     ELSE \'UNAPPROVED\'
                                 END 
            WHERE apply_id IN ('.$allIDs.')';
}

I have added some basic guidelines for you so that you can apply them going forward

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download