我一直在試圖弄清楚這個問題,但它不起作用。我可以從數據庫中檢索所有數據,但是我想從用戶在表單中輸入的html表單中檢索jobname,並且用戶也在表單中輸入位置,並將這些文本與jobname和location字段進行比較數據庫上的一張表。我知道這聽起來有點混亂,但這裏是我的代碼,什麼也不做:從html表格中提取數據並將其與數據庫中的字段進行比較php
<html>
<body>
<?php
ob_start();
$host=""; // Host name
$username= ""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$job=$_POST['jobName'];
$mylocation=$_POST['location'];
// To protect MySQL injection (more detail about MySQL injection)
$job = stripslashes($job);
$mylocation = stripslashes($mylocation);
$job = mysql_real_escape_string($job);
$mylocation = mysql_real_escape_string($mylocation);
$sql="SELECT * FROM ".$tbl_name." WHERE jobName='".$job."' and location='".$mylocation."';";
$result=mysql_query($sql);
$num=mysql_numrows($result);
mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td><font face="Arial, Helvetica, sans-serif">jobName</font></td>
<td><font face="Arial, Helvetica, sans-serif">jobType</font></td>
<td><font face="Arial, Helvetica, sans-serif">Location</font></td>
<td><font face="Arial, Helvetica, sans-serif">Description</font></td>
<td><font face="Arial, Helvetica, sans-serif">Contact</font></td>
</tr>
<?php
$i=0;
while ($i < $num) {
$job=mysql_result($result,$i,"jobName");
$type=mysql_result($result,$i,"jobType");
$myLocation=mysql_result($result,$i,"location");
$desc=mysql_result($result,$i,"description");
$cont=mysql_result($result,$i,"contact");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $job; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $type; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $myLocation; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $desc; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $cont; ?></font></td>
</tr>
<?php
$i++;
}
ob_end_flush();
?>
</body>
</html>
我只需要一個很好的教程來幫助我,雖然沒有我研究到現在幫了我,也許如果我能得到一個例子代碼表示類似的問題,或者幫助解釋我在這段代碼中出錯的地方。我很感激。
你面臨什麼錯誤..? –
順便說一句,你有一個語法錯誤:mysql_numrows它應該是mysql_num_rows – Sam
嘗試只是'while($ num){'不需要'$ i' –