Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用用户编辑帖子更新数据库_Php_Mysql - Fatal编程技术网

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="&lt;? echo $rows['a_id']; ?&gt;">
          <input name="question_id" type="hidden" value="&lt;? echo $rows['question_id']; ?&gt;">
            <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()
。接下来发生什么并不重要。