Php 使用用户编辑帖子更新数据库
我试图用一个用户在论坛中编辑的帖子更新我的数据库。整个编辑表单都在运行,除了:当他们单击编辑时,表单提交并转到主论坛页面,但数据库和帖子不会更改 我的保存编辑代码如下:Php 使用用户编辑帖子更新数据库,php,mysql,Php,Mysql,我试图用一个用户在论坛中编辑的帖子更新我的数据库。整个编辑表单都在运行,除了:当他们单击编辑时,表单提交并转到主论坛页面,但数据库和帖子不会更改 我的保存编辑代码如下: #data preparation for the query $id=intval($_POST['id']); $a_id=intval($_POST['a_id']); $question_id=intval($_POST['question_id']); foreach ($_POST as $key =&
#data preparation for the query
$id=intval($_POST['id']);
$a_id=intval($_POST['a_id']);
$question_id=intval($_POST['question_id']);
foreach ($_POST as $key => $value)
$_POST[$key] =
mysql_real_escape_string($value);
$sql = "UPDATE $tbl_name SET
a_answer='$_POST[a_answer]' WHERE
a_id='$a_id' AND
question_id='$question_id'";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error()); }
mysql_close; header ("location:
main_forum.php");
?>
#data preparation for the query
$id=intval($_GET['id']);
$a_id=intval($_GET['a_id']);
$question_id=intval($_GET['question_id']);
# selects title and description fields from database
$sql = "SELECT a_answer FROM $tbl_name WHERE a_id='$a_id' AND question_id='$question_id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
我的编辑页面代码如下:
#data preparation for the query
$id=intval($_POST['id']);
$a_id=intval($_POST['a_id']);
$question_id=intval($_POST['question_id']);
foreach ($_POST as $key => $value)
$_POST[$key] =
mysql_real_escape_string($value);
$sql = "UPDATE $tbl_name SET
a_answer='$_POST[a_answer]' WHERE
a_id='$a_id' AND
question_id='$question_id'";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error()); }
mysql_close; header ("location:
main_forum.php");
?>
#data preparation for the query
$id=intval($_GET['id']);
$a_id=intval($_GET['a_id']);
$question_id=intval($_GET['question_id']);
# selects title and description fields from database
$sql = "SELECT a_answer FROM $tbl_name WHERE a_id='$a_id' AND question_id='$question_id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
还有HTML
<h3>Edit</h3>
<form action="save_edit.php" method="get" name="myForm" />
<center>
<table>
<tr>
<td valign="top">
<b>Answer</b>
</td>
<td>
<textarea cols="80%" rows="10" name="a_answer">
<?php echo htmlspecialchars($rows['a_answer']); ?>
</textarea>
</td>
</tr>
<tr>
<td colspan="3">
<input name="a_id" type="hidden" value="<? echo $rows['a_id']; ?>">
<input name="question_id" type="hidden" value="<? echo $rows['question_id']; ?>">
<input type="submit" name="Submit" value="edit post">
<?php mysql_close(); ?>
</input>
</input>
</input>
</td>
</tr>
</table>
</center>
编辑
答复
你把get
和post
搞混了。在您的表单中,您使用method=“get”
,而在处理页面中使用$\u POST
。将表格更改为method=“post”
:
另外,你不应该用
/>
关闭一个开始标记,因为我的sql查询无法识别问题id或一个id,如果你注释掉标题(“location:main_forum.php”)代码>你看到任何mysql错误了吗?哦。我没有看到您使用die()
。接下来发生什么并不重要。