如果之前已经涵盖过,我道歉。我查看了所有的建议,似乎没有什么符合我的需要。
无论如何,我正在尝试开发一个汽车零件库,其中以下信息存储在名为" auto_parts"的表格中:
我希望客户能够提交一份表格,询问他们以下问题:
城市,州,部分,新的或二手的,年份和品牌/型号,
将查询我的数据库并输出符合以前条件的表格列上的所有信息:
查询输出:
以下是我的表单的缩写版本:
<form action="parts_query.php" method="get">
<h2 align="center">Find Auto Part</h2>
<strong>City:</strong>
<br />
<input type="text" name="City" value="City" required>
<br />
<strong>State:</strong>
<br />
<select name="State" size="1" required><option selected="selected">Choose One:
<option>AL
<option>AK
<option>AZ
<option>AR
<option>CA
<option>...
</select>
<br />
<strong>Select Part:</strong>
<br />
<select name="Part" size="1" required><option selected="selected">Choose One:
<option>A Pillar
<option>A/C Compressor
<option>A/C Compressor Clutch Only
<option>A/C Condenser
<option>A/C Condenser Fan
<option>A/C Control Computer
<option>A/C Evaporator
<option>A/C Evaporator Housing only
<option>A/C Heater Control (see also Radio or TV Screen)
<option>A/C Hose
<option>Accelerator Parts
<option>Adaptive Cruise Projector
<option>...
</select>
<br />
<strong>New or Used?</strong>
<br />
<select name="New_Used" size="1"><option selected="selected">Choose One:
<option>New
<option>Used
</select>
<br />
<strong>Vehicle Year:</strong>
<br />
<select name="Year" required><option selected="selected">Choose One:
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
</select>
<br />
<strong>Vehicle Make / Model:</strong><br />
<select name="Make_Model" required><option selected="selected">Choose One:
<option>AMC Ambassador
<option>AMC American
<option>AMC AMX
<option>...
</select>
<br />
<br />
<input class="btn" type="submit" value="Submit">
</form>
我尝试过多种查询,在我的数据库表中搜索通过我的表单提交的上述信息,然后将所有相关信息(如上所述)输出到HTML表格,但似乎没有任何效果。我仍然很擅长使用数据库,如果我的问题看起来多余或过于宽泛,我再次道歉。
我不记得parts_query.php原来是怎么看的,因为我已经改变了很多,但我现在看起来像这样:
<?php
//Connecting to sql db.
$connect = mysqli_connect("localhost","root","","autoparts_bank");
$query = "SELECT * FROM auto_parts WHERE City = [] AND Part = [] AND Year = [] AND Make_Model = []"
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr>
<td>" . $row['Distributor'] . "</td>
<td>" . $row['Address'] . "</td>
<td>" . $row['City'] . "</td>
<td>" . $row['State'] . "</td>
<td>" . $row['Zip'] . "</td>
<td>" . $row['Phone'] . "</td>
<td>" . $row['Part'] . "</td>
<td>" . $row['New_Used'] . "</td>
<td>" . $row['Price'] . "</td>
</tr>"; //$row['index'] the index here is a field name
}
echo "</table>";
mysql_close(); //Make sure to close out the database connection
?>
<?php
//Connecting to sql db.
$connect = mysqli_connect("localhost","root","","autoparts_bank");
//Sending form data to sql db.
mysqli_query($connect,"INSERT INTO `auto_parts` (`Distributor`, `Address`, `City`, `State`, `Zip`, `Phone_No`, `SKU`, `Part`, `New_Used`, `Year`, `Make_Model`, `Price`)
VALUES ('$_POST[Distributor]', '$_POST[Address]', '$_POST[City]', '$_POST[State]', '$_POST[Zip]', '$_POST[Phone_No]', '$_POST[SKU]', '$_POST[Part]', '$_POST[New_Used]', '$_POST[Year]', '$_POST[Make_Model]', '$_POST[Price]')");
?>
答案 0 :(得分:0)
要将提交的表单数据返回到外部页面,只需检索$POST or $GET form个变量,然后将其作为parameters in SQL query传递:
<?php
// RETRIEVE FORM DATA (consider using $POST array to avoid url show and character limit)
$city = $_GET["City"];
$state = $_GET["State"];
$part = $_GET["Part"];
$new_used = $_GET["New_Used"];
$year = $_GET["Year"];
$make_model = $_GET["Make_Model"];
// DATABASE CONNECTION
// (consider saving credentials below in external .php file and required in this script)
$servername = "localhost";
$username = "root";
$password = "****";
$dbname = "autoparts_bank";
$conn = new mysqli($servername, $username, $password, $dbname);
// QUERY PREPARATION AND EXECUTION
$query = "SELECT * FROM auto_parts
WHERE City = ? AND Part = ? AND Year = ? AND Make_Model = ?";
$stmt = $conn->prepare($query);
if ($stmt) {
$stmt->bind_param("ssss", $city, $part, $year, $make_model);
$stmt->execute();
$stmt->store_result();
$result = $stmt->get_result();
echo "<table>";
// LOOP THROUGH RESULTS
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>" . $row['Distributor'] . "</td>
<td>" . $row['Address'] . "</td>
<td>" . $row['City'] . "</td>
<td>" . $row['State'] . "</td>
<td>" . $row['Zip'] . "</td>
<td>" . $row['Phone'] . "</td>
<td>" . $row['Part'] . "</td>
<td>" . $row['New_Used'] . "</td>
<td>" . $row['Price'] . "</td>
</tr>";
}
echo "</table>";
$stmt->free_result();
$stmt->close();
} else {
echo "query failed: ".$conn->error;
}
// CLOSE CONNECTION
$conn->close();
?>