簡體   English   中英

如何使用綁定參數從下拉菜單中返回查詢結果頁

[英]How can I get a results page using bind params to return query from drop down

我正在完成一個在線教程,以使用綁定參數重寫結果頁面。 我以為我理解得很好,但我顯然無法使它正常工作,我沒有。 我已經嘗試了所有我認為合乎邏輯的方法以及一些不是但仍然可以得到的內容,但仍然是空白頁。 這是下拉列表。

 <form action="search3.php" method="post" >

<?php

$mysqli = new mysqli(localhost,user,password,database);

if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

$query = "SELECT DISTINCT Country FROM engravers ORDER BY Country";
$result = $mysqli->query($query);

?>
<select name="dropdown">
<?php

while ($row = $result->fetch_assoc()) {
    echo "<option value=\"{$row['Country']}\">";
    echo $row['Country'];
    echo "</option>";
}
$mysqli->close();
?>
</select>

          <input type="submit"  />   
          </form>

這是結果頁。它幾乎從本教程復制而來,除了本教程中的$ queryparam等於$ _POST ['Country']。 由於不起作用,我將其更改為$ _POST ['dropdown'],這是下拉菜單的名稱。

$hostname = "localhost";
$user = "user";
$password = "password";
$connection = mysqli_connect($hostname, $user, $password,);
if(!$connection){
echo"Could not connect to server";
};
mysqli_select_db($connection,'engraved_stamps');
if(!mysqli_select_db($connection,'engraved_stamps')){
echo"could not connect to database";
};
if(isset($_POST['dropdown']){
}

$stmt=mysqli_prepare($connection,"SELECT Key, Country, Year, Description, Images FROM engravers   WHERE Country=?");
$queryParam=$_POST['dropdown'];
mysqli_stmt_bind_param($stmt,"s",$queryParam);
mysqli_stmt_bind_result($stmt,$Key,$Country,$Year,$Description,$Images);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);


$img_url = "http://www.engravedstamps.net/images/"; 
print '<table border="1" >';
while($row ->mysqli_stmt_fetch($stmt); 
{

print '<tr>';
print '<td>'.$row["Key"].'</td>';
print '<td>'.$row["Country"].'</td>';
print '<td>'.$row["Year"].'</td>';
print '<td>'.$row["Description"].'</td>';

print  '<td>'.'<img src="'.$img_url.$row['Images'].'" />'.'</td>'; 
print '</tr>';
}  
print '</table>';


$results->free();

$mysqli->close();

我想讓這個工作正常,但我也想知道為什么事情會起作用或不起作用。

自編寫以上內容以來,我發現了一種更簡潔的方法來執行此操作,因此對於像我這樣的任何其他新手來說,這就是代碼。 連接數據庫的第一行是相同的。 然后...

if(isset($_POST['dropdown'])){

}else{
 echo "No input";
}

$stmt = $mysqli->prepare("SELECT ID,Country,Year,Description,Images FROM engravers WHERE Country = ? ORDER BY ID");
$stmt->bind_param('s', $_POST['dropdown']);
$stmt->execute();
$stmt->bind_result($ID,$Country,$Year,$Description,$Images);
$img_url = "http://www.engravedstamps.net/images/";

print  "<table border='1' cellpadding='0'>";
while ($stmt->fetch()){
print '<tr><th>ID</th><th>Country</th><th>Year</th><th>Description</th><th>Image</th></tr>';
print '<tr>';
print "<td> " .$ID." </td>";
print "<td> " .$Country. " </td>";
print "<td> " .$Year. " </td>";
print "<td> " .$Description." </td>";

print  '<td>'.'<img src="'.$img_url.$row.$Images.'" />'.'</td>';

  ?>

    <td><a href="more.php?ID=<? echo $ID;?>">More Details</td>

    <?

print '</tr>';
}  
print '</table>';
$stmt->close(); 
?>

最困難的部分是將其打印到表中。 現在還有一個單元格,為每行提供更多詳細信息。 如果單擊它,將帶您到新頁面。 我希望這對某人有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM