Andy
Andy

Reputation: 99

Updating database based on certain ID

I have a table within my database containing subscriptions, each subscription has a name, id and a notes column.

I'm trying to allow the user to update the notes column through a text area on the webpage. All of the subscriptions are in a list on the page which allows the user to click on them to view that specific subscription.

How would I make sure the note that is updated is correct with the id of the subscription they have clicked on?

I currently have this code.

<form method="POST" action="noteAction.php">
  <textarea id="notes" name="noteValue">$notes</texarea>
  <input type="submit" name="submit"/>
</form>

This is what I think my noteAction.php should look like however I cannot get it working.

    mysql_connect ("host", "user", "password") or die ('Error: ' . mysql_error());

    mysql_select_db("database_name") or die ('Data error:' . mysql_error());

    $text = mysql_real_escape_string($_POST['noteValue']); 
    $query="UPDATE `subscription` SET `notes`= '$text' WHERE `id` = '$id'";

    mysql_query($query) or die ('Error updating database ' . mysql_error());

Any help would be great, thanks.

Upvotes: 0

Views: 58

Answers (2)

mfisher91
mfisher91

Reputation: 807

When you're putting the note in the form, you must have an id for that note kicking about somewhere, after you retrieved it from the database. If you only selected the note contents in that query, select the ID as well. Then pass the ID over in a hidden field, and you have the ID to use in the MySQL query (which is correct).

<input type="hidden" name="note-id" value="note_id_here">

Upvotes: 1

Shailesh Katarmal
Shailesh Katarmal

Reputation: 2785

Use hidden element to store your id inside it.

 <form method="POST" action="noteAction.php">
   <textarea id="notes" name="noteValue">$notes</texarea>
   <input type="hidden" name="id" value="id" value="your id goes here" /> 
   <input type="submit" name="submit"/>
 </form>

Upvotes: 1

Related Questions