第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

PHP / SQL:多次刪除不成功

PHP / SQL:多次刪除不成功

PHP
手掌心 2022-10-28 14:46:58
目前我已經(jīng)創(chuàng)建了一個(gè)系統(tǒng),該系統(tǒng)可以對(duì)從 SQL 數(shù)據(jù)庫中獲取并顯示在dashboard_engineer.php. 每個(gè)數(shù)據(jù)行都包含一個(gè)刪除按鈕。每行還有一個(gè)復(fù)選框,使用戶能夠刪除選定的數(shù)據(jù)行。在數(shù)據(jù)行的底部,有一個(gè)用于刪除所有選定數(shù)據(jù)行的按鈕。我的問題是多次刪除,比如說,如果我選擇兩個(gè)數(shù)據(jù)行(id 4 和 5),它只會(huì)刪除一個(gè) ID。下面是我的代碼儀表板_engineer2.php<?php    if(isset($_REQUEST['from'], $_REQUEST['to'], $_REQUEST['team'])){    $from = $_REQUEST['from'];    $to   = $_REQUEST['to'];    $team = $_REQUEST['team'];    $result = '';    $query = "SELECT * FROM ot_report LEFT JOIN ot_users ON ot_report.badgeid = ot_users.badgeid     WHERE ot_report.status = 'yes' AND ot_users.team_id = :team AND report_date BETWEEN :from     AND :to ORDER BY ot_report.report_id DESC";    $sql = $conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));    $sql->bindParam(':team', $team);    $sql->bindParam(':from', $from);    $sql->bindParam(':to', $to,);    $sql -> execute();     if($sql->rowCount() > 0){    echo'        <table class = "table-bordered" width = "100%">        <thead>        <tr>        <th width = "10%"><input type="checkbox" id="checkAl"> All</th>        <th width = "3%">id</th>        <th width = "15%">Date</th>        <th width = "25%">Supervisor</th>        <th width = "30%">Task Name</th>        <th width = "10%">Status</th>        <th colspan = "2" width = "7%">Action</th>        </tr>        </thead>        <tbody>';            $i=0;            while($row = $sql->fetch(PDO::FETCH_ASSOC)){            $status=$row['report_status'];            if($status=="Pending"){                $color="color:blue";            }            else {                $color="color:green";            }            }誰能幫我解決這個(gè)問題?謝謝
查看完整描述

1 回答

?
狐的傳說

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

好的 - 以下是基于問題的代碼,但為了構(gòu)建一個(gè)工作演示,它已經(jīng)被概括了。這里重要的是使用 Javascript 來維護(hù)選定的 ID 列表(通過單擊select-all復(fù)選框或單擊單個(gè)復(fù)選框。請(qǐng)忽略表單中生成的內(nèi)容 - 日期對(duì)于本示例而言是虛假且無意義的。


您可以復(fù)制它并創(chuàng)建一個(gè)應(yīng)該可以正常運(yùn)行的測(cè)試頁面 - 生成偽 SQL 的 PHP 只是顯示將要執(zhí)行的語句,而不是它應(yīng)該如何執(zhí)行(應(yīng)該按照問題,prepared statement)


<?php

    if( $_SERVER['REQUEST_METHOD']=='POST' ){

        if( isset( $_POST['ids'] ) ){

            $items=explode( ',', $_POST['ids'][0] );


            foreach($items as $id){

                $sql=sprintf('UPDATE ot_report SET status="no" WHERE report_id=%d',$id);

                echo $sql . '<br />';

            }

        }

    }

?>

<!DOCTYPE html>

<html>

    <head>

        <meta charset='utf-8' />

        <title></title>

    </head>

    <body>

        <table>

            <?php


                for( $i=1; $i <= 10; $i++ ){

                    /* example data for dates... */

                    $hrs=mt_rand(1,24);

                    $mins=mt_rand(0,60);

                    $secs=mt_rand(0,60);


                    $month=mt_rand(1,12);

                    $day=mt_rand(1,28);

                    $year=mt_rand(2000,2020);


                    $from=date('y-m-d',mktime($hrs,$mins,$secs,$month,$day,$year));

                    $to=date('y-m-d',mktime($hrs+3,$mins+20,$secs,$month,$day,$year));


                    printf('

                    <tr>

                        <td><input type="checkbox" name="check[]" value="%1$d" /></td>

                        <td>Some data %1$d</td>

                        <td>

                            <form method="post">

                                <input type="hidden" name="from" value="%2$s" />

                                <input type="hidden" name="to" value="%3$s" />

                                <input type="hidden" name="id" value="%1$d" />

                                <input type="submit" />

                            </form>

                        </td>

                    </tr>', $i, $from, $to );

                }


            ?>

        </table>

        <form method='post'>

            <input type='checkbox' name='selectall' value='Select-All' />

            <input type='hidden' name='ids[]' />

            <input type='button' name='sub-delete' value='Update selected' />

        </form>

        <script>

            let ids=document.querySelector('input[type="hidden"][name="ids[]"]');

            let tmp=[];


            document.querySelector('[name="selectall"]').addEventListener('change',function(e){

                let col=document.querySelectorAll('[name="check[]"]');

                col.forEach(chk=>{

                    chk.checked=this.checked;

                    if( this.checked )tmp.push(chk.value)

                    else tmp.splice(tmp.indexOf(chk.value),1)

                });

            });


            document.querySelectorAll('[name="check[]"]').forEach( chk=>{

                chk.addEventListener('change',function(e){

                    if( this.checked )tmp.push(this.value)

                    else tmp.splice( tmp.indexOf(this.value),1);

                });

            });


            document.querySelector('[name="sub-delete"]').addEventListener('click',function(e){

                ids.value=tmp.join(',')

                this.parentNode.submit()

            });

        </script>

    </body>

</html>

將產(chǎn)生類似于:


UPDATE ot_report SET status="no" WHERE report_id=10

UPDATE ot_report SET status="no" WHERE report_id=8

UPDATE ot_report SET status="no" WHERE report_id=6

UPDATE ot_report SET status="no" WHERE report_id=4

快速嘗試使用您的代碼調(diào)整此示例:


<?php

    if(isset($_REQUEST['from'], $_REQUEST['to'], $_REQUEST['team'])){

        $from=$_REQUEST['from'];

        $to  =$_REQUEST['to'];

        $team=$_REQUEST['team'];


        $result='';

        $query="SELECT * FROM ot_report 

                LEFT JOIN ot_users ON ot_report.badgeid=ot_users.badgeid 

                WHERE ot_report.status='yes' AND ot_users.team_id=:team AND report_date BETWEEN :from AND :to 

                ORDER BY ot_report.report_id DESC";


        $sql=$conn->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));

        $sql->bindParam(':team', $team);

        $sql->bindParam(':from', $from);

        $sql->bindParam(':to', $to);

        $sql->execute();






        if($sql->rowCount() > 0){

            echo'

            <table class="table-bordered" width="100%">

                <thead>

                <tr>

                    <th width="10%"><input type="hidden" name="selectall" value="Select-All">All</th>

                    <th width="3%">id</th>

                    <th width="15%">Date</th>

                    <th width="25%">Supervisor</th>

                    <th width="30%">Task Name</th>

                    <th width="10%">Status</th>

                    <th colspan="2" width="7%">Action</th>

                </tr>

            </thead>

            <tbody>';


            $i=0;


            while($row=$sql->fetch(PDO::FETCH_ASSOC)){

                $status=$row['report_status'];


                if($status=="Pending"){

                    $color="color:blue";

                } else {

                    $color="color:green";

                }


                $report_id=$row["report_id"];


                echo'

                <tr>

                    <td><input type="checkbox" name="check[]" value="'.$report_id.'"></td>

                    <td>'.$report_id.'</td>

                    <td>'.$row['report_date'].'</td>

                    <td>'.$row["fullname"].'</td>

                    <td>'.$row["task_name"].'</td>

                    <td style='.$color.'><strong>'.$status.'</strong></td>


                    <td>

                        <form action="view_task/view_task.php" method="post" target="_blank">

                            <input type="hidden" name="report_id" value="'.$report_id.'">

                            <button type="submit" class="btn-primary">View</button>

                        </form>

                    </td>


                    <td>

                        <form action="remove2.php" method="post" onsubmit="return confirm(\'Do you want to remove this reports?\')">

                            <input type="hidden" name="from" value="'.$from.'">

                            <input type="hidden" name="to" value="'.$to.'">

                            <input type="hidden" name="team" value="'.$team.'">

                            <input type="hidden" name="report_id" value="'.$report_id.'">

                            <button type="submit" class="btn-danger">Remove</button>

                        </form>

                    </td>

                </tr>';


                $i++;

            }


            echo '

            <tr>

                <td>

                    <form action="delete_selected.php" method="post">

                        <input type='hidden' name='ids[]' />

                        <input type='button' name='sub-delete' value='DELETE' />

                    </form>

                </td>

            </tr>';

        }

    }

?>

<script>

    let ids=document.querySelector('input[type="hidden"][name="ids[]"]');

    let tmp=[];


    document.querySelector('[name="selectall"]').addEventListener('change',function(e){

        let col=document.querySelectorAll('[name="check[]"]');

        col.forEach(chk=>{

            chk.checked=this.checked;

            if( this.checked )tmp.push(chk.value)

            else tmp.splice(tmp.indexOf(chk.value),1)

        });

    });


    document.querySelectorAll('[name="check[]"]').forEach( chk=>{

        chk.addEventListener('change',function(e){

            if( this.checked )tmp.push(this.value)

            else tmp.splice( tmp.indexOf(this.value),1);

        });

    });


    document.querySelector('[name="sub-delete"]').addEventListener('click',function(e){

        ids.value=tmp.join(',')

        this.parentNode.submit()

    });

</script>


delete_selected.php


<?php


    include("../../../config/configPDO.php");

    include("../../../config/check.php");



    $checkbox=explode( ',', $_POST['ids'][0] );


    for($i=0;$i < count($checkbox); $i++){

        $report_id=$checkbox[$i];


        $sql="UPDATE ot_report SET status='no' WHERE report_id=:report_id";

        $query=$conn->prepare($sql);

        $query->execute(array(':report_id' => $report_id));


        header("Location: dashboard_engineer.php");


    }


?>


查看完整回答
反對(duì) 回復(fù) 2022-10-28
  • 1 回答
  • 0 關(guān)注
  • 126 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)