开发者

updating database with user edit

I am trying to update my database with a post that a user has edited in a forum. The whole edit form is functioning excet for when they click edit the form submits and goes to the main forum page, but the database and the post doesn't change.

When the submit edit button is pressed I have this:

    <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">

My save edit code is this:

 #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_answ开发者_开发知识库er]' 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"); 

 ?>

Any ideas???

EDIT

For those who find this question useful unlike @mario, the problem was in the variable I was sending to the save edit page.

       <input name="a_id" type="hidden" value="<? echo $rows['a_id']; ?>">     
<input name="question_id" type="hidden" value="<? echo $rows['question_id']; ?>">

Should have been

    <input name="a_id" type="hidden" value="<? echo $a_id; ?>">     
<input name="question_id" type="hidden" value="<? echo $question_id; ?>">


First of all I would make the syntax a bit nicer.

$foor    =     $bar;
foreach()
{

}

Than I would not work with the $_POST like that. make an array such as $data=array()

than you go

foreach ($_POST as $key => $value)
{
     $data[$key] => mysql_real_escape_string($value);
}

And after that your $sql needs to look like this:

$sql = "UPDATE $tbl_name SET a_answer=`$data[a_answer]` WHERE a_id=$a_id AND question_id=$question_id";

Then, if I were you I would make a print on your $sql.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