简体   繁体   English

PHP MySQL选择多个条件

[英]PHP MySQL select with multiple conditions

I need to select rows from my database using multiple conditions. 我需要使用多个条件从我的数据库中选择行。

Now I have done a lot of searching but was unable to find any examples similar to mine. 现在我已经做了很多搜索,但是找不到任何类似于我的例子。

Please don't swarm me like sharks about SQL injections and stuff as I'm in my second week of learning PHP and what I'm making will only be used internally. 请不要像鲨鱼一样关注SQL注射和内容,因为我正在学习PHP的第二周,而我正在制作的内容只会在内部使用。

The Form: 表格:

<form action="<?php echo $_server['php_self']; ?>" method="post">
    Display results from <input name="from_date" class="src_drop" type="textarea" id="from_date">
    to <input name="to_date" class="src_date" type="textarea" id="to_date"> 
    for <select class="src_drop" name="uniqueid" type="text">

    <?php 
        $sql   = "select * from table_staff";
        $staff = mysqli_query($connection, $sql);
        while($stafflist = mysqli_fetch_array( $staff )) 
        {
            echo '<option value="' . $stafflist['uniqueid'] . '">' . $stafflist['first_name'] . " " . $stafflist['surname'] . '</option>';
        } 
    ?>
    </select>
    <input type="submit" name="submit" class="search" value="">
</form>

The SELECT: SELECT:

$startDate = date("Y-m-d", strtotime($_POST['from_date']));
$endDate   = date("Y-m-d", strtotime($_POST['to_date']));
$uniqueid  = $_POST['uniqueid'];

$shiftdata = mysqli_query($connection, "SELECT * FROM table_shift 
    INNER JOIN table_staff ON table_shift.uniqueid = table_staff.uniqueid 
         WHERE shift_date BETWEEN '".$startDate."' AND '".$endDate."
           AND table_shift.uniqueid='".$uniqueid."''
      ORDER by shift_date ASC");

I'm trying to display all shifts for specific staff member between certain dates. 我试图在特定日期之间显示特定工作人员的所有班次。 The code works fine for selecting shifts between the dates, but breaks the connection when adding select by uniqueid . 该代码适用于选择日期之间的转换,但在通过uniqueid添加select时会中断连接。 I've tried a couple different (guesses) variations of syntax eg AND WHERE table_shift.unqiueid ... without any luck. 我尝试了几种不同的(猜测)语法变体,例如AND WHERE table_shift.unqiueid ......没有任何运气。

The Error: 错误:

You have an error in your SQL syntax; 您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '15'' ORDER by shift_date ASC' at line 1 检查与您的MySQL服务器版本对应的手册,以便在第15行使用'15'附近的正确语法ORDER by shift_date ASC'

Have you tried removing the single quotes around 你有没有尝试删除周围的单引号

..AND table_shift.uniqueid='".$uniqueid."''

so that it looks like 所以它看起来像

..AND table_shift.uniqueid=".$uniqueid."

This assumes that your uniqueid field is an integer, not a string. 这假设您的uniqueid字段是整数,而不是字符串。

Try this.. (and I agree with others about securing your data WITH PREPARED STATEMENTS) 试试这个..(我同意其他人关于用准备好的声明来保护你的数据)

$startDate = date("Y-m-d", strtotime($_POST['from_date']));
$endDate = date("Y-m-d", strtotime($_POST['to_date']));
$uniqueid = $_POST['uniqueid'];
$query = "
SELECT * 
  FROM table_shift x
  JOIN table_staff y
    ON x.uniqueid = y.uniqueid 
 WHERE x.shift_date BETWEEN '$startDate' AND '$endDate'
   AND x.uniqueid = $uniqueid
 ORDER 
    BY x.shift_date ASC;
";

echo $query;
$shiftdata = mysqli_query($connection, $query) or die(mysqli_error($connection));

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

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