复选框无法使用PHP

时间:2018-12-18 04:58:08

标签: php html html5

使用此代码,它可以从数据库中检索值,并且每行都有一个复选框。我要执行的操作是,为每个选中的复选框将数据库中未选中的值(即0)更新为1。

这是对数据库和一些示例行的查询。

CREATE TABLE IF NOT EXISTS `job_order` (
`ID` int(255) NOT NULL AUTO_INCREMENT,
`SI_no` varchar(12) NOT NULL DEFAULT '1',
`Date_Issued` date NOT NULL,
`Date_completed` date DEFAULT NULL,
`checked` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;


INSERT INTO `job_order` (`ID`, `SI_no`, `Date_Issued`, `Date_completed`, 
`checked`) VALUES
(1, '2', '2018-12-19', '2018-12-26', 1),
(2, '5', '2018-11-06', '2018-12-04', 1),
(3, '7', '2018-12-01', '2018-12-13', 0),
(4, '8', '2018-12-20', '2018-12-12', 0);
 COMMIT;

db_c.php-类文件

<?php

define ( 'DB_HOST', 'localhost' );
define ( 'DB_USER', 'root' );
define ( 'DB_PASS', '' );
define ( 'DB_NAME', 'db_name' );


class db_c{  
    public $mysqli;
    function __construct() {
          $this->mysqli = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
          if(!$this->mysqli){
            die('Could not Connect My Sql:' .mysql_error());
          }
    }
    function complete_orders($orders){
        $processed = array();
        if(is_array($orders) && !empty($orders)){
            if(isset($order['order-complete'])){
                foreach($order['order-complete'] as $ids){
                    $sql = "UPDATE `job_order` SET `checked`= 1 WHERE `ID` = ?";
                    if($stmt = $this->mysqli->prepare($sql)){
                        $stmt->bind_param("i", $id);
                            if($stmt->execute()){
                               array_push($processed, $id);  
                            }
                    }
                }
                return $processed;
            }else{
                 echo '<script>console.log("Nothing returned line 32")</script>';
                return 0; //No orders selected
            }
        }else{
              echo '<script>console.log("Nothing processed")</script>';
            return 0; //Nothing to process
        }
    }
    function return_orders(){
        $orders = array();
        $sql = "SELECT `ID`, `SI_no`, `date_issued`, `date_completed`, `checked` FROM `job_order` WHERE `checked` != 1";
        if($stmt = $this->mysqli->prepare($sql)){
                if($stmt->execute()){
                    $stmt->bind_result($ID, $SI_no, $date_issued, $date_completed, $checked);
                    $stmt->store_result();
                    while($stmt->fetch()){
                           $orders[$ID]['SI_no'] = $SI_no;
                           $orders[$ID]['Issued'] = $date_issued;
                           $orders[$ID]['Completed'] = $date_completed;
                           $orders[$ID]['Checked'] = $checked;
                    }
                    return $orders;
                }else{
                    return 1;
                // failed to execute
                }
        }else{
            return 0;
            // failed to prepare
        }
    }
    function orders_2_table(){
        $unchecked = $this->return_orders();
        if(is_array($unchecked) && !empty($unchecked)){
            //returned results, build rows
            $table = '';
            foreach($unchecked as $id => $dets){
              $table .= '<tr><td>'.$dets['SI_no'].'</td><td>'.$dets['Issued'].'</td><td>'.$dets['Completed'].'</td><td><input type="checkbox" name="order-complete[]" value="'.$id.'"  /></td></tr>';  
            }
            return array('Rows'=>$table, 'Count'=>count($unchecked));
        }elseif(!is_array($unchecked)){
            if($unchecked === 0){
                return array('Rows'=>'<tr><td colspan="3">Error (SQL) </td></tr>', 'Count'=>0);
            }else{
                return array('Rows'=>'<tr><td colspan="3">Error (EXE) </td></tr>', 'Count'=>0);
            }
        }else{
            return array('Rows'=>'<tr><td colspan="3">All Orders Completed </td></tr>', 'Count'=>0);
        }
    }

}
?>

我在使用complete_orders函数时经常遇到问题,在按下提交按钮时该函数不会返回任何内容。也不会检查复选框是否被选中。

这是HTML布局文件

jobrequestfilter.php

<?php 

session_start();
include 'db_c.php';

$dbc = new db_c();
$msg = '';
if(isset($_POST) && isset($_POST['process_orders'])){
    $process = $dbc->complete_orders($_POST);
    if(is_array($process) && !empty($process)){
        $msg = '<tr><td colspan="3">Successfully Processed '.count($process).' Orders</td></tr>';
    }
    else{
        echo '<script>console.log("Nothing processed at jobrequestfilter")</script>';
     }
}

$data = $dbc->orders_2_table();

?>
<html>
    <head>
        <meta charset="utf-8">
        <title>Job Request Chart</title>
    </head>
    <body>
        <div id="navbar">
            <div id ="wrap">
                <div class="logo"></div> 
                <img id="b" class="b">
            </div>
        </div>
        <form action="" method="post">
            <div id="filterby">
               <input type="submit" id="Email" class="requestbutton" name="Email" value="Email">
            </div>
        </form>
        <form method="post"  enctype="multipart/form-data">
            <table id ="jobtable">
                <tr><th>SI no.</th><th>Date Issued</th><th>Date Started </th><th>Approve?</th></tr>
                <?php echo $msg ?> 
                <?php echo $data['Rows'] ?>
                <tr><td colspan="2"><input type="submit" name="process_orders" value="Process Orders" /></td><td>Count:<?php echo $data['Count'] ?></td></tr>
            </table>
        </form>
    </body>
</html>

isset按钮返回我输入的echo语句,但是,除了流程顺序按钮之外,大多数似乎工作正常。仅将javascript用于更新复选框是否明智?

2 个答案:

答案 0 :(得分:0)

请替换下面的complete_orders功能代码

function complete_orders($orders){
        $processed = array();
        if(is_array($orders) && !empty($orders)){
            if(isset($orders['order-complete'])){       
                foreach($orders['order-complete'] as $id){
                    $sql = "UPDATE `job_order` SET `checked`= 1 WHERE `ID` = ?";
                    if($stmt = $this->mysqli->prepare($sql)){
                        $stmt->bind_param("i", $id);
                            if($stmt->execute()){
                               array_push($processed, $id);  
                            }
                    }
                }       
                return $processed;
            }else{
                 echo '<script>console.log("Nothing returned line 32")</script>';
                return 0; //No orders selected
            }
        }else{
              echo '<script>console.log("Nothing processed")</script>';
            return 0; //Nothing to process
        }
    }

代码中的两个问题:

  1. 您正在传递的函数参数$ orders,但在使用order处理过程中。所以它并没有陷入循环
  2. 在foreach迭代中,您使用的是ID,但在更新查询时使用的是ID。因此相应地更改变量。请检查

答案 1 :(得分:0)

尝试这个

db_c.php

  1. 不应该
  

$ order ['order-complete']

但是

  

$ orders

因为帖子中的数组变量名称已经存储在变量$ orders中。

  1. 不应该
  

$ id

但是

  

$ ids

因为您将其声明为

  

foreach($ orders为$ ids)

<?php

define ( 'DB_HOST', 'localhost' );
define ( 'DB_USER', 'root' );
define ( 'DB_PASS', '' );
define ( 'DB_NAME', 'your_db_name' );


class db_c{  
    public $mysqli;
    function __construct() {
          $this->mysqli = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
          if(!$this->mysqli){
            die('Could not Connect My Sql:' .mysql_error());
          }
    }
    function complete_orders($orders){
        $processed = array();
        if(is_array($orders) && !empty($orders)){
            if(isset($orders)){
                foreach($orders as $ids){
                    $sql = "UPDATE `job_order` SET `checked`= 1 WHERE `id` = ?";
                    if($stmt = $this->mysqli->prepare($sql)){
                        $stmt->bind_param("i", $ids);
                            if($stmt->execute()){
                               array_push($processed, $ids);  
                            }
                    }
                }
                return $processed;
            }else{
                 echo '<script>console.log("Nothing returned line 32")</script>';
                return 0; //No orders selected
            }
        }else{
              echo '<script>console.log("Nothing processed")</script>';
            return 0; //Nothing to process
        }
    }
    function return_orders(){
        $orders = array();
        $sql = "SELECT `ID`, `SI_no`, `date_issued`, `date_completed`, `checked` FROM `job_order` WHERE `checked` != 1";
        if($stmt = $this->mysqli->prepare($sql)){
                if($stmt->execute()){
                    $stmt->bind_result($ID, $SI_no, $date_issued, $date_completed, $checked);
                    $stmt->store_result();
                    while($stmt->fetch()){
                           $orders[$ID]['SI_no'] = $SI_no;
                           $orders[$ID]['Issued'] = $date_issued;
                           $orders[$ID]['Completed'] = $date_completed;
                           $orders[$ID]['Checked'] = $checked;
                    }
                    return $orders;
                }else{
                    return 1;
                // failed to execute
                }
        }else{
            return 0;
            // failed to prepare
        }
    }
    function orders_2_table(){
        $unchecked = $this->return_orders();
        if(is_array($unchecked) && !empty($unchecked)){
            //returned results, build rows
            $table = '';
            foreach($unchecked as $id => $dets){
              $table .= '<tr><td>'.$dets['SI_no'].'</td><td>'.$dets['Issued'].'</td><td>'.$dets['Completed'].'</td><td><input type="checkbox" name="order-complete[]" value="'.$id.'"  /></td></tr>';  
            }
            return array('Rows'=>$table, 'Count'=>count($unchecked));
        }elseif(!is_array($unchecked)){
            if($unchecked === 0){
                return array('Rows'=>'<tr><td colspan="3">Error (SQL) </td></tr>', 'Count'=>0);
            }else{
                return array('Rows'=>'<tr><td colspan="3">Error (EXE) </td></tr>', 'Count'=>0);
            }
        }else{
            return array('Rows'=>'<tr><td colspan="3">All Orders Completed </td></tr>', 'Count'=>0);
        }
    }

}
?>

jobrequestfilter.php

3。不应

  

if(isset($ _ POST)&& isset($ _ POST ['process_orders'])){       $ process = $ dbc-> complete_orders($ _ POST);

应该是

  

if(isset($ _ POST ['order-complete'])&&   isset($ _ POST ['process_orders'])){       $ process = $ dbc-> complete_orders($ _ POST ['order-complete']);

<?php 

session_start();
include 'db_c.php';

$dbc = new db_c();
$msg = '';
if(isset($_POST['order-complete']) && isset($_POST['process_orders'])){
    $process = $dbc->complete_orders($_POST['order-complete']);
    if(is_array($process) && !empty($process)){
        $msg = '<tr><td colspan="3">Successfully Processed '.count($process).' Orders</td></tr>';
    }
    else{
        echo '<script>console.log("Nothing processed at jobrequestfilter")</script>';
     }
}

$data = $dbc->orders_2_table();

?>
<html>
    <head>
        <meta charset="utf-8">
        <title>Job Request Chart</title>
    </head>
    <body>
        <div id="navbar">
            <div id ="wrap">
                <div class="logo"></div> 
                <img id="b" class="b">
            </div>
        </div>
        <form action="" method="post">
            <div id="filterby">
               <input type="submit" id="Email" class="requestbutton" name="Email" value="Email">
            </div>
        </form>
        <form method="post"  enctype="multipart/form-data">
            <table id ="jobtable">
                <tr><th>SI no.</th><th>Date Issued</th><th>Date Started </th><th>Approve?</th></tr>
                <?php echo $msg ?> 
                <?php echo $data['Rows'] ?>
                <tr><td colspan="2"><input type="submit" name="process_orders" value="Process Orders" /></td><td>Count:<?php echo $data['Count'] ?></td></tr>
            </table>
        </form>
    </body>
</html>