我无法从数据库表中检索数据。请帮助我) 错误太多了。无法从数据库中获取任何数据
 <input type="checkbox" name="model" value="Jetta" />Jetta
<input type="checkbox" name="model" value="Laguna" />Laguna
<input type="checkbox" name="model" value="Focus" />Focus
<input type="checkbox" name="model" value="Tico" />Tico
<input type="checkbox" name="model" value="Avensis" />Avensis
<input type="submit" name="submit" value="Register" /> </div>
<input type="hidden" name="submitted" value="TRUE"/>
</fieldset>
file=(test2.php)
<?
mysql_connect("localhost","root","");
mysql_select_db("carforsale");
$sql =mysql_query("SELECT * FROM carforsale");
$row=mysql_fetch_assoc($sql);
$manufacturerName = 'manufacturerName';
$modelName = 'modelName';
$acquisitionPrice = 'acquisitionPrice';
$model = $_POST['model'];
if ($model=="Jetta") {
include('connect.php');
$result = mysql_query("manufacturerName,modelName,acquisitionPrice FROM
carforsale WHERE modelName='Jetta'");
while($row = mysql_fetch_array($result))
{
echo '<td>'. $row['manufacturerName'].' </td>';
echo '<td>'.$row['modelName'].'</td>';
echo '<td>'.$row['acquisitionPrice'].'</td>';
echo '</tr>';
}
elseif ($model=="Laguna")
$result = mysql_query("manufacturerName,modelName,acquisitionPrice FROM
carforsale WHERE modelName='Laguna'");
while($row = mysql_fetch_array($result)) {
echo '<td style="border-left: 1px solid #eacae7;">'.
$row['manufacturerName'].'</td>';
echo '<td>'.$row['modelName'].'</td>';
echo '<td>'.$row['acquisitionPrice'].'</td>';
echo '</tr>';
}
}
&GT;
*我无法从数据库表中检索数据。请帮助我 有太多的错误;&#39;(
答案 0 :(得分:1)
你有条件
if ($model=="Jetta") {...
但$ model未被分配到任何地方
将其更改为
$model = $_POST['model'];
if ($model=="Jetta") {
由于没有分配,条件是假的,你在底部的else中没有任何东西可以做,所以它基本上什么都不做
答案 1 :(得分:1)
您的代码将被分散,尝试在更逻辑的块中重构您的代码。
此外,mysql_*
功能还有不再支持,它们为officially deprecated,不再维护,并且将来会removed 。您应该使用PDO或MySQLi更新代码,以确保将来的项目功能。
您希望实现的更精细版本
<?php
$host = '127.0.0.1';
$user = 'root';
$pass = '';
$database = 'blub';
try {
$pdo = new PDO('mysql:host=' . $host . ';dbname=' . $database, $user, $pass);
}catch(PDOException $e) {
echo 'Could not connect to database: ' . $e->getMessage();
exit;
}
<?php
include('connect.php'); //only connect once
$sql = 'SELECT * FROM carforsale'; //default query
$parms = array();
$parameters = '';
if (isset($_POST['model'])) {
$sql .= ' WHERE modelName IN ({parameters})'; //adjust query when checkbox was selected
foreach($_POST['model'] as $checkbox) {
$parms[] = $checkbox;
$parameters. = '?,';//forsee a placeholder for this checkbox
}
$parameters = rtrim($parameters, ',');//remove last comma from ?,?,?,
$sql = str_replace('{parameters}', $parameters);//inject parameters when needed
try {
$stmt = $pdo->prepare($sql); //use PDO with prepared statements instead of mysql_query (see comment above)
$stmt->execute($parms); //inject the model name in the parameter ? in the sql if needed
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}catch(PDOException $e) {
echo 'Query failed: ' . $e->getMessage(); //always check SQL errors
exit;
}
$table = '<table border="1">';
$table .= '<tr><th>Manufacturer</th><th>Model</th><th>Price</th></tr>';
foreach($rows as $row) {
$table .= '<tr>';
$table .= '<td>'. $row['manufacturerName'].' </td><td>'.$row['modelName'].'</td><td>'.$row['acquisitionPrice'].'</td>';
$table .= '</tr>';
}
$table .= '</table>';
?>
<!DOCTYPE html>
<html>
<head>
<title>Cars</title>
</head>
<body>
<form action="#" method="POST">
<fieldset>
<input type="checkbox" name="model[]" value="Jetta" />Jetta
<input type="checkbox" name="model[]" value="Laguna" />Laguna
<input type="checkbox" name="model[]" value="Focus" />Focus
<input type="checkbox" name="model[]" value="Tico" />Tico
<input type="checkbox" name="model[]" value="Avensis" />Avensis
<input type="submit" name="submit" value="Register" /> </div>
<input type="hidden" name="submitted" value="TRUE"/>
</fieldset>
</form>
<?= $table ?>
</body>
</html>