简体   繁体   English

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

[英]Getting multiple results from WHERE clause in PHP

Please excuse my horrid coding and design aspect of this. 请原谅我可怕的编码和设计方面。 I am not too concerned about the look of it as I am of how well it works. 我并不太关心它的外观,因为我的工作效果如何。

I have 2 tables (Cars, Customers) in which both have the VIN columns. 我有2个表(汽车,客户),其中两个都有VIN列。 When I add a new car I put in a VIN, and when a customer purchases a vehicle, I select the VIN from a drop-down list that is populated in all cars with the field VSold is set to 'N'. 当我添加一辆新车时,我放入了VIN,当客户购买车辆时,我从所有车辆中填充的下拉列表中选择VIN,并将字段VSold设置为“N”。 That works great, the issue I have is that when I run the code below, it gives me multiple customer names. 这很好用,我的问题是,当我运行下面的代码时,它给了我多个客户名称。 When I run a search query in that database for that table and exact VIN, there is only 1 customer that has that matching VIN (I made it UNIQUE), yet in my ugly code, it gives me a bunch of results, all the same car, just different customers. 当我在该数据库中为该表和精确的VIN运行搜索查询时,只有一个客户具有匹配的VIN(我使其成为独一无二的),但在我丑陋的代码中,它给了我一堆结果,都是一样的汽车,只是不同的客户。 What am I doing wrong here? 我在这做错了什么? How can I clean this thing up? 我怎样才能清理这个东西?

<?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);
?> 

When I put a VIN of a vehichle not sold (VSold='N'), I don't have any issue. 当我把未售出的车辆的VIN(VSold ='N')时,我没有任何问题。 (I think...) I tried using a UNION between the tables, but I got even more mixed up. (我想......)我尝试在桌子之间使用UNION,但我更加混乱了。

Thanks ahead of time for the help! 提前谢谢你的帮助!

UPDATE: 更新:

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

Is what I have on the page that I add customers to. 是我在页面上添加客户的内容。 It inputs all the customers information, (CustFirst, CustLast, etc.), to the table Customers. 它将所有客户信息(CustFirst,CustLast等)输入到客户表中。 Thus no Customers.VIN will ever be filled out if there was no customer associated with any VIN (Cars.VIN). 因此,如果没有客户与任何VIN(Cars.VIN)相关联,则不会填写Customers.VIN。

If I understood correctly your problem (you have multiple queries returning and not only one), change this: 如果我正确理解了您的问题(您有多个查询返回而不仅仅是一个),请更改此:

WHERE Cars.VIN='$VIN'

to this 对此

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

From what it looks like you're trying to do you should be using an inner to get info from both tables. 从你想要做的事情来看,你应该使用内部来从两个表中获取信息。

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'";
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'

Produces a cartesian product between cars and customers . carscustomers之间生产笛卡尔积 That is, it returns all combinations of rows between the two tables. 也就是说,它返回两个表之间的所有行组合。 To avoid this, you need a join. 为避免这种情况,您需要加入。 If you will always have at least one sold car per customer (ie, it's a sales database), then use an inner join . 如果每个客户总是至少有一辆售出的汽车(即,它是销售数据库),那么使用inner join If, however, you may sometimes have customers that have not bought a car, but still want all customers, then use a left join . 但是,如果您的客户有时没有购买汽车,但仍然想要所有客户,那么请使用left join This will cause all of the car columns to contain NULL if there's not a corresponding record. 如果没有相应的记录,这将导致所有car列包含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'

Additionally, look into using the mysqli library (or similar) and learn how to parameterize your queries so you can avoid dealing with SQL Injection later. 另外,请查看使用mysqli库(或类似文件)并了解如何参数化查询,以便以后可以避免处理SQL注入

While all your answers did solve the multiple search results, it was giving me no results if the vehicle wasn't sold, hence no VIN associated with any customer. 虽然您的所有答案都解决了多个搜索结果,但如果车辆未售出则没有给我任何结果,因此没有与任何客户相关的VIN。 I decided to go about it through multiple search queries and IF statements. 我决定通过多个搜索查询和IF语句来解决这个问题。 PROBABLY not the most efficient or cleanliness but it works great. 可能不是最有效或最清洁,但效果很好。 Any code improvements are always welcome. 任何代码改进都是受欢迎的。 Thank for all your guys help! 感谢你们所有人的帮助!

$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>";
                }
}
}

Like I said, probably not the most efficient way to go about it, but it works great. 就像我说的那样,可能不是最有效的方法,但效果很好。 Thanks everyone! 感谢大家!

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

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