使用PHP根据选择显示MDB文件的结果
我希望有人能帮我这个忙 我有一个Joomla安装运行和网站的外观和工程伟大 这里的问题是,该网站是为汽车经销商,这意味着他们需要在地板上显示他们的股票清单 他们使用自定义系统管理库存,该系统将数据保存到MS Access数据库中 我让它工作到可以显示数据库中的表的程度。() 现在,当有人单击模型时,这是一个链接,会将他们带到另一个页面,该页面仅显示与所选模型相关的信息 这就是我不明白的。该链接工作正常,它将我带到我想要的页面,但它没有像预期的那样显示数据 请参阅下面的代码以连接到数据库并显示结果:使用PHP根据选择显示MDB文件的结果,php,ms-access,joomla,odbc,Php,Ms Access,Joomla,Odbc,我希望有人能帮我这个忙 我有一个Joomla安装运行和网站的外观和工程伟大 这里的问题是,该网站是为汽车经销商,这意味着他们需要在地板上显示他们的股票清单 他们使用自定义系统管理库存,该系统将数据保存到MS Access数据库中 我让它工作到可以显示数据库中的表的程度。() 现在,当有人单击模型时,这是一个链接,会将他们带到另一个页面,该页面仅显示与所选模型相关的信息 这就是我不明白的。该链接工作正常,它将我带到我想要的页面,但它没有像预期的那样显示数据 请参阅下面的代码以连接到数据库并显示结果
<?php
$dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/db/savvyautoweb.mdb";
// Throws an error if the database cannot be found
if (!file_exists($dbName)) {
die("Could not find database file.");
}
// Connects to the database
// Assumes there is no username or password
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');
// This is the query
// You have to have each column you select in the format tableName.[ColumnName]
$sql = "SELECT Make, Model, Year, Price, SpecialPrice, Branch, StockNO FROM Vehicle ORDER BY Make";
// Runs the query above in the table
$rs = odbc_exec($conn, $sql);
echo "\t" . "<tr>\n";
echo "\t" . "<th>Make</th><th>Model</th><th>Year</th><th>Price</th><th>Special Price</th><th>Location</th><th>Stock Number</th>" . "\n";
while (odbc_fetch_row($rs))
{
$make = odbc_result($rs, Make);
$model = odbc_result($rs, Model);
$year = odbc_result($rs, Year);
$price = odbc_result($rs, Price);
$specialPrice = odbc_result($rs, SpecialPrice);
$branch = odbc_result($rs, Branch);
$stockNo = odbc_result($rs, StockNO);
echo "\t" . "<tr>\n";
echo "\t\t" . "<td>" . $make . "</td><td><a href=http://www.autodeal.co.za/newsite/selected-vehicles>" . $model . "</a></td><td>" . $year . "</td><td>" . $price . "</td><td>" . $specialPrice . "</td><td>" . $branch . "</td><td>" . $stockNo . "</td>\n";
echo "\t" . "</tr>\n";
}
odbc_free_result($rs);
odbc_close($conn);
// This message is displayed if the query has an error in it
if (!$rs) {
exit("There is an error in the SQL!");
}
?>
请参见下面的代码,以根据上述脚本所做的选择显示表中的特定车辆信息
<?php
$dbName = "F:/Domains/autodeal/autodeal.co.za/wwwroot/newsite/db/savvyautoweb.mdb";
// Throws an error if the database cannot be found
if (!file_exists($dbName)) {
die("Could not find database file.");
}
// Connects to the database
// Assumes there is no username or password
$conn = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", '', '');
// This is the query
// You have to have each column you select in the format tableName.[ColumnName]
$selected_id = intval($_GET['Id']);
$sql = "SELECT Id, Make, Model, Year, Price, SpecialPrice, Branch, StockNO, MainPic FROM Vehicle WHERE Id = Id";
// Runs the query above in the table
$rs = odbc_exec($conn, $sql);
$id = odbc_result($rs, Id);
$make = odbc_result($rs, Make);
$model = odbc_result($rs, Model);
echo $make;
echo $model;
$image_path_main = "<img src=db/vehicleImages/" . $mainPic . "/>";
echo "this is a test";
odbc_free_result($rs);
odbc_close($conn);
// This message is displayed if the query has an error in it
if (!$rs) {
exit("There is an error in the SQL!");
}
?>
编辑,以便根据收到的答案更新上述代码,但不显示单独的记录。我打印了一个测试行,效果很好,这说明查询有问题?问题是,查询可以很好地显示表中的所有记录,但我需要在单击该记录时显示一条记录
此外,上面的$mainPic变量引用了数据库中的图像名称。实际图像未保存在数据库中;它在另一个地方。我假设我需要用图像的实际路径创建一个变量,并使用上面的参考变量来显示图像,但它不起作用
总而言之,我需要一些帮助,根据选择显示数据库中的所有信息
例如:在表中,我选择323i。在新页面上,我需要在新页面上显示数据库中关于323i的所有信息
这是可行的,如果是的话,谁能在这件事上帮助我
非常感谢您。您在查询中没有使用给定的ID参数:
$sql = "SELECT ... FROM Vehicle WHERE Id = Id ORDER BY Make";
您需要从用户处获取$ID,并将其放入查询中,如下所示:
$id = intval($_GET['id']); // assuming it is an integer
$sql = "SELECT ... FROM Vehicle WHERE Id = $id; // no need to order
嗨,谢谢你的回答。我已经根据您的答案更新了第二段代码,但代码不起作用。不,它没有显示任何内容。我已经打印了一个测试行,这很有效,那么这是否意味着查询出了问题?感谢您的帮助。似乎您正在以id(小写)的形式发送变量,但将其用作$_GET[“id”](camelcase)。这可能就是问题所在。我认为这确实会有所不同。在实际数据库中,冒号称为Id(大写字母“I”)。我用它来解决问题,但它不正确。无论我选择哪个项目,它都会显示相同的结果。为什么会这样?请参阅上面编辑的代码(第二段代码)。这可能不会对DB产生影响,但在使用$\u GET数组中的参数时会产生影响。我看到了另一个问题:您在上面的while循环中硬编码了id=27。该id是Joomla文章的位置。但是我改变了它,我也改变了$u GET变量。它仍然显示错误的信息。例如,当我点击BMW时,它应该向我显示特定BMW的信息,但它却显示雷诺的信息。我更新了代码。