在CMS上更新新闻

时间:2012-07-21 05:09:52

标签: php mysql

所以我一直在尝试为客户编写CMS来更新他们的网站。我正在尝试让更新新闻脚本工作,我想知道如何让脚本不仅更新新闻本身而且还更新标题。这是脚本:

PHP:

    //used to get data from previous page. $previous_title was so I can use it to set the where in the update query.
    if($_GET['title']) {
            $title = $_GET['title'];
             $previous_title = $_GET['title'];
    }
    if($_POST['title']) {
            $title = $_POST['title'];
            $news = $_POST['news'];

            //used to update both news and title
           mysql_query("UPDATE `News` SET `Title`='$title', `Content`='$news' WHERE `Title`='$previous_title'");
    header("Location: viewNews.php");
    }

HTML:

                <form id = "addNews" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                <input type="hidden" name="hidden" id="hidden" value="hidden" />
                <input type="text" name="title" size="68" id = "title" value="<?php echo $query['Title'];?>"/><br/><br/>
                <textarea name="news" id="news"><?php echo $query['Content'];?></textarea><br />
                <input type="submit" value="Update News"/>
                </form>

谢谢!

1 个答案:

答案 0 :(得分:0)

在更新查询中,您要更新两个字段Title & Content

UPDATE `News` SET `Title`='$title', `Content`='$news' WHERE `Title`='$previous_title

但是如果你只想更新新闻那么你的SQL就像这样:

UPDATE `News` SET `Content`='$news' WHERE `Title`='$previous_title

你应该照顾$previous_title 因为在这种情况下$previous_title可能不可用

if($_POST['title']) {
            $title = $_POST['title'];
            $news = $_POST['news'];

           mysql_query("UPDATE `News` SET `Title`='$title', `Content`='$news' WHERE `Title`='$previous_title'");
    header("Location: viewNews.php");
    }