Php 和 mysql 查询问题
Php and mysql query issue
我有一个像这样的简单 table:table。表单提交数据以检查此 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">
这是我检查这个 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;
}
}
}
我们共有 10 辆出租车(标准 5 辆,豪华 5 辆)。出租车是四个人。我想检查乘客总数是否超过 4 人但少于 8 人,如果这是真的,我想检查标准或豪华列是否都有至少两辆出租车(int 2)和 return某物。如果乘客总数超过 8 人但少于 12 人,我需要检查两栏是否至少有 3 辆出租车,这将一直持续到我达到 20 人。因为如果乘客总数超过 16 人但少于 20 人,我们需要 5 辆出租车,这是我们的限制。如果用户在 20 之后提供更多乘客,我们可以 return "not enough taxi for your query"。我怎样才能检查这个模式。
我还需要 return 至少一列结果。例如,用户搜索 7 位乘客,我检查发现在豪华栏中我有 2 辆出租车,但在标准栏中我有 1 辆出租车,所以我需要 return 豪华栏匹配结果给用户(或为了使查询更具挑战性,我们可以 return 一辆标准出租车和一辆豪华出租车,对吗?)。我该如何实现这种情况。请帮我。
此代码使用 ajax
在不刷新页面的情况下获取数据。我用过 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 文件
<?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
<?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';
?>
我有一个像这样的简单 table:table。表单提交数据以检查此 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">
这是我检查这个 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;
}
}
}
我们共有 10 辆出租车(标准 5 辆,豪华 5 辆)。出租车是四个人。我想检查乘客总数是否超过 4 人但少于 8 人,如果这是真的,我想检查标准或豪华列是否都有至少两辆出租车(int 2)和 return某物。如果乘客总数超过 8 人但少于 12 人,我需要检查两栏是否至少有 3 辆出租车,这将一直持续到我达到 20 人。因为如果乘客总数超过 16 人但少于 20 人,我们需要 5 辆出租车,这是我们的限制。如果用户在 20 之后提供更多乘客,我们可以 return "not enough taxi for your query"。我怎样才能检查这个模式。
我还需要 return 至少一列结果。例如,用户搜索 7 位乘客,我检查发现在豪华栏中我有 2 辆出租车,但在标准栏中我有 1 辆出租车,所以我需要 return 豪华栏匹配结果给用户(或为了使查询更具挑战性,我们可以 return 一辆标准出租车和一辆豪华出租车,对吗?)。我该如何实现这种情况。请帮我。
此代码使用 ajax
在不刷新页面的情况下获取数据。我用过 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 文件
<?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
<?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';
?>