gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:15:58 24 4
gpt4 key购买 nike

请原谅我糟糕的编码和设计方面。我不太关心它的外观,因为我关心它的工作情况。

我有 2 个表(Cars、Customers),其中都有 VIN 列。当我添加一辆新车时,我输入一个 VIN,当客户购买车辆时,我从一个下拉列表中选择 VIN,该列表填充在所有汽车中,字段 VSold 设置为“N”。效果很好,我遇到的问题是,当我运行下面的代码时,它给了我多个客户名称。当我在该数据库中针对该表和确切的 VIN 运行搜索查询时,只有 1 个客户具有匹配的 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='N') 时,我没有任何问题。 (我认为......)我尝试在表之间使用 UNION,但我更加混淆了。

提前感谢您的帮助!

更新:

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

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

最佳答案

如果我理解正确你的问题(你有多个查询返回,而不是只有一个),改变这个:

WHERE Cars.VIN='$VIN'

对此

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

关于php - 从 PHP 中的 WHERE 子句获取多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38286207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com