我在使用$ _POST从mysql数据库获取数据时遇到麻烦

时间:2019-07-12 13:16:25

标签: php mysql

我无法从数据库中获取需要显示的数据。

<label style="font-size:20px"><b>Doctor:</b></label><br>
        <select name="doctor" id="doctor-list" class="demoInputBox" style="width:100%;height:35px;border-radius:9px">
        <option value="">Select Doctor</option>
        <?php
        session_start();
        $mid=$_SESSION['mgrid'];
        $sql1="SELECT * FROM doctor where did in(select did from doctor_availability where cid in (select cid from manager_clinic where mid=$mid));";
         $results=$conn->query($sql1); 
        while($rs=$results->fetch_assoc()) { 
        ?>
        <option value="<?php echo $rs["did"]; ?>"><?php echo "Dr. ".$rs["name"]; ?></option>
        <?php
        }
        ?>
        </select>
        <br>

        <label><b>Date:</b></label><br>
        <input type="date" name="dateselected" required><br><br>
        <br>
            <button type="submit" style="position:center" name="submit" value="Submit">Submit</button>
            </form>
<?php
if(isset($_POST['submit']))
{

        include 'dbconfig.php';
        $did=$_POST['doctor'];
        $cid=1;
        $dateselected=$_POST['dateselected'];
        $sql1 = "SELECT * FROM book WHERE DOV='". $_POST['dateselected']."' AND DID= $did AND CID= $cid order by Timestamp ASC";
         $results1=$conn->query($sql1); 
            require_once("dbconfig.php");
?>      

我有一个表book,那里有很多值。如果我选择日期,则必须在日期相同的地方显示所有相同的值。

$cid=1;

[书桌1

doctor_availability

it wont display the expected output

1 个答案:

答案 0 :(得分:1)

我不确定,但是我认为您需要JOIN,并且肯定需要准备好的语句。请参阅下面的代码。我使用JOIN将3个表连接在一起,然后使用您的会话值来匹配mid

session_start();
if (isset($_POST['submit'])) {
    include 'dbconfig.php';

    $stmt = $conn->prepare('SELECT * FROM book 
        JOIN doctor_availability USING(did)
        JOIN manager_clinic USING(cid)
        WHERE dov=? AND did=? AND mid=?');
    $stmt->bind_param('sii', $_POST['dateselected'], $_POST['doctor'], $_SESSION['mgrid']);
    $stmt->execute();
    $results1 = $stmt->get_result();
}