从PHP中的WHERE子句获取多个结果

时间:2016-07-09 20:45:09

标签: php mysql

请原谅我可怕的编码和设计方面。我并不太关心它的外观,因为我的工作有多好。

我有2个表(汽车,客户),其中两个都有VIN列。当我添加一辆新车时,我放入了VIN,当客户购买车辆时,我从所有车辆中填充的下拉列表中选择VIN,其中VSold的字段设置为' N&#39 ;。这很好用,我的问题是,当我运行下面的代码时,它给了我多个客户名称。当我在该数据库中为该表和精确的VIN运行搜索查询时,只有一个客户具有匹配的VIN(我使其成为独一无二的),但在我丑陋的代码中,它给了我一堆结果,都是一样的汽车,只是不同的客户。我在这做错了什么?我该如何清理这件事?

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn ) {
    die('Could not connect: ' . mysql_error());
}

$VIN=$_POST['formVIN'];

$sql = 
  "SELECT 
    Cars.VIN, Cars.VYear, Cars.VMake, Cars.VModel, 
    Cars.VOdometer, Cars.VPurchasedPrice, Cars.VPurchasedDate, 
    Cars.VSold, Cars.VSoldPrice, Cars.VSoldDate, Cars.VSalesPerson, 
    Customers.CustFirst, Customers.CustLast 
  FROM 
    Cars, Customers 
  WHERE Cars.VIN='$VIN'";

mysql_select_db('dbCar2016');
$retval = mysql_query( $sql, $conn );

if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
  while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
    echo "Information on record for the VIN provided:<br><br>";
    echo "VIN:" . $row["VIN"] . "<br>";
    echo "Year:" . $row["VYear"] . "<br>";
    echo "Make:" . $row["VMake"] . "<br>";
    echo "Model:" . $row["VModel"] . "<br>";
    echo "Odometer:" . $row["VOdometer"] . "<br>";
    echo "Purchased Price:$" . $row["VPurchasedPrice"] . "<br>";
    echo "Purchased Date:" . $row["VPurchasedDate"] . "<br><br>";
      if ($row["VSold"]=='Y') {
        echo "This Vehicle sold.<br>";
        echo "Price Sold:" . $row["VSoldPrice"] . "<br>";
        echo "Date Sold:" . $row["VSoldDate"] . "<br>";
        echo "Sales Person:" . $row["VSalesPerson"] . "<br><br>";

        echo "It was sold to<br>";
        echo "Customer Name:" . $row["CustFirst"] . " " . $row["CustLast"] . "<br>";
    } else {
        echo "This Vehicle has not sold yet.<br>";
    }
  echo "<p>VIN Successfully Searched</p>";
}
echo "<a href=vinlookup.php>Search Another VIN</a>";
mysql_close($conn);
?> 

