根据用户选择编辑多个选择

时间:2016-01-26 22:31:56

标签: php mysql

我一直试图解决这个问题,如果有人可以帮助我,我会很感激。这是我的问题。

enter image description here

这是我的代码;它只是查看数据库中作业表的内容,并根据选择根据需要执行编辑。复选框位于每个作业旁边,页面末尾有一个更新按钮,用于提交..

我收到更新错误。请帮帮我。

        <?php
    session_start();
    if( isset($_SESSION['username']) ){
     include('../CIEcon.php');

    echo "<form action= 'adminCleaning.php'  method = 'post'>" ; 
// when the user click update..         
    if(isset($_POST['update'])){
            if( empty($_POST['Id'])   || $_POST['Id'] == 0 ){
     echo"<h4>  please choose something to update   </h4>";
         echo"test(1): pass <br> ";
              }else{

            // comes here even though u dind't chhose, cause
            // it set IDs next to each feild.. 
             echo"!!....HERE....!! ";   
                }
            }// end of update $_POAT[update]

  $sql = "SELECT * FROM Cleaning ";
  $result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));

                          /// NOW DISPLAY ALL INFO FROM CHOSEN DATABASE...

                                echo "

    <table cellpadding ='4' border='1' width='80%' align='center'>
 <tr>

    <th class='tt' >Check </th>
    <th class='tt'> Job's Name</th>
    <th class='tt' >Description</th>
    <th class='tt' > No Students needed</th>
    <th class='tt' >Due Date</th>
    </tr>";

        while($row = mysqli_fetch_array($result))
        {
            echo "<br>";
            echo "<tr>";
 echo "<td> <input type='checkbox'  name='Id[]'  value='".$row['Id']."' /> </td>";  // array[] cause to edit more than one record... 
            echo "<td>".'<input type="text"  name="jobname['.$row['Id'].']"     value='.$row['JobName'].' >'."</td>";
            echo "<td>".'<input type="text" name="description['.$row['Id'].']"   value='.$row['Description'].'> '."</td>";
            echo "<td>".'<input type="text" name="nostudent['.$row['Id'].']"     value='.$row['NoStudent'].'>'."</td>";
            echo "<td>".'<input type="text" name="duedate['.$row['Id'].']"        value='.$row['DueDate'].'>'."</td>";

  echo "<input type=hidden name='Id[]'  value='".$row['Id']."' >";

            echo "</tr>";
    echo "jobname['.$row[Id].']" ;  // testing. 
    echo "description['.$row[Id].']" ;  // testing. 
    echo "nostudent['.$row[Id].']" ;  // testing. 
                                }
                                echo "</table>";
                       /// END THE SEARCH HERE...........

                                echo " <br>
                                    <div align='center'>
                                    <input type='reset' value='clear' /> 

                                    <input type='submit' name='update' value='update' />

                                    </div> ";

                              mysqli_close($dbCIE);

    echo "</form>";
    }
    else{echo "must logout to see this page..!!";}

    ?>

    <html>

    <head><title> ..Cleanding.... </title></head>

    <style type="text/css">

    body{
        margin-top: 70px;    /*space above the table....*/
        background-color: #23438e; 
    }
    table{
        background-color: white; 
    }


    .tt{
        background: #f26822;
        color: white ;
    }
    </style>


    <body>

    <!-- <a href= "../AdminIndex.php" > <button> Main Page </button></a>     -->

    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

首先,您的表单和表格位于<body>标记之外。您必须显示以下所有内容:<body> **display here** </body>

其次,从代码中删除此行echo "<input type=hidden name='Id[]' value='".$row['Id']."' >";,这不是必需的。

现在回答您的问题,$_POST['Id']是一个作业ID数组,因此请使用count()函数检查数组是否为空,并使用foreach循环更新每个个别行。所以你应该像这样处理你的表格:

// when the user click update..         
if(isset($_POST['update'])){

    if(count($_POST['Id'])){

        // $_POST['Id'] is an array of job id
        foreach($_POST['Id'] as $v){
            $sqlUpdate = "UPDATE Cleaning SET JobName='" . $_POST['jobname'][$v] . "', Description='" . $_POST['description'][$v] . "', NoStudent='" . $_POST['nostudent'][$v] ."', DueDate='" . $_POST['duedate'][$v] . "' WHERE Id = '" . $v . "'";
            $resultUpdate = mysqli_query($dbCIE,$sqlUpdate) or die(mysqli_error($dbCIE));

            // If you want you can use mysqli_affected_rows() function to 
            // check how many were affected by the UPDATE query
        }


    }else{

        echo"<h4>please choose something to update</h4>";

    }

}

你的整个代码应该是这样的:

<?php
    session_start();
?>

<html>

<head>
    <title> ..Cleanding.... </title>
    <style type="text/css">
        body{
            margin-top: 70px;    /*space above the table....*/
            background-color: #23438e; 
        }
        table{
            background-color: white; 
        }
        .tt{
            background: #f26822;
            color: white ;
        }
    </style>
</head>

<body>

<?php
    if( isset($_SESSION['username']) ){
        include('../CIEcon.php');

        // when the user click update..         
        if(isset($_POST['update'])){

            if(count($_POST['Id'])){

                // $_POST['Id'] is an array of job id
                foreach($_POST['Id'] as $v){
                    $sqlUpdate = "UPDATE Cleaning SET JobName='" . $_POST['jobname'][$v] . "', Description='" . $_POST['description'][$v] . "', NoStudent='" . $_POST['nostudent'][$v] ."', DueDate='" . $_POST['duedate'][$v] . "' WHERE Id = '" . $v . "'";
                    $resultUpdate = mysqli_query($dbCIE,$sqlUpdate) or die(mysqli_error($dbCIE));

                    // If you want you can mysqli_affected_rows() function to 
                    // check how many were affected by the UPDATE query
                }


            }else{

                echo"<h4>please choose something to update</h4>";

            }

        }

        $sql = "SELECT * FROM Cleaning ";
        $result = mysqli_query($dbCIE, $sql) or die(mysqli_error($dbCIE));
        ?>
        <form action= 'adminCleaning.php' method = 'post'>
            <table cellpadding ='4' border='1' width='80%' align='center'>
            <tr>

            <th class='tt' >Check </th>
            <th class='tt'> Job's Name</th>
            <th class='tt' >Description</th>
            <th class='tt' > No Students needed</th>
            <th class='tt' >Due Date</th>
            </tr>
            <?php
            while($row = mysqli_fetch_array($result)){
                ?>
                <tr>
                <td> <input type="checkbox" name="Id[]" value="<?php echo $row['Id']; ?>" /> </td>
                <td><input type="text" name="jobname[<?php echo $row['Id']; ?>]" value="<?php echo $row['JobName']; ?>" /></td>
                <td><input type="text" name="description[<?php echo $row['Id']; ?>]" value="<?php echo $row['Description']; ?>" /></td>
                <td><input type="text" name="nostudent[<?php echo $row['Id']; ?>]" value="<?php echo $row['NoStudent']; ?>" /></td>
                <td><input type="text" name="duedate[<?php echo $row['Id']; ?>]" value="<?php echo $row['DueDate']; ?>" /></td>
                </tr>
                <?php
            }
            ?>
            </table>
            <input type="reset" value="clear" />
            <input type="submit" name="update" value="update" />
        </form>

        <br />
        <div align='center'>
        </div>
        <?php
            mysqli_close($dbCIE);
    }
    else{
        echo "must logout to see this page..!!";
    }

?>

</body>
</html>