URLにユーザーが表示しているページのcat_nameが表示されるようにします。現在はcategory.php?id=2または
category.php?id=' . $row['cat_id'] . '
category.php?title=Soccerまたは
category.php?title=' . $row['cat_name'] . '
ただし、その行を置き換えると、他のページで問題が発生します。
ページcategory.phpは次のとおりです。
<?php
//category.php
include 'connect.php';
//first select the category based on $_GET['cat_id']
$sql = "SELECT
cat_name,
cat_id,
cat_description
FROM
categories
WHERE
cat_id = '" . mysql_real_escape_string($_GET['id']) ."'";
$result = mysql_query($sql);
if(!$result)
{
echo 'The category could not be displayed, please try again later.' . mysql_error();
}
else
{
if(mysql_num_rows($result) == 0)
{
echo 'This category does not exist.';
}
else
{
//display category data
while($row = mysql_fetch_assoc($result))
{
echo '<h2>Topics in ′' . $row['cat_name'] . '′ category</h2><br />';
}
//do a query for the topics
$sql = "SELECT topic_id, topic_subject, topic_date, topic_cat
FROM topics
JOIN categories
ON topics.topic_cat = categories.cat_id
WHERE cat_id = " . mysql_real_escape_string($_GET['id']);
$result = mysql_query($sql);
if(!$result)
{
echo 'The topics could not be displayed, please try again later.';
}
else
{
if(mysql_num_rows($result) == 0)
{
echo 'There are no topics in this category yet.';
}
else
{
//prepare the table
echo '<table border="1">
<tr>
<th>Topic</th>
<th>Created at</th>
</tr>';
while($row = mysql_fetch_assoc($result))
{
echo '<tr>';
echo '<td class="leftpart">';
echo '<h3><a href="topic.php?id=' . $row['topic_id'] . '">' . $row['topic_subject'] . '</a><br /><h3>';
echo '</td>';
echo '<td class="rightpart">';
echo date('d-m-Y', strtotime($row['topic_date']));
echo '</td>';
echo '</tr>';
}
}
}
}
}
?>
問題は、URLを置き換えると、2番目の$ sql(すぐ下にあるもの:トピックのクエリを実行する)が機能しないようになることです。SQLがURLへの変更で機能するようにしたいと思います。
cat_idをcat_nameに変更したいのですが、変更すると、機能しなかったというエラーが表示されます。したがって、問題は、URLにcat_nameを付けたいのに、SQLではCAT_idをGETする必要があるということです。cat_nameを使用し、それを機能させるために、SQLクエリで何を変更しますか?
それが問題のより良い説明であることを願っています。さらに情報が必要な場合は教えてくださいありがとう、ライアン