2

Here is my test form (link to form removed) if you click on the update ticket link it will bring you to a page to add additional information to the record. I see the id for that record passed to the form in the browser url as the end of the url says ?id=10 or whatever the id number for that record is. This is where I'm stuck, I can't get the record to update, I input the info in the form and click the update button and it tells me it added 1 record but it doesn't add the info to MySQL and I don't get any errors either. so here is the code I'm using to update the record:

<?php

include 'db_connect.php';

$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$id=$_REQUEST['id'];

 mysql_query("UPDATE `test` SET 
                                 `city` = '$city',
                                 `state` = '$state',
                                 `zip` = '$zip',
                           WHERE `id` = '$id'") 

?>

I added a hidden input field to my form with the name of 'id' thinking that was what was wrong but no change. What am I doing wrong? what do I need to do to pass the id? Thanks!

Adding the code for my update form:

<html>
<body>

<form action="update.php" method="post">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
City <input type="text" name="city">
State<input type="text" name="state">
Zip <input type="text" name="zip">
<input type="submit" value="update">
</form>

</body>
</html> 

Revised update code:

<?php

include 'db_connect.php';

$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$id=$_POST['id'];




 mysql_query("UPDATE `test` SET      `city` = '$city', `state` = '$state', `zip` = '$zip' WHERE `id` = '$id'");



?>
Jason
  • 224
  • 3
  • 11

2 Answers2

5

You need to post id with the form.

//Add this hidden field inside form to store and post id
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"/>

The id gets lost when form is posted. so we need to store it and send it with the form.

// remove the comma before where in this line
mysql_query("UPDATE `test` SET      `city` = '$city', `state` = '$state', `zip` = '$zip' WHERE `id` = '$id'");
cartina
  • 1,409
  • 13
  • 21
  • I had the hidden input field named 'id' I just didn't have the php value in it so I've added that and when I view source of the page I can see the value in the field but it still doesn't update the record in MySQL also I don't know what's up with this but I type "@cartina" and it doesn't show up in my comment! – Jason May 31 '13 at 06:45
  • When you post the form, set $id=$_POST['id']; update will work. – cartina May 31 '13 at 06:51
  • I do have it set $id=$_POST['id']; and it's not posting, I'm posting the revised code in my question now. – Jason May 31 '13 at 06:55
  • Could you please print your post array like print_r($_POST) and echo your query like echo $query="UPDATE `test` SET `city` = '$city', `state` = '$state', `zip` = '$zip', WHERE `id` = '$id'"; and post the result here. – cartina May 31 '13 at 06:58
  • Add these 2 lines to your update.php file so that i can check – cartina May 31 '13 at 07:03
  • here is what displays after the update is submitted UPDATE test SET city = 'Dallas', state = 'Texas', zip = '89987', WHERE id = '1'Array ( [id] => 1 [city] => Dallas [state] => Texas [zip] => 89987 ) – Jason May 31 '13 at 07:05
  • You got comma before where " `zip` = '$zip', WHERE " in your code. Please remove comma after '$zip' . The syntax is incorrect. You can user in future to check for any errors in youir query. – cartina May 31 '13 at 07:19
2

You are passing id by GET method. so use $_GET['id] to get id. Assign this id to a hidden field. Like: <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>"/>

In update page. $id=$_POST['id'];//get id value by post method

 mysql_query("UPDATE `test` SET      `city` = '$city', `state` = '$state', `zip` = '$zip', WHERE `id` = '$id'");
Fasil kk
  • 2,147
  • 17
  • 26
  • so I added $id=$_POST['id']; in my upate page along with adding the php value in the hidden input box and it still wont post to the record in mysql – Jason May 31 '13 at 06:48
  • should be inside form tag. – Fasil kk May 31 '13 at 06:58
  • it is inside the form tag, you can see the updated code in my question – Jason May 31 '13 at 07:09
  • use print_r($_POST); to check values are posting or not. – Fasil kk May 31 '13 at 07:12
  • I am using it, if you use the form in the link in my question you can see the array results show the id and all the inputs to the form are being supposedly posted but nothing is posted in the db. I know I can post to the db as I added a new record first then tried to update it but nothing posts in the update. – Jason May 31 '13 at 07:21
  • Thanks for your help, @Cartina found an error in my syntax, there was a comma after city in my query, removed it and now it's all good. Thanks again! – Jason May 31 '13 at 07:43