当我放置未售出车辆的VIN时(VSold =&#39; N&#39;),我没有任何问题。 (我想......)我尝试在桌子之间使用UNION,但我更加混乱了。

提前感谢您的帮助!

更新:

UPDATE 
  Cars SET VSold='Y', 
  VSoldPrice='$VSoldPrice', 
  VSoldDate='$CustDownDate', 
  VSalesPerson='$VSalesPerson' 
WHERE 
  VIN='$VIN'

我在页面上添加了客户的内容。它将所有客户信息(CustFirst,CustLast等)输入到客户表中。因此,如果没有客户与任何VIN(Cars.VIN)相关联,则不会填写Customers.VIN。

4 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题(您有多个查询返回而不只是一个),请更改此:

WHERE Cars.VIN='$VIN'

到这个

WHERE Cars.VIN='$VIN' AND Cars.VIN = Customers.VIN

答案 1 :(得分:0)

从你想要做的事情来看,你应该使用内部来从两个表中获取信息。

SELECT Cars.VIN, Cars.VYear, Cars.VMake, Cars.VModel, Cars.VOdometer, Cars.VPurchasedPrice, Cars.VPurchasedDate, Cars.VSold, Cars.VSoldPrice, Cars.VSoldDate, Cars.VSalesPerson, Customers.CustFirst, Customers.CustLast 
FROM Cars 
INNER JOIN Customers 
ON Customers.vin = Cars.vin  
WHERE Cars.VIN='$VIN'";

答案 2 :(得分:0)

SELECT 
  Cars.VIN, Cars.VYear, Cars.VMake, Cars.VModel, Cars.VOdometer,
  Cars.VPurchasedPrice, Cars.VPurchasedDate, Cars.VSold, Cars.VSoldPrice,
  Cars.VSoldDate, Cars.VSalesPerson, Customers.CustFirst, Customers.CustLast 
FROM 
  Cars, Customers 
WHERE 
  Cars.VIN='$VIN'

carscustomers之间生成cartesian product。也就是说,它返回两个表之间的所有行组合。为避免这种情况,您需要加入。如果每个客户总是至少有一辆售出的汽车(即它是一个销售数据库),那么使用inner join。但是,如果您的客户有时没有买车,但仍想要所有客户,那么请使用left join。如果没有相应的记录,这将导致所有汽车列都包含NULL

SELECT 
  Cars.VIN, Cars.VYear, Cars.VMake, Cars.VModel, Cars.VOdometer,
  Cars.VPurchasedPrice, Cars.VPurchasedDate, Cars.VSold, Cars.VSoldPrice,
  Cars.VSoldDate, Cars.VSalesPerson, Customers.CustFirst, Customers.CustLast 
FROM 
  Cars
  LEFT JOIN Customers on Cars.VIN = Customers.VIN
WHERE 
  Cars.VIN='$VIN'

此外,请查看使用mysqli库(或类似文件),并了解如何参数化查询,以便以后可以避免处理SQL Injection

答案 3 :(得分:0)

虽然您的所有答案确实解决了多个搜索结果,但如果车辆未售出则没有给我任何结果,因此没有与任何客户相关的VIN。我决定通过多个搜索查询和IF语句来解决这个问题。可能不是最有效或最清洁,但效果很好。任何代码改进都是受欢迎的。感谢你们所有人的帮助!

$formVIN = $_POST[formVIN]

sql1= SELECT * FROM Cars WHERE VIN=$formVIN
sql2= SELECT * FROM Customers WHERE VIN=$formVIN

$retval1=mysql_query( $sql1, $conn )
$retval2=mysql_query( $sql2, $conn )

if(! $retval1 ) {
  die('No VIN information.' . mysql_error());
} else {

    while($row1 = mysql_fetch_array($retval1, MYSQL_ASSOC)) {
    echo "<p><b>Information on record for the VIN provided:</b></p>";
    echo "VIN:" . $row1["VIN"] . "<br>";
    echo "Year:" . $row1["VYear"] . "<br>";
    echo "Make:" . $row1["VMake"] . "<br>";
    echo "Model:" . $row1["VModel"] . "<br>";
        if ($row1["VSold"]=='Y') 
        {
            while($row2 = mysql_fetch_array($retval2, MYSQL_ASSOC)) {
            echo "<p><b>This Vehicle sold.</b></p>";
            echo "Price Sold:" . $row1["VSoldPrice"] . "<br>";
            echo "Date Sold:" . $row1["VSoldDate"] . "<br>";
            echo "Sales Person:" . $row1["VSalesPerson"] . "<br>";

            echo "<p><b>It was sold to:</b></p>";
            echo "Customer ID:" . $row2["CustID"] . "<br>";
            echo "Customer Name:" . $row2["CustFirst"] . " " . $rowl["CustLast"] . "<br>";
            }
        } else 
                {
                    echo "<p><b>This Vehicle has not sold yet.</b></p><br>";
                }
}
}

就像我说的那样,可能不是最有效的方法,但效果很好。谢谢大家!