在我的学校数据库项目的第一次迭代中,我成功创建了一个页面,该页面将在使用$_GET
标记单击每个表列的标题时对MySQL表进行排序。第一次,我为要访问的数据库表中的每个主条目创建了一个单独的页面。
对于第二次迭代,我选择使用一个页面,该页面将显示与以前相同的信息,但是当使用$_GET
标记链接到该页面时,它将从数据库中进行选择。
我试图弄清楚为什么以下代码将对我显示的表进行正确排序:
$query = "SELECT Review.username, Review.service_date, Review.review_date,
Review.review_rating, Review.review_details, Review.service_id,
Plumber.company_name
FROM Review
INNER JOIN Plumber
ON Review.service_id = Plumber.service_id
WHERE Plumber.service_id='1'";
if ($_GET['sort'] == 'username') {
$query .= " ORDER BY username";
} else if ($_GET['sort'] == 'service_date') {
$query .= " ORDER BY service_date DESC";
} else if ($_GET['sort'] == 'review_date') {
$query .= " ORDER BY review_date DESC";
} else if ($_GET['sort'] == 'review_rating') {
$query .= " ORDER BY review_rating DESC";
} else {
$query .= " ORDER BY review_date";
}
$response = @mysqli_query($db, $query);
$plumber;
if($response){
echo '<table align="left" cellspacing="5" cellpadding="8" width="50%">
<td align="left"><b><a href="Plumber1.php?sort=username">Username</a></b></td>
<td align="left"><b><a href="Plumber1.php?sort=service_date">Service Date</a></b></td>
<td align="left"><b><a href="Plumber1.php?sort=review_date">Review Date</a></b></td>
<td align="left"><b><a href="Plumber1.php?sort=review_rating">Review Rating</a></b></td>
<td align="left"><b>Review Details</b></td>';
while($row = mysqli_fetch_array($response)) {
echo '<tr><td align="left">' .
$row['username'] . '</td>
<td align="left">' . $row['service_date'] . '</td>
<td align="left">' . $row['review_date'] . '</td>
<td align="left">' . $row['review_rating'] . '</td>
<td align="left">' . $row['review_details'] . '</td>';
echo '</tr>';
$plumber = $row['company_name'];
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($db);
}
当此代码不会:
$query2 = "SELECT Review.username, Review.service_date, Review.review_date,
Review.review_rating, Review.review_details, Review.service_id,
Plumber.company_name
FROM Review
INNER JOIN Plumber
ON Review.service_id = Plumber.service_id
WHERE Plumber.service_id='$plumber_id'";
$response2 = @mysqli_query($db, $query2);
if ($_GET['sort'] == 'username') {
$query2 .= " ORDER BY username";
} else if ($_GET['sort'] == 'service_date') {
$query2 .= " ORDER BY service_date DESC";
} else if ($_GET['sort'] == 'review_date') {
$query2 .= " ORDER BY review_date DESC";
} else if ($_GET['sort'] == 'review_rating') {
$query2 .= " ORDER BY review_rating DESC";
} else {
$query2 .= " ORDER BY username";
}
if ($response2) {
echo '<table align="left" cellspacing="5" cellpadding="8" width="50%">
<td align="left"><b><a href="Plumber.php?id='.$plumber_id.'&sort=username">Username</a></b></td>
<td align="left"><b><a href="Plumber.php?id='.$plumber_id.'&sort=service_date">Service Date</a></b></td>
<td align="left"><b><a href="Plumber.php?id='.$plumber_id.'&sort=review_date">Review Date</a></b></td>
<td align="left"><b><a href="Plumber.php?id='.$plumber_id.'&sort=review_rating">Review Rating</a></b></td>
<td align="left"><b>Review Details</b></td>';
while($row = mysqli_fetch_array($response2)) {
echo '<tr><td align="left">' .
$row['username'] . '</td>
<td align="left">' . $row['service_date'] . '</td>
<td align="left">' . $row['review_date'] . '</td>
<td align="left">' . $row['review_rating'] . '</td>
<td align="left">' . $row['review_details'] . '</td>';
echo '</tr>';
$plumber = $row['company_name'];
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($db);
}
这两个页面之间的唯一区别在于,不是在SQL查询的WHERE子句中静态定义变量,而是向其传递从
$_GET
标记提取的值,并重命名了视图变量。在定义$ query2之后立即添加
'$query2 .= " ORDER BY var";'
将成功地按我用于var的任何行对表进行排序。我在if块的每一行中都使用了一个header('location: url')
,该块用于确定要传入的$_GET['sort']
值,以确保到达每一行并成功地为每一行重定向。有人发现我在做什么错吗?我的教授(因此也是我)并不担心诸如使用准备好的语句之类的适当做法-它只需要起作用即可。我只是想弄清楚什么坏了。
对不起这本小说,如果我违反规则,这是我第一次在这里发布
Here's what the table looks like for reference
最佳答案
我逃脱了你的变数。这将大部分时间解决查询问题。
$query2 = "SELECT Review.username, Review.service_date, Review.review_date,
Review.review_rating, Review.review_details, Review.service_id,
Plumber.company_name
FROM Review
INNER JOIN Plumber
ON Review.service_id = Plumber.service_id
WHERE Plumber.service_id='".$plumber_id."'";
$response2 = @mysqli_query($db, $query2);
这只是一个很好的建议:
switch ($_GET['osrt']) {
case 'something':
$query = "";
break
case 'somethingelse':
$query = "";
break;
default:
$query = "";
break;
}