新闻脚本插入新闻数据,但不编辑新闻

时间:2012-12-19 08:04:52

标签: php html mysql

所以我已经有了这个代码,所以它可以将新闻插入到它的表中,但问题是如果需要的话可以在之后编辑新闻。

我一直在尝试不同的方式,但似乎并没有完全停止。

<? 
if(!$id)
echo("Please choose a page to edit..");
elseif($id==edit)
{
$select = mysql_query("select * from news where newsid = '$id'");
$article = mysql_fetch_array($select);
?>
<form action="edit-news.php?id=edited" method="post">
    Title:<br />
    <input name="readuser" type="text" value="<? echo("$article[title]");?>" size="70" />
Article Content:<br />
<textarea name="pageuser" cols="40" rows="6"><? echo("$article[text1]");?></textarea>
    <br />
    <br />
    <input type="submit" value="Update article" />
</form>
<?
}
elseif($page==edited)
{
$text1 = $_POST[pageuser];
$title = $_POST[readuser];
$updateit = mysql_query("update news set text1 = '$text1' AND title = '$title' where newsid = $id");
echo("Article updated");
}
?>

我再次访问该页面时没有收到任何错误消息(谢天谢地!!!)但它只是没有编辑文章。

2 个答案:

答案 0 :(得分:3)

为文本输入和textareas指定一个值,以便它们显示要编辑的文章:

<input name="title" size="40" maxlength="255" value="<?php echo htmlspecialchars($title); ?>">

<textarea name="text1"  rows="7" cols="30"><?php echo htmlspecialchars($text1); ?></textarea>

<textarea name="text2" rows="7" cols="30"><?php echo htmlspecialchars($text2); ?></textarea>

同时将<?php echo $PHP_SELF ?>更改为:

<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>

附注:mysql_*已弃用,请将MySQLi或PDO与预准备语句一起使用。您的查询容易受到SQL注入攻击。快速解决方法是:

 mysql_query("SELECT * FROM news WHERE newsid='" . (int)$_GET['newsid'] . "'",$connect);

同样mysql_real_escape_string()优于mysql_escape_string(),因为后者不尊重字符集。然而,两者都不比准备好的陈述好。

答案 1 :(得分:0)

试试这个:

UPDATE news SET title = '$title', dtime = NOW(), text1 = '$text1', text2 = '$text2' WHERE newsid = '$newsid'