我有這樣的代碼:如何從數據庫中獲取數據到PHP選項中的另一個選項MySQL?
<html>
<body>
<form method="post" action="">
Category: <select name="category">
<option>Choose Category</option>
<?php
include("connect.php");
$select="SELECT * FROM category";
$result=mysqli_query($link,$select) or die (mysqli_error($link));
while($row=mysqli_fetch_array($result))
{
echo "<option value='$row[category]'>".$row['category'];
}
?>
</select>
<br>
Subcategory:<select name="category">
<?php
include("connect.php");
[email protected]$_POST['category'];
if($category=="Friuts")
{
$select="SELECT * FROM subcategory WHERE $category='$category'";
$result=mysqli_query($link,$select) or die (mysqli_error($link));
while($row=mysqli_fetch_array($result))
{
echo "<option value='echo $row[subcategory]'>".$row['subcategory'];
}
}
?>
</select>
<br>
<input type="submit" value="Open" name="submit">
</form>
</body>
</html>
我有兩個表,表中的一個名稱類別和表中的兩個名稱的子類別,我要兩張,當我從表類別選擇一個項目選項獲得選擇選項,從表子類數據出現如下圖所示:
This image is table subcategory
您的代碼容易受到[** SQL注入**](https://en.wikipedia.org/wiki/SQL_injection)攻擊。您應該通過[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)或[** PDO **](https ://secure.php.net/manual/en/pdo.prepared-statements.php)驅動程序。 [**這篇文章**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)有一些很好的例子。 –
@AlexHowansky這是我的代碼,我該如何修復? – Talib
也許閱讀我發佈的鏈接? –