【发布时间】:2013-09-25 09:27:38
【问题描述】:
我正在开发一个定制的 CMS 来尝试学习一些关于 PHP 的东西。我的经验基于我看到/阅读的一些教程。
所以,我有一张名为page_hem 的表,其中保存了所有数据。然后我让它在整个页面的文本字段上回显,这暂时是我的 index.php。一切正常,特别是如果我从 phpMyAdmin 插入数据。
但是现在我正在创建编辑表单并且事情不再起作用了。我仍然可以将所有内容回显到文本框中,这样我就可以更改文本,但是当我按下“保存”按钮时,尽管我收到了成功消息,但我的变量不会在数据库中更新。我不知道问题是在编辑页面上还是在解析代码上;即使我是 Firebugging,我也没有收到任何错误。
这里是编辑页面:
<?php
require_once "../scripts/connect_to_mysql.php";
// Determine which page ID to use in our query below
if (!$_GET['pid']) {
$pageid = '1';
} else {
$pageid = ereg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}
// Query the body section for the proper page
$sqlCommand = "SELECT text1, text2 FROM page_hem WHERE id='$pageid' LIMIT 1";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$body1 = $row["text1"];
$body2 = $row["text2"];
}
mysqli_free_result($query);
?>
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="container">
<div class="home-page main">
<section class="grid-wrap" >
<header class="grid col-full">
<hr>
<form id="form" name="form" method="post" action="page_edit_parse.php">
<div class="grid col-two-thirds mq2-col-full">
<label>Titel:</label>
<textarea name="body1" id="body1" cols="3" rows="2"><?php echo $body1 ?></textarea>
<label>Meddelande</label>
<p><textarea name="body2" id="body2" cols="6" rows="10"><?php echo $body2 ?></textarea></p><br>
</div>
<input type="submit" name="button" id="button" value="Update!" />
</form>
</section>
</body>
</html>
这里是 page_edit_parse.php
<?php
// You may want to obtain refering site name that this post came from for security purposes here
// exit the script if it is not from your site and script
$body1 = $_POST["text1"];
$body2 = $_POST["text2"];
include_once "../scripts/connect_to_mysql.php";
// Add the updated info into the database table
$query = mysqli_query($myConnection, "UPDATE page_hem SET text1='$body1', text2='$body2', lastmodified='now()' WHERE id='$pid'") or die (mysqli_error($myConnection));
echo 'Updated, yeah! <br /><br /><a href="kpanel.php">Go back!</a>';
exit();
?>
PS:由于我编辑了html,可能会有一些小错误,所以它会更短。
【问题讨论】:
-
我看不出你在哪里定义
$pid -
还有什么错误,为什么不将输入转义到数据库中?这也可能导致问题。我建议 PDO 使用准备好的语句而不是 mysqli
-
1.
if (!$_GET['pid']) {使用 isset。 2.ereg_replace已弃用。我不知道你为什么没有收到通知。打开错误报告可能是什么? 3.在page_edit_parse.php,你还没有定义pid -
@LiamSorsby
mysqli也准备了声明。 -
我应该用什么来代替 ereg_replace itachi?
标签: php html post content-management-system