1

I am trying to display a database input in a textbox for editing. I cant seem to get it working?! The page links from a forum post edit button. Firstly, I don't know how to get the database info for that particular field to be displayed and also I don't understand how to make sure the post that the user clicks to edit is the post that is displayed.

My code looks like this

<?php
#data preparation for the query 
$id=$_GET['id']; 

# selects title and description fields from database 
$sql = "SELECT a_answer FROM $tbl_name WHERE question_id='$id'"; 
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

?> 
<h3>Edit</h3> <form action="save_edit.php" enctype="multipart/form-data" method="post" name="myForm" />   
<table>     

<tr>       
<td><b>Answer</b></td>       
<td><textarea cols="80%" rows="10" name="a_answer"><?php echo $row['$a_answer']; ?></textarea></td>   
  </tr>  
   </table>   <input name="id" type="hidden" value="<? echo $id; ?>">   
   <input name="enter" type="submit" value="Edit"> </form>  

  <?php mysql_close(); ?>
2
  • 1
    $rows contains row and you are usign $row a suggestion as well How about going with some good tutorial Commented Apr 30, 2011 at 9:58
  • yep, fixed that and still no luck Commented Apr 30, 2011 at 10:24

2 Answers 2

1

confirm if the question id is integer or varchar in your database. if it is integer then remove the single quotes. ( just a hint may it helps you. ). One more thing you do not need to put the multipart in your form if you not uploading any files another tip form me.

Sign up to request clarification or add additional context in comments.

Comments

0

Two errors in your code: Firstly, you are using row instead of rows. The second is the dollar sign you 're using when accessing the field. The code below should work, provided your database query is correct.

<?php
#data preparation for the query 
$id=$_GET['id']; 

# selects title and description fields from database 
$sql = "SELECT a_answer FROM $tbl_name WHERE question_id='$id'"; 
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

?> 
<h3>Edit</h3> <form action="save_edit.php" enctype="multipart/form-data" method="post" name="myForm" />   
<table>     

<tr>       
<td><b>Answer</b></td>       
<td><textarea cols="80%" rows="10" name="a_answer"><?php echo $rows['a_answer']; ?></textarea></td>   
  </tr>  
   </table>   <input name="id" type="hidden" value="<? echo $id; ?>">   
   <input name="enter" type="submit" value="Edit"> </form>  

  <?php mysql_close(); ?>

Another way to go about things would be to use prepared statements, and let PHP and MySQL quess the correct type. Here can be found a simple example.

1 Comment

No problem. You should indicate that this answer is the solution to your problem by accepting it, so others can potentially benefit. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.