php - 我可以在 SQL 中使用 AND 运算符 2 次吗?

标签 php mysql

所以我已经尝试了一周,在我选择日期的提交按钮后,表内的值丢失了

所以当进入这个页面时会显示

'SELECT * FROM attendance WHERE lect_id = 'CS0198289' AND dateofupdate LIKE '%0%' AND codeofsubject = 'CSNB214' ORDER BY studname ASC'

它将显示我的 lect_id、dateofupdate=0 和主题代码。当我选择提交(选择)按钮时,它会变成

'SELECT * FROM attendance WHERE lect_id = 'CS0198289' AND dateofupdate LIKE '%2019-01-23%' AND codeofsubject = '' ORDER BY studname ASC'

那么有人可以帮助我吗?非常感谢

<?php
                $connect = mysqli_connect ("localhost","root","","ubas")
                or die("Cannot connect to server".mysqli_error($connect));

                if (!isset($_POST['dates'])){
                  $_POST['dates']=0;
                }
                $id = @$_POST["lect_id"];
                $codeofsubject = @$_POST['code'];

                $display = "SELECT * FROM attendance WHERE lect_id = '$_SESSION[id]' AND dateofupdate LIKE '%".$_POST['dates']."%'
                AND codeofsubject = '$codeofsubject' ORDER BY studname ASC";
                echo $display;
                $result = mysqli_query ($connect,$display)
                or die ("Cannot view due to".mysqi_error($connect));

                echo"<form role='form' method='post'>
                        <div class='form-group'>
                          <label>Date</label>
                          <input style='width:180px; display:inline' class='form-control' type='date' name='dates' id='dates'>
                          <button type='submit' style='display:inline' name='select'>Select</button>
                      </div>
                    </form>";

                echo"
                <form method = post action = updateattend.php>
                <table width='100%' class='table table-striped table-bordered table-hover' id='dataTables-example'>
                      <thead>
                          <tr>
                              <th><center>Date</center></th>
                              <th><center>Student Name</center></th>
                              <th><center>Student ID</center></th>
                              <th><center>View</center></th>
                              <th><center>Attend Status</center></th>
                          </tr>
                      </thead>";
                      while($row = mysqli_fetch_array($result,MYSQLI_NUM))
                      {
                        $attendid = $row [0];
                        $studname = $row [1];
                        $studid = $row [2];
                        $lect_id = $row [3];
                        $codeofsubject = $row [4];
                        $date = $row [5];
                        $dateofenroll = $row [6];
                        $attendstatus = $row [7];

                          echo"<tbody>
                          <tr>
                          <td><center>$date</center></td>
                          <td><center>$studname</center></td>
                          <td><center>$studid</center></td>
                          <td align = center>
                          <a class ='btn-warning btn' href ='updatestud.php?id=".$row['2']."'>VIEW</a>
                          </td>";
                          echo"

                          <td align = center>
                          <select class = 'form-control' name = 'attendstatus[]'>
                          <option value='$attendstatus' selected>$attendstatus</option>";

                          if($attendstatus =="Attend")
                          {
                            echo "<option value='Not Attending'>Not Attending</option>
                            <option value='Not Attending with Reason'>Not Attending with Reason</option>";
                          }
                          elseif ($attendstatus =="Not Attending") {
                            echo"<option value='Attend'>Attend</option>
                            <option value='Not Attending with Reason'>Not Attending with Reason</option>";
                          }
                          else{
                            echo"<option value='Attend'>Attend</option>
                            <option value='Not Attending'>Not Attending</option>";
                          }

                          echo"</select>
                          </td>
                        </tr>
                        </tbody>";
                      }

                      ?>
                </table>

最佳答案

SQL 语句中的 AND 运算符没有限制。您可以根据需要使用任意多个 AND,但您的逻辑在 SQL 语句的意义上应该是正确的。

例如

SELECT something FROM something WHERE something=something AND something=something AND something=something AND something=something;

因此,something=something 部分成为一个单独的逻辑语句,基于 AND 与其他 something=something 语句分开评估运算符(operator)。但请记住,上面的整个声明只是一个声明。

关于php - 我可以在 SQL 中使用 AND 运算符 2 次吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54377237/

相关文章:

php - 在 PHP 中以不同的方式选择关联数组中的元素

PHP 警告 : Module 'mcrypt' already loaded

php - 单击客户时不显示复选框值

Mysql DELETE Where 1=1 AND 2=2

php - 高效安全的查询思路——mysql

php - 将 XML 文件中的数据插入 MySql DB

php - 在 onchange 上调用 2 个 JS 函数并且都返回一些值

mysql - 左加入。字段列表中的列 'product_id' 不明确

php - 代表用户从 Instagram 获取公共(public)内容

sql - 如何查看数据库上当前正在运行的查询