答案 0 :(得分:1)
根据@AshishChoudhury的建议,您需要使用$_POST['sortby']
而不是使用$_GET['sortby']
,因为您的表单使用POST而不是GET。
使用这个
if (isset($_POST['sortby']) && $_POST['sortby'] == '1')
{
$sql = "SELECT * FROM ProductManagement ORDER BY FilmName ASC";
}
elseif (isset($_POST['sortby']) && $_POST['sortby'] == '2')
{
$sql = "SELECT * FROM ProductManagement ORDER BY FilmName DESC";
}
else
{
$sql = "SELECT * FROM ProductManagement ORDER BY ID ASC;";
}
$result = $connection->query($sql);
if ($result->num_rows > 0) {
echo "<p><table>
<tr>
<th>ID</th>
<th>Film Name</th>
<th>Producer</th>
<th>Year Published</th>
<th>Stock</th>
<th>Price</th>
<th>Function</th>
</tr>";
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>".$row["ID"]."</td>
<td>".$row["FilmName"]."</td>
<td>".$row["Producer"]."</td>
<td>".$row['YearPublished']."</td>
<td>".$row['Stock']."</td>
<td>".$row['Price']."</td>
<td><a href=\"edit.php?ID=".$row["ID"]."\">Edit</a>
<td><a href=\"delete.php?ID=".$row["ID"]."\">Delete</a>
</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
答案 1 :(得分:0)
我认为您应该将线重新排列为下面的内容
$sort = @$_POST['order'];
if (isset($_POST['sortby']) && $_POST['sortby'] == '1')
{
$sql = "SELECT * FROM ProductManagement ORDER BY FilmName ASC";
}
elseif (isset($_POST['sortby']) && $_POST['sortby'] == '2')
{
$sql = "SELECT * FROM ProductManagement ORDER BY FilmName DESC";
}
else
{
$sql = "SELECT * FROM ProductManagement ORDER BY ID ASC;";
}
$result = $connection->query($sql);
if ($result->num_rows > 0) {
echo "<p><table>
<tr>
<th>ID</th>
<th>Film Name</th>
<th>Producer</th>
<th>Year Published</th>
<th>Stock</th>
<th>Price</th>
<th>Function</th>
</tr>";
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>".$row["ID"]."</td>
<td>".$row["FilmName"]."</td>
<td>".$row["Producer"]."</td>
<td>".$row['YearPublished']."</td>
<td>".$row['Stock']."</td>
<td>".$row['Price']."</td>
<td><a href=\"edit.php?ID=".$row["ID"]."\">Edit</a>
<td><a href=\"delete.php?ID=".$row["ID"]."\">Delete</a>
</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
答案 2 :(得分:0)
我认为这是您的查询
$sql = "SELECT * FROM ProductManagement ORDER BY ID ASC;";
你要重新排序
所以在它之前你应该选择GET或POST,但是例如我将使用post
$order = $_POST['order'];// this can be DESC or ASC
然后将$ order放入查询
$sql = "SELECT * FROM ProductManagement ORDER BY FilmName $order;";
所以在您的查询结果发布后,结果会发生变化
答案 3 :(得分:0)
$ sort = $ _POST [&#39; order&#39;];
$ sortBy =(isset($ _ GET [&#39; sortby&#39;])&amp;&amp; $ _GET [&#39; sortby&#39;]!= 0)?$ _ GET [&#39; sortby&#39;]:0;
开关($ sortBy){
case "1":
$sql = "SELECT * FROM ProductManagement ORDER BY FilmName ASC";
break;
case "2":
$sql = "SELECT * FROM ProductManagement ORDER BY FilmName DESC";
break;
default:
$sql = "SELECT * FROM ProductManagement ORDER BY FilmName ASC";
}
$ result = $ connection-&gt; query($ sql);
if($ result-&gt; num_rows&gt; 0){
echo "<p><table>
<tr>
<th>ID</th>
<th>Film Name</th>
<th>Producer</th>
<th>Year Published</th>
<th>Stock</th>
<th>Price</th>
<th>Function</th>
</tr>";
while($row = $result->fetch_assoc()) {
回声&#34;
&#34; $行[&#34; ID&#34;]&#34;
<td>".$row["FilmName"]."</td>
<td>".$row["Producer"]."</td>
<td>".$row['YearPublished']."</td>
<td>".$row['Stock']."</td>
<td>".$row['Price']."</td>
<td><a href=\"edit.php?ID=".$row["ID"]."\">Edit</a>
<td><a href=\"delete.php?ID=".$row["ID"]."\">Delete</a>
</tr>";
}
echo "</table>";
}
否则 {
echo "0 results";
}
答案 4 :(得分:-1)
现在看到我更新了我的代码检查它。现在它的工作。
$sort = @$_POST['order'];
if (isset($_POST['sortby']) && $_POST['sortby'] == '2')
{
$sql = "SELECT * FROM ProductManagement ORDER BY FilmName DESC";
}
else
{
$sql = "SELECT * FROM ProductManagement ORDER BY FilmName ASC";
}
$result = $connection->query($sql);
if ($result->num_rows > 0) {
echo "<p><table>
<tr>
<th>ID</th>
<th>Film Name</th>
<th>Producer</th>
<th>Year Published</th>
<th>Stock</th>
<th>Price</th>
<th>Function</th>
</tr>";
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>".$row["ID"]."</td>
<td>".$row["FilmName"]."</td>
<td>".$row["Producer"]."</td>
<td>".$row['YearPublished']."</td>
<td>".$row['Stock']."</td>
<td>".$row['Price']."</td>
<td><a href=\"edit.php?ID=".$row["ID"]."\">Edit</a>
<td><a href=\"delete.php?ID=".$row["ID"]."\">Delete</a>
</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
$ _ POST使用,因为在你的表单中方法=“post”