我正試圖搜索數據庫中的一些數據並將其顯示出來。這是我必須輸入搜索條件的html表單代碼。從SQL數據庫中搜索並顯示數據
<h2> Search </h2>
<form action = "search.php" method = "post" >
Search for: <input type = "text" name ="find" /> in
<select NAME = "field">
<Option VALUE = "Animal Type"> Animal Type</option>
<Option VALUE = "latitude"> Latitude</option>
<Option VALUE = "longitude"> longitude</option>
<Option VALUE = "dateseen"> Date Required</option>
<Option VALUE = "timeseen"> Time</option>
</select>
<inpput type= "hidden" name = "searching" value ="yes"/>
<inpput type= "submit" name = "search" value ="Search"/>
</form>
這是我使用的php代碼。但我一直剛開了錯誤的路線18/22和
mysql_fetch_array說未定義的變量()預計參數1是資源
錯誤。有任何想法嗎?
<?php
if ($searching=="yes")
{echo "<h2> Results</h2><p>";
}
if ($find=="")
{echo "<p> Please enter a search iten";
exit;
}
$link=mysqli_connect("localhost","root","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$db_selected = mysqli_select_db($link,"Animal_Tracker");
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysqli_error());
}
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim($find);
$sql=mysql_query("Select * FROM Animal_Tracker WHERE upper($field) LIKE '%$find%' ");
while($result = mysql_fetch_array($sql))
{
echo $result ['Animal Type'];
echo " ";
echo $result ['latitude'];
echo "<br> ";
echo $result ['longitude'];
echo " <br>";
echo $result ['dateseen'];
echo " <br> ";
echo $result ['timeseen'];
echo "<br> ";
echo "<br> ";
}
您使用一個變量叫'$ field'在您的SQL查詢,但你不隨地定義它。 – David
你正在做一個mysqli連接並執行mysql_ *函數.. –