1
<?php
$con=mysqli_connect("localhost","root","","clarks");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$place = $_GET['place'];
$place2 = $_GET['place2'];
$return = $_GET['return'];
$people = $_GET['people'];
$pickup = $_GET['pickup'];
$dropoff = $_GET['dropoff'];

$result = mysqli_query($con,"SELECT * FROM pricelist WHERE place1='$place' AND place2='$place2' AND people='$people'");

while($row = mysqli_fetch_array($result))
{
if (!empty($row['Price']))
{
echo "Not Applicable";
}
else
{
  echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
  echo "<div id='prices'>£";
  echo $row['Price'] * $return + $pickup + $dropoff;
  echo "</div>";
  echo "<div id='back'>";
  echo "<a href='index.html'>Go Back</a>";
  echo "</div>";
  echo "<br>";
  }
}
mysqli_close($con);
?>

这每次都从 if 参数返回“不适用”。

我尝试以不同的顺序放置行并同时使用价格和结果变量,它仍然只返回不适用但没有 if else 参数,其余部分按预期工作并提出价格,所以我知道价格变量不应为空,结果变量也不应为空。

由于它始终返回不适用,我认为它不再正确地从数据库中检索信息,但我无法弄清楚为什么如果没有 if else 它工作得很好。

任何帮助都会很棒,谢谢。

4

4 回答 4

3

你的逻辑是错误的:

if (!empty($row['Price']))
    ^ here
{
echo "Not Applicable";
}

应该:

if (empty($row['Price']))
{
echo "Not Applicable";
}

您还有一个严重的 sql 注入问题,您应该使用准备好的语句或至少使用mysqli's 转义函数。

于 2013-09-22T15:06:46.660 回答
0
if (empty($row['Price']))
{
echo "Not Applicable";
}
else
{
  echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet'  
.....
  }
 }

我认为if条件中的否定导致问题去掉感叹号!

于 2013-09-22T15:07:36.680 回答
0

这可能是因为您使用的是 mysqli_fetch_array 而不是 mysqli_fetch_assoc。http://www.php.net/manual/en/mysqli-result.fetch-assoc.php

您还应该开始在参数上使用 real_escape_ 字符串,或者使用参数化的预处理语句以正确的方式执行此操作。http://www.php.net/manual/en/mysqli.prepare.php

于 2013-09-22T15:32:10.940 回答
0
<?php
$con=mysqli_connect("localhost","root","","clarks");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$place = $_GET['place'];
$place2 = $_GET['place2'];
$return = $_GET['return'];
$people = $_GET['people'];
$pickup = $_GET['pickup'];
$dropoff = $_GET['dropoff'];

$result = mysqli_query($con,"SELECT * FROM pricelist WHERE place1='$place' AND place2='$place2' AND people='$people'");
$row = (mysqli_fetch_array($result));
$a = $row[3]; 

if (empty($a))
{
  echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
  echo "<div id='prices'>";
  echo "N/A";
  echo "</div>";
  echo "<div id='back'>";
  echo "<a href='index.html'>Go Back</a>";
  echo "</div>";
  echo "<br>";
}
else
{
  echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
  echo "<div id='prices'>£";
  echo $a * $return + $pickup + $dropoff;
  echo "</div>";
  echo "<div id='back'>";
  echo "<a href='index.html'>Go Back</a>";
  echo "</div>";
  echo "<br>";
  }
mysqli_close($con);
?>

好吧,我找到了这个解决方法,而不是使用while我刚刚摆脱它并将数据库中的价格分配给它自己的变量,这意味着与if else语句没有冲突。像魅力一样工作......现在看看准备好的陈述:S

于 2013-09-23T10:27:19.763 回答