dongqin1861 2011-05-01 00:16
浏览 18

用户编辑更新数据库

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_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"); 

 ?>

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; ?>">
  • 写回答

1条回答 默认 最新

  • dongyuan9292 2011-05-01 00:30
    关注

    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.

    评论

报告相同问题?