简体   繁体   中英

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. 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'. 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. 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. (I think...) I tried using a UNION between the tables, but I got even more mixed up.

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. Thus no Customers.VIN will ever be filled out if there was no customer associated with any VIN (Cars.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 . 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 . If, however, you may sometimes have customers that have not bought a car, but still want all customers, then use a left join . This will cause all of the car columns to contain NULL if there's not a corresponding record.

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.

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. I decided to go about it through multiple search queries and IF statements. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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