0

所以我一直在尝试为客户编写一个 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>

谢谢!

4

1 回答 1

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");
    }
于 2012-07-21T05:21:18.550 回答