检查是否设置了一个或多个$ _GET

时间:2012-03-02 11:44:11

标签: php mysql

生病再试一次:/

即时设置博客页面,

我希望它能够显示帖子,海报或者catagory或月份或年份,或者组合。 但只有$ _GET在url中。如果没有,他们只是去blogs.php我希望它只显示12个最近的帖子。不,我有自己的工作和海报独立工作。

这是我的代码到目前为止,在编辑了一点之后

                    <?php 


$category = mysql_real_escape_string( $_GET['Category']);
$Poster = mysql_real_escape_string($_GET['Poster']);
$Month = mysql_real_escape_string($_GET['Month']);
$Year = mysql_real_escape_string($_GET['Year']);


$sql = "SELECT * FROM blog, blog_poster, blog_category WHERE blog.category = '$category' 
AND  blog_poster.pid = blog.posterid 
AND blog_category.ID = blog.category 

OR posterid = '$Poster' 
AND  blog_poster.pid = blog.posterid 
AND blog_category.ID = blog.category  
ORDER BY timestamp DESC LIMIT 5";

$result = mysql_query($sql) or print ("Can't select entries from table.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
    $date = date("D M d Y", $row['timestamp']);

$title = stripslashes($row['title']);
$entry = stripslashes($row['entry']);
$id = $row['id'];
$entry =substr($entry, 0, 250);


?>

            <div class="ribbon">
                    <div class="wrapAround"></div>
                    <div class="tab">
                        <span class="blogDate"><?php echo $date; ?></span>
                        <span class="blogPostInfo">Posted by <a href="#"><?php echo $row['name']; ?></a> <!--|  <a href="#">0 Comments</a --></span>
                    </div>
                </div>
                <div class="blogPostSummary">
                    <h1><span><b><?php echo $title; ?></b></span></h1>
                    <div class="blogPostImage">
                        <a href='cs_blog/content.php?id=<?=$id?>'class="img">
                        <img src="../images/content/blogs/cs_blog/date/headers/blog-post-01.JPG" width="556" height="133" alt="blog post image" /></a>
                  </div>
                    <p>
                        <?php echo $entry."..&nbsp;<br><a href='content.php?id=$id'>Read more...</a>"; ?>
                    </p>
                    <p></p>
                </div>



                <!-- End of Content -->
                <?php }; ?>

2 个答案:

答案 0 :(得分:0)

试试这个

$category='DefaultCategory';
$poster='DefaultPoster';
$month='defaultMonth';
$month='defaultYear';

if(isset($_GET['Category']))
{
   $category = mysql_real_escape_string( $_GET['Category']);
}
if(isset($_GET['Poster']))
{
   $Poster = mysql_real_escape_string($_GET['Poster']);
}
if(isset($_GET['Month']))
{
   $Month = mysql_real_escape_string($_GET['Month']);
}
if(isset($_GET['Year']))
{
   $Year = mysql_real_escape_string($_GET['Year']);
}

$query="insert into sampleTable(category,poster,month,year)
 values ($Category,$Poster,$Month,$Year)"; 

如果您为表格设计中的所有字段设置了默认值,则为“或” 那么你只能插入提供的字段

答案 1 :(得分:0)

$conditions = array();
if( isset($_GET['Category']) ) $conditions[] = "category='". mysql_real_escape_string( $_GET['Category']) . "'";
if( isset($_GET['Poster']) )   $conditions[] = "poster='". mysql_real_escape_string( $_GET['Poster']) . "'";

if(!empty($conditions))
    $conditions = 'where ' . implode(' AND ', $conditions);
echo 'select * from table ' . $conditions;