简体   繁体   English

复选框未使用PHP在按按钮时更新SQL查询

[英]Checkbox not updating SQL query on button press using PHP

With this code, it can retrieve the values from the database with a checkbox on each row. 使用此代码,它可以从数据库中检索值,并且每行都有一个复选框。 What I want for it to do is to update the unchecked values (namely 0) in the database with 1 for each checkbox checked. 我要执行的操作是为每个选中的复选框用1更新数据库中未选中的值(即0)。

Here's the query for the database and some sample rows. 这是数据库查询和一些示例行。

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 - the class file 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);
        }
    }

}
?>

I'm mostly having problems with the function complete_orders, which doesn't return anything on button press of the submit button. 我主要遇到的是complete_orders函数的问题,该函数在按下提交按钮时不返回任何内容。 Nor does it check if the checkboxes are ticked. 它也不检查复选框是否被勾选。

Here's the HTML layout file 这是HTML布局文件

jobrequestfilter.php 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>

The isset button returns the echo statement I put, however, most seems to be working fine except for the process order button. isset按钮返回我输入的echo语句,但是,除了流程顺序按钮之外,大多数似乎工作正常。 Is it wise to just use javascript for the checkbox on update? 仅将javascript用于更新复选框是否明智?

Please replace below complete_orders function code 请替换下面的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
        }
    }

Two problem in code: 代码中的两个问题:

  1. Function argument $orders you are passing but while process you using order. 您传递的函数参数$ orders,但在处理过程中使用order。 So it's was not going inside into loop 所以它并没有陷入循环
  2. in foreach iteration your are using ids but while updating query you using id. 在foreach迭代中,您使用的是ID,但在更新查询时使用的是ID。 so change variable accordingly. 因此相应地更改变量。 Please check 请检查

Try this one 试试这个

db_c.php db_c.php

  1. It should not be 不应该

$order['order-complete'] $ order ['order-complete']

But

$orders $订单

Because the array variable name from post already stored in variable $orders. 因为帖子中的数组变量名称已经存储在变量$ orders中。

  1. It should not be 不应该

$id $ id

But

$ids $ ids

Because you declare it as 因为您声明为

foreach($orders as $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 jobrequestfilter.php

3.It should not 3,不应该

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

It should be 它应该是

if(isset($_POST['order-complete']) && isset($_POST['process_orders'])){ $process = $dbc->complete_orders($_POST['order-complete']); 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM