简体   繁体   English

php和mysql查询问题

[英]Php and mysql query issue

I have a simple table like this: table . 我有一个像这样的简单表: table A form submits data to check in this table. 表单提交数据以检查此表。

    <form action="" method="GET">
    <a href="">From</a>
    <select name="startpoint" id="from">
        <option >Hat Yai Airport</option>
        <option >Pak Bara</option>
        <option >Kohlipe</option>
    </select>
    <a href="">To</a>
    <select name="endpoint" id="to">
        <option >Pak Bara</option>
        <option >Hat Yai Airport</option>
        <option >Kohlipe</option>
    </select>
    <label for="Date">Date</label>
    <input type="text" name="date_up" id="datepicker">
    <h4 style="margin-top: 30px;">Passengers</h4>
    <a href="">Adults</a>
    <select name="adult" id="from">
        <option >1</option>
        <option >2</option>
        <option >3</option>
        <option >4</option>
        <option >5</option>
    </select>

    <a href="">< 12 years</a>
    <select name="juvenile" id="to">
        <option >0</option>
        <option >1</option>
        <option >2</option>
        <option >3</option>
        <option >4</option>
    </select>
        <a href=""> < 7 years</a>
    <select name="kids" id="from">
        <option >0</option>
        <option >1</option>
        <option >2</option>
        <option >3</option>
    </select>
    <a href=""> < 3 years</a>
    <select name="child" id="to">
        <option >0</option>
        <option >1</option>
        <option >2</option>
        <option >3</option>
    </select>
    <input type="submit" value="Search" class="submit" name="submit">

And this is the initial i have made to check this table. 这是我查看此表的初步内容。

     $servername = "localhost";
     $username = "root";
     $password = "";
     $dbname = "hello";


// Create connection
 $conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
 }      

      if (isset($_GET['submit'])) {


            $date = $_GET['date_up'];
            $startPlace = $_GET['startpoint'];
            $endPlace = $_GET['endpoint'];
            $adult = $_GET['adult'];
            $juvenile = $_GET['juvenile'];
            $kids = $_GET['kids'];
            $child = $_GET['child'];

            $totalPassenger = $adult + $juvenile + $kids + $child;




            $query = "SELECT * FROM taxi WHERE date_up = '$date'";


            $result = mysqli_query($conn, $query);


            if (!$result) {
            echo "Could not successfully run query from DB: " . mysqli_error();
                exit;
            }

            if (mysqli_num_rows($result) == 0) {
                $nodate = "No date matches for speedboat";
                echo $nodate;

            } else {
                while ($row = mysqli_fetch_assoc($result)) {
                $start = $row["startpoint"];
                $end = $row["endpoint"];
                $standards= $row["standards"];
                $deluxe= $row["deluxe"];


                //print_r($deluxe);
            } 

            if (($start === $startPlace && $end === $endPlace) && (what can be the right query?????)) {
                $success = "seats are found in taxi. total seat: user choses seat for $totalPassenger people";
                echo $success;
            } 
          }


        }

We have total 10 taxi(standard 5 , deluxe 5). 我们共有10辆出租车(标准5,豪华5)。 A taxi is for four people. 出租车是四个人。 I want to check if the total passenger is more than 4 people but less than 8 people and if this is true i want to check the standard or deluxe columns whether they both have at least two taxi(int 2) and return something. 我想检查总乘客是否超过4人但少于8人,如果这是真的,我想检查标准或豪华列是否他们都至少有两辆出租车(int 2)并返回一些东西。 If the total passenger is more than 8 but less than 12 i need to check both columns if they have at least 3 taxi and this will go on until i reach 20 people. 如果总乘客超过8但小于12,如果他们至少有3辆出租车需要检查两列,这将持续到20人。 Because if the total passenger is more than 16 but less than 20 we need 5 taxi and this is our limit. 因为如果总乘客超过16但少于20,我们需要5辆出租车,这是我们的限制。 If user provides more passenger after 20 we can return "not enough taxi for your query". 如果用户在20岁后提供更多乘客,我们可以返回“没有足够的出租车来查询”。 How can i check this pattern. 我该如何检查这种模式。

I also need to return at least one column result. 我还需要返回至少一列结果。 For example user searches with 7 passenger and i check and find that in deluxe column i have 2 taxi but in the standard column i have 1 taxi, so i need to return the deluxe column match result to the user(or to make the query more challenging we can return one standard taxi and one deluxe taxi right?). 例如用户搜索7位乘客和我检查并发现在豪华列我有2辆出租车但在标准列我有1辆出租车,所以我需要将豪华列匹配结果返回给用户(或使查询更多挑战我们可以返回一个标准出租车和一个豪华出租车吗?)。 How can i implement this scenario. 我该如何实现这种情况。 Please help me. 请帮我。

This code use ajax to fetch data without refreshing the page. 此代码使用ajax获取数据而不刷新页面。 and i have used PDO you can change it to mysqli 我使用PDO你可以把它改成mysqli

<?php  
$db = new PDO('mysql:host=localhost; dbname=data','root','');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$results = $db->query('SELECT * FROM taxi');
//this is a function for creating a table
function availableTaxis($results){ ?>
<table>
    <tr>
        <th>ID</th>
        <th>from</th>
        <th>to</th>
        <th>Available On</th>
        <th>standard</th>
        <th>deluxe</th>
    </tr>
    <?php foreach ($results as $value) : ?>
        <tr>
            <td><?php echo $value['taxi_id']; ?></td>
            <td><?php echo $value['startpoint']; ?></td>
            <td><?php echo $value['endpoint']; ?></td>
            <td><?php echo $value['date_up']; ?></td>
            <td><?php echo $value['standards'] .' ('.($value['standards'] * 4).' Passengers)'; ?></td>
            <td><?php echo $value['deluxe'].' ('.($value['deluxe'] * 4).' Passengers)'; ?></td>
        </tr>
    <?php endforeach; ?>
</table>
  <?php } ?> <!-- end of a function -->
  <!-- this is another function for getting taxi routes -->
  <?php function routes(){ ?>
  <br>
   <form>
   <a href="">From</a>
 <select name="startpoint" onchange="available(this.value,to.value)" id="from">
    <option value="Hat Yai Airport">Hat Yai Airport</option>
    <option >Pak Bara</option>
    <option >Kohlipe</option>
</select>
<a href="">To</a>
<select name="endpoint" onchange="available(from.value,this.value)" id="to">
    <option >Pak Bara</option>
    <option >Hat Yai Airport</option>
    <option >Kohlipe</option>
</select>
<p id="availabe_date"></p>
<div id="disable">
    <label for="Date">Date</label>
<input type="date" name="date_up" value="<?php echo date('Y-m-d'); ?>" id="datepicker" onchange="available(from.value,to.value)">
<h4 style="margin-top: 30px;">Passengers</h4>
<a href="">Adults</a>
<select name="adult" id="adult">
    <option >1</option>
    <option >2</option>
    <option >3</option>
    <option >4</option>
    <option >5</option>
</select>

<a href=""> 12 years</a>
<select name="juvenile" id="juvenile">
    <option >0</option>
    <option >1</option>
    <option >2</option>
    <option >3</option>
    <option >4</option>
</select>
    <a href="">  7 years</a>
<select name="kids" id="kids">
    <option >0</option>
    <option >1</option>
    <option >2</option>
    <option >3</option>
</select>
<a href="">  3 years</a>
<select name="child" id="child">
    <option >0</option>
    <option >1</option>
    <option >2</option>
    <option >3</option>
</select>
<br>
<p id="error" style="color:red"></p>
<br>
<input type="button" value="Search" onclick="results(datepicker.value,startpoint.value,endpoint.value,adult.value,juvenile.value,kids.value,child.value)" id="submit" name="submit">
</div>
</form>
<?php } ?><!-- end of a function -->
<!DOCTYPE html>
<html>
 <head>
<title></title>
<style type="text/css">
table,th,td{
    border: 1px solid #000;
}
th{
    width: 120px;
    background-color: #000;
    color: #fff;
    text-transform: capitalize;
}
table {
border-collapse: collapse;
}
</style>
<script type="text/javascript">
//creating ajax 
function available(from,to){
 var query = "from="+from+"&"+"to="+to;
var data = new XMLHttpRequest();
data.open("POST","answer.php");

data.onreadystatechange = function(){
  if(data.readyState === 4 && data.status === 200){
    document.getElementById('availabe_date').innerHTML = data.responseText;
    if(data.responseText.indexOf('is not avalaible') > -1){
      document.getElementById('disable').style.display = 'none';
    }else{
        document.getElementById('disable').style.display = 'inline';
        if(data.responseText.indexOf(datepicker.value) == -1){
             document.getElementById('error').style.display = 'inline';
            document.getElementById('error').innerHTML = 'There\'s no taxi on : '+datepicker.value +', from <span style="font-style:italic;text-decoration:underline">'+from+'</span> to <span style="font-style:italic;text-decoration:underline">'+to+'</span>';
            document.getElementById('submit').style.display = 'none';
            document.getElementById('res').style.display = 'none';
        }else{
            document.getElementById('error').style.display = 'none';
            document.getElementById('submit').style.display = 'inline';
            document.getElementById('res').style.display = 'inline';
            //document.getElementById('error').innerHTML = 'There\'s no taxi on : '+datepicker.value; 
        }
    }
  }
}
data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
data.send(query);
 }
  function results(date_up,startpoint,endpoint,adult,juvenile,kids,child){
 var query = 'date_up='+date_up+'&'+'startpoint='+startpoint+'&'+'endpoint='+endpoint+'&'+'adult='+adult+'&'+'juvenile='+juvenile+'&'+'kids='+kids+'&'+'child='+child;
var data = new XMLHttpRequest();
data.open("POST","results.php");

data.onreadystatechange = function(){
  if(data.readyState === 4 && data.status === 200){
    document.getElementById('res').innerHTML = data.responseText;

  }
}
data.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
data.send(query);
 }
   </script>
</head>
<body>
 <?php availableTaxis($results); 

  routes();
 ?>
 <div id="res">
 </div>
 </body>
 </html>

answer.php file answer.php文件

<?php 
 $from = $_POST['from'];
 $to = $_POST['to'];
 $db = new PDO('mysql:host=localhost; dbname=data','root','');
 $results = $db->prepare('SELECT * FROM `taxi` WHERE `startpoint` = ? AND `endpoint` = ?');
 $results->execute(array($from,$to));
 $rows = $results->fetchAll(PDO::FETCH_ASSOC);
 $avalaible = "Avalaible on: <br>";
 for ($i=0; $i < count($rows) ; $i++) { 
    $avalaible = $avalaible. $rows[$i]['date_up'].'<br>';
 }
 if(count($rows) > 0){
   echo "$avalaible ";
 }else{
echo "Taxi from <span style='font-style:italic;text-decoration:underline'>$from</span> to <span style='font-style:italic;text-decoration:underline'>$to</span> is not avalaible";
}
?>

results.php results.php

<?php 
        $db = new PDO('mysql:host=localhost; dbname=data','root','');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $date = $_POST['date_up'];
        $startPlace = $_POST['startpoint'];
        $endPlace = $_POST['endpoint'];
        $adult = $_POST['adult'];
        $juvenile = $_POST['juvenile'];
        $kids = $_POST['kids'];
        $child = $_POST['child'];

        $totalPassenger = $adult + $juvenile + $kids + $child;
        $taxis = ceil($totalPassenger/4);
        $results = $db->prepare('SELECT * FROM taxi where (startpoint = ? AND endpoint = ?) AND (date_up = ?) AND (standards >= ? OR deluxe >= ?)');
        try {
            $results->execute(array($startPlace,$endPlace,$date,$taxis,$taxis));
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        $rows = $results->fetchAll(PDO::FETCH_ASSOC);
        $seats = ($rows[0]['standards']+$rows[0]['deluxe']) * 4;
        $taxis = $rows[0]['standards']+$rows[0]['deluxe'];
        echo 'Found '.$seats." seats, in $taxis taxis:<br>".($rows[0]['standards']*4).' Standards seats.<br>'.($rows[0]['deluxe']*4).' Deluxe seats.<br>For '.$totalPassenger.' passengers';
?>

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

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