Home > Backend Development > PHP Tutorial > php页面传递参数的问题,求指教

php页面传递参数的问题,求指教

WBOY
Release: 2016-06-20 12:58:19
Original
1047 people have browsed it

$query = $db->findall("p_newsclass where f_id='$_GET[cid]'");while ($row = $db->fetch_array($query)) {	$news_class_in.= $row[id].",";}echo $news_class_in=$news_class_in."$_GET[cid]";
Copy after login

就是显示新闻列表,能正确打印出来

但是写到SQL中
$result = mysql_query("select id from p_newsbase where cid in ($news_class_in)");$total = mysql_num_rows($result);pageft($total, 20);if ($firstcount < 0)	$firstcount = 0;$query = $db->findall("p_newsbase where cid in ($news_class_in)  limit  $firstcount, $displaypg");
Copy after login


就出现错误了,获取的cid居然变了


回复讨论(解决方案)

$news_class_in.= $row[id].",";
这样拼接出来的结果是会多出一个逗号,可以用substr或者rtrim去掉:
while ($row = $db->fetch_array($query)) {
$news_class_in.= $row[id].",";
}
$new_class_in = rtrim($new_class_in, ",");

echo $new_class
看你的截图,应该就是cid in()括号中的最后一个逗号引起的sql错误

URL上是id,怎么用$_GET[ cid] 获取?

URL上是id,怎么用$_GET[ cid] 获取?




$query = $db->findall("p_newsclass where f_id='$_GET[cid]'");while ($row = $db->fetch_array($query)) {	$news_class_in.= $row[id].",";}echo  $news_class_in=$news_class_in."$_GET[cid]";
Copy after login




这是直接打印URL获取的就是cid

下面写的htm文件指定的链接是cid啊
{section name=l loop=$sm_list}     <div class="listtt">      <h2><a href="list.php?cid={$sm_list[l].cid}">{$sm_list[l].cidname}</a></h2>      <span><a href="view.php?id={$sm_list[l].id}">{$sm_list[l].title}</a></span>      <div id="more">{$sm_list[l].date_time}</div>    </div>    {/section}
Copy after login

$news_class_in.= $row[id].",";
这样拼接出来的结果是会多出一个逗号,可以用substr或者rtrim去掉:
while ($row = $db->fetch_array($query)) {
$news_class_in.= $row[id].",";
}
$new_class_in = rtrim($new_class_in, ",");

echo $new_class
看你的截图,应该就是cid in()括号中的最后一个逗号引起的sql错误



谢谢回答,看我下面的截图,为什么我直接打印就不会有后面的逗号呢?

最后一张图,错误的sql语句,多了一个“,”。
你这样写循环最后会多一个逗号

$news_class_in 这个变量最后多了一个逗号。

加一句:

$news_class_in = trim($news_class_in, ',');
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template