我有一個按ASC順序排列名稱的表,但是當我單擊按鈕時它不起作用。 我試着用2個按鈕做同樣的事情,並檢查了一些可用的代碼,但它根本不起作用。任何幫助?使用PHP排序MYSQL表格
PHP代碼:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myfeeds";
$conn = mysql_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed");
}
$db = mysql_select_db("myfeeds", $conn);
if (!$db) {
die("Can't select database");
}
if (isset($_POST['asc'])) {
$result = mysql_query("SELECT * FROM websites ORDER BY name ASC");
} else {
$result = mysql_query("SELECT * FROM websites ORDER BY name DESC");
}
if (!$result) {
die("Failed to show queries from table");
}
$num = mysql_numrows($result);
mysql_close();
?>
這裏的按鈕:
SORT BY:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<button type="submit" id="asc" name="asc">ASC</button>
</form>
表:
<table cellpadding="0">
<tr>
<th>ID</th>
<th>Name</th>
<th>URL</th>
<th>Description</th>
<th>Logo</th>
</tr>
<?php
$i = 0;
while ($i < $num) {
$f5 = mysql_result($result, $i, "id");
$f1 = mysql_result($result, $i, "name");
$f2 = mysql_result($result, $i, "url");
$f3 = mysql_result($result, $i, "description");
$f4 = mysql_result($result, $i, "image");
?>
<tr>
<td><?php echo $f5; ?></td>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
<td><?php echo "<img src='$f4'>"; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
旁註:'mysql_numrows'無效。這應該讀作'mysql_num_rows'。另外,你在哪裏迴應你的結果?這不是你發佈的內容。你需要回應這些。 – 2014-11-23 04:27:16
在打開'<?php'標記後立即在文件頂部添加錯誤報告 'error_reporting(E_ALL); ini_set('display_errors',1);'看看它是否產生任何東西。還有'或者(mysql_error())'去''mysql_query()'。做'死(「消息X」)'沒有幫助。 – 2014-11-23 04:29:06
嘗試'' – cepradeep 2014-11-23 04:32:15