更新多行

时间:2017-02-05 17:09:44

标签: php sql-update multiple-columns

我收到这样的错误;

  

注意:未定义的偏移量:第93行的0

我的php代码如下所示代码..

$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>";

这是第93行的错误:$checkbox[] .= $_POST['applyid'][$i];}

更新状态的SQL查询看起来像这样......

<?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));
    }       
?> 

我想更新复选框选中的多行。这是表输出 Click Here

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

查看待处理状态:

这是我的代码,只有apply_status = 'PENDING'只会查看。

我添加了if else语句......但是没有用。如果有多个apply_status = approved。它不会显示待处理的。但如果没有apply_status = aprroved。它将查看所​​有应用程序。

<?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>';

    }
?>

1 个答案:

答案 0 :(得分:0)

尝试更改:

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

TO

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

编辑:

使用循环索引来索引输入,以便您可以在接收页面中将它们相互关联:

    $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>'; 

在接收页面上,使用此

 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.')';
}

我已经为您添加了一些基本指南,以便您可以继续使用它们