2010-04-19 15 views
0

我只是想簡化我之前做的事情,對所有要列出的數據都有多個php文件。 這裏是我的HTML表單:在mysql中遇到問題,如果語句

         <table border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#D3D3D3"> 
<tr> 
<form name="formcheck" method="post" action="list.php" onsubmit="return formCheck(this);"> 
<td> 
<table border="0" cellpadding="3" cellspacing="1" bgcolor=""> 
<tr> 

<td colspan="16" height="25" style="background:#5C915C; color:white; border:white 1px solid; text-align: left"><strong><font size="3">List Students</td> 
</tr> 
<tr> 
<td width="30" height="35"><font size="3">*List:</td> 
<td width="30"><input name="specific" type="text" id="specific" maxlength="25" value=""> 
</td> 

<td><font size="3">*By:</td> 
<td> 
    <select name="general" id="general"> 
     <font size="3"> 
     <option>Year</option> 
     <option>Address</option> 


    </select></td></td> 
    </tr> 
    <tr> 
    <td width="10"><input align="right" type="submit" name="Submit" value="Submit" > </td> 
</tr> 
</form> 
</table> 

而這裏的表單操作:

<?php 
$con = mysql_connect("localhost","root","nitoryolai123$%^"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("school", $con); 

$gyear= $_POST['general']; 



if ("YEAR"==$_POST['general']) { 
$result = mysql_query("SELECT * FROM student WHERE YEAR='{$_POST["specific"]}'"); 


echo "<table border='1'> 
<tr> 
<th>IDNO</th> 
<th>YEAR</th> 
<th>LASTNAME</th> 
<th>FIRSTNAME</th> 

</tr>"; 

while($row = mysql_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['IDNO'] . "</td>"; 
    echo "<td>" . $row['YEAR'] . "</td>"; 
    echo "<td>" . $row['LASTNAME'] . "</td>"; 
    echo "<td>" . $row['FIRSTNAME'] . "</td>"; 


    echo "</tr>"; 
    } 
echo "</table>"; 
} 

mysql_close($con); 
?> 

請幫幫忙,我怎麼等號年(mysql數據庫列)和選項框(一般)。

if ("YEAR"==$_POST['general']) 

如果我錯了,請糾正我。

+0

你能指望什麼$ _ POST [ '一般']是?舉一些適當的例子。如果你正在尋找日期,然後執行谷歌搜索「日期函數PHP」。 – pinaki 2010-04-19 09:16:32

+3

希望您的mysql_connect詳細信息不是真實的 – Unreason 2010-04-19 09:17:34

+1

您應該*永遠不要*使用根MySQL帳戶在您的腳本中查詢,您應該爲此創建一個單獨的用戶帳戶並僅使用root帳戶進行數據庫維護。 – wimvds 2010-04-19 11:51:42

回答

1
<option>Year</option> 

你只需要改變,要

<option value="YEAR">Year</option> 

肯定也有這個頁面的讀取:http://en.wikipedia.org/wiki/SQL_injection,否則你的代碼是下流的攻擊敞開的。

[編輯:也留意在你原來的問題的意見,在網站上發佈您的真正的根用戶名/密碼是不是一個很好的主意]

-1
if ("YEAR"==$_POST['general']) { 
    $result = mysql_query("SELECT * FROM student WHERE date_format(YEAR, '%Y')='{$_POST["specific"]}'"); 
+0

您可以使用'YEAR(\'YEAR \')'而不是'DATE_FORMAT()'。可讀性略高,但完全等同。 – 2010-04-19 14:09:52