By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,290 Members | 544 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,290 IT Pros & Developers. It's quick & easy.

PHP page won't update mysql record

P: 5
Setup: I only have one database with one table in it. The first page has a form that adds a record (w/ 6 fields in it) to the mySQL database's lone table via PHP. This works fine. I also have a PHP table that displays all records in my database on this same page. This too works fine. I have my table set up so that there is a 7th field, called "rid" to act as a "record id" or unique identifying field. This is primary and auto-incremented. There is a second form on the same page that prompts the user to enter a "rid". One this form is submitted it loads a second PHP page that displays the fields of whichever record was specified by the "rid" and allows the user to edit them.

Problem: When this second PHP page's form is submitted, a third PHP page is supposed to UPDATE the mySQL table. It... doesn't, despite echoing that is has.

Second Page Code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', true);
  4. foreach($HTTP_POST_VARS as $varname => $value)
  5. $formVars[$varname]=$value;
  6. $db1=mysql_connect("XXXXX","XXXXXXX","XXXXXXX");
  7. mysql_select_db("tracking");
  8. $query="SELECT * FROM job_status WHERE rid = \"".$formVars["rid"]."\"";
  9. $result=mysql_query($query);
  10. $row=mysql_fetch_array($result);
  11. $formVars = array();
  12. $formVars["bone"]=$row["bone"];
  13. $formVars["ctwo"]=$row["ctwo"];
  14. $formVars["lthree"]=$row["three"];
  15. $formVars["rfour"]=$row["rfour"];
  16. $formVars["ifive"]=$row["ifive"];
  17. $formVars["fsix"]=$row["fsixl"];
  18. $formVars["rid"]=$row["rid"];
  19. mysql_close($db1);
  20. ?>
  21. <html>
  22. <head>
  23. <title>Job Status Update</title>
  24. </head>
  25. <body bgcolor="white">
  26. <form method="post" action="page3.php">
  27. <table>
  28. <col span="1" align="right">
  29. <tr>
  30. <td><font color="blue">Bone:</font></td>
  31. <td><input type="text" name="bone" 
  32. value="<? echo $formVars["bone"]; ?>" size=100></td>
  33. </tr>
  34. <tr>
  35. <td><font color="blue">Ctwo:</font></td>
  36. <td><input type="text" name="ctwo" 
  37. value="<? echo $formVars["ctwo"]; ?>" size=100></td>
  38. </tr>
  39. <tr>
  40. <td><font color="blue">Lthree:</font></td>
  41. <td><input type="text" name="lthree" 
  42. value="<? echo $formVars["lthree"]; ?>" size=100></td>
  43. </tr>
  44. <tr>
  45. <td><font color="blue">Rfour:</font></td>
  46. <td><input type="text" name="rfour" 
  47. value="<? echo $formVars["rfour"]; ?>" size=100></td>
  48. </tr>
  49. <tr>
  50. <td><font color="blue">Ifive:</font></td>
  51. <td><input type="text" name="ifive" 
  52. value="<? echo $formVars["ifive"]; ?>" size=100></td>
  53. </tr>
  54. <tr>
  55. <td><font color="blue">Fsix:</font></td>
  56. <td><input type="text" name="fsix" 
  57. value="<? echo $formVars["fsix"]; ?>" size=100></td>
  58. </tr>
  59. <tr>
  60. <td><font color="blue">rid:</font></td>
  61. <td><input type="text" name="rid" 
  62. value="<? echo $formVars["rid"]; ?>" size=100></td>
  63. </tr>
  64. <tr>
  65. </tr>
  66. <tr>
  67. <td><input type="submit" value="Submit"></td>
  68. </tr>
  69. </body>
  70. </html>
  71.  
Third Page Code:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Job Status Updated</title>
  4. </head>
  5. <body bgcolor="white">
  6. <?php
  7. foreach($HTTP_POST_VARS as $varname => $value)
  8. $formVars[$varname]=$value;
  9. $db1=mysql_connect("XXXXXX","XXXXXX","XXXXXX");
  10. mysql_select_db("tracking");
  11. echo "Record updated<br><a href=\"tracking.php\">click here</a> to update another record<br>";
  12. $query="UPDATE job_status set ". 
  13. "bone= \"".$formVars["bone"]."\",".
  14. "ctwo= \"".$formVars["ctwo"]."\",".
  15. "lthree= \"".$formVars["lthree"]."\",".
  16. "rfour= \"".$formVars["rfour"]."\",".
  17. "ifive= \"".$formVars["ifive"]."\",".
  18. "fsix= \"".$formVars["fsix"]."\",".
  19. "\" WHERE rid = \"".$formVars["rid"]."\"" or die('<hr />MySQL Error: ' .mysql_error(). '<hr />');
  20. mysql_query($query);
  21. mysql_close($db1);
  22. ?>
  23. </body>
  24. </html>
  25.  
(I've removed the mysql_connect values from the code. I don't think the first page's code needs to be included as everything is passed perfectly fine from the first to the second and everything works fine on that page)

Can anyone provide any pointers on what I'm messing up? I'm new to PHP and I feel like I've made some dumb typo but just can't see it. (The table will update in PHPadmin perfectly fine)
Jan 25 '08 #1
Share this Question
Share on Google+
11 Replies

brettl
P: 41
I don't think you need the foreach($HTTP_POST_VARS as $varname => $value).

Read here about Predefined Variables .
Jan 25 '08 #2

stepterr
100+
P: 157
Setup: I only have one database with one table in it. The first page has a form that adds a record (w/ 6 fields in it) to the mySQL database's lone table via PHP. This works fine. I also have a PHP table that displays all records in my database on this same page. This too works fine. I have my table set up so that there is a 7th field, called "rid" to act as a "record id" or unique identifying field. This is primary and auto-incremented. There is a second form on the same page that prompts the user to enter a "rid". One this form is submitted it loads a second PHP page that displays the fields of whichever record was specified by the "rid" and allows the user to edit them.

Problem: When this second PHP page's form is submitted, a third PHP page is supposed to UPDATE the mySQL table. It... doesn't, despite echoing that is has.

Second Page Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
foreach($HTTP_POST_VARS as $varname => $value)
$formVars[$varname]=$value;
$db1=mysql_connect("XXXXX","XXXXXXX","XXXXXXX");
mysql_select_db("tracking");
$query="SELECT * FROM job_status WHERE rid = \"".$formVars["rid"]."\"";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$formVars = array();
$formVars["bone"]=$row["bone"];
$formVars["ctwo"]=$row["ctwo"];
$formVars["lthree"]=$row["three"];
$formVars["rfour"]=$row["rfour"];
$formVars["ifive"]=$row["ifive"];
$formVars["fsix"]=$row["fsixl"];
$formVars["rid"]=$row["rid"];
mysql_close($db1);
?>
<html>
<head>
<title>Job Status Update</title>
</head>
<body bgcolor="white">
<form method="post" action="page3.php">
<table>
<col span="1" align="right">
<tr>
<td><font color="blue">Bone:</font></td>
<td><input type="text" name="bone"
value="<? echo $formVars["bone"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Ctwo:</font></td>
<td><input type="text" name="ctwo"
value="<? echo $formVars["ctwo"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Lthree:</font></td>
<td><input type="text" name="lthree"
value="<? echo $formVars["lthree"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Rfour:</font></td>
<td><input type="text" name="rfour"
value="<? echo $formVars["rfour"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Ifive:</font></td>
<td><input type="text" name="ifive"
value="<? echo $formVars["ifive"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Fsix:</font></td>
<td><input type="text" name="fsix"
value="<? echo $formVars["fsix"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">rid:</font></td>
<td><input type="text" name="rid"
value="<? echo $formVars["rid"]; ?>" size=100></td>
</tr>
<tr>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</body>
</html>

Third Page Code:
<html>
<head>
<title>Job Status Updated</title>
</head>
<body bgcolor="white">
<?php
foreach($HTTP_POST_VARS as $varname => $value)
$formVars[$varname]=$value;
$db1=mysql_connect("XXXXXX","XXXXXX","XXXXXX");
mysql_select_db("tracking");
echo "Record updated<br><a href=\"tracking.php\">click here</a> to update another record<br>";
$query="UPDATE job_status set ".
"bone= \"".$formVars["bone"]."\",".
"ctwo= \"".$formVars["ctwo"]."\",".
"lthree= \"".$formVars["lthree"]."\",".
"rfour= \"".$formVars["rfour"]."\",".
"ifive= \"".$formVars["ifive"]."\",".
"fsix= \"".$formVars["fsix"]."\",".
"\" WHERE rid = \"".$formVars["rid"]."\"" or die('<hr />MySQL Error: ' .mysql_error(). '<hr />');
mysql_query($query);
mysql_close($db1);
?>
</body>
</html>

(I've removed the mysql_connect values from the code. I don't think the first page's code needs to be included as everything is passed perfectly fine from the first to the second and everything works fine on that page)

Can anyone provide any pointers on what I'm messing up? I'm new to PHP and I feel like I've made some dumb typo but just can't see it. (The table will update in PHPadmin perfectly fine)
Just the other day I had an update statement that was acting goofy and for the life of me couldn't figure out what was going on. I also use a foreach loop to go through each of the form fields since my form is a dynamic table with multiple rows. Anyways, it ended up being something small, but the way I finally figured out the problem was by printing my update statement to the screen and then the problem was very obvious.

Also, you said that it is telling you that it is telling you that it is updating, but it looks like you are printing your "Record Updated" before it even does the query. You should be doing an if statement on if the query executed correctly and then printing your message based on that, just a suggestion.
Jan 25 '08 #3

P: 5
I don't think you need the foreach($HTTP_POST_VARS as $varname => $value).

Read here about Predefined Variables .
Brett,

I read that bit about the change from $HTTP_POST_VARS to using $_POST. I have register_globals set to on and am using PHP 4.3. I understand that w/ register_globals on then $HTTP_POST_VARS should behave the same as it is would treated as a global variable. I can't seem to get it to work out yet.

I did just see something about setting the "name=" portions of my form inputs to (all the same) "name []" and using $_POST in the foreach loop. I'll give that a whirl.
Jan 25 '08 #4

Atli
Expert 5K+
P: 5,058
Hi.

$HTTP_POST_VARS and $_POST are essentially the same thing, except the former is an older version that should be avoided.
If you are using PHP 4 or higher you should use $_POST.

The "register_globals" directive has nothing to do with either the $_POST or the old $HTTP_POST_VARS arrays.
It simply takes every single POST variable and creates a normal variable in the global scope using the name in the "name" parameter of the input box.

That is, when "register_globals" is on, this code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   echo $_POST['var1'];
  3.   echo $_POST['var2'];
  4. ?>
  5.  
Could be written like so:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   echo $var1;
  3.   echo $var2;
  4. ?>
  5.  
I don't recommend relying on this because of the security risk it poses. Many hosting services have this turned off so any code relying on this would be useless on such a server.

As to posting input boxes with "name[]" as a name, that creates an array inside the $_POST array. Like, for example:
Expand|Select|Wrap|Line Numbers
  1. <form action="page.php" method="POST">
  2.   <input type="text" name="textbox[]" />
  3.   <input type="text" name="textbox[]" />
  4.   <input type="text" name="textbox[]" />
  5. </form>
  6.  
Submitting this code would populate the $_POST super-global somewhat like this:
Expand|Select|Wrap|Line Numbers
  1. array(
  2.   textbox = array(
  3.     0 => "text in box 1",
  4.     1 => "text in box 2",
  5.     2 => "text in box 3"
  6.   )
  7. )
  8.  
Which could be read like this:
Expand|Select|Wrap|Line Numbers
  1. foreach($_POST['textbox'] as $_k => $_v) {
  2.   echo "Box #{$_k} = {$_v} <br />";
  3. }
  4.  
Which would print the three values in the textboxes.
Jan 26 '08 #5

P: 5
Atli,

Thank you for your response. You've pushed me along in my PHP learning experience. On my second page I was able to substitute $_POST for $HTTP_POST_VARS perfectly fine -- the query still successfully pulls the record's fields and inserts them into the html form for editing. However, on the third page (the PHP page updating the mysql table) is still unsuccessful.

I understand that the foreach() loop is looking for an array. Upon replacing $HTTP_POST_VARS w/ $_POST the script does not run. From your explanation $_POST on my third page contains an array of the name= & value= from the input fields passed from the HTML form from the second page. I've also tried playing around with variations of something like this with no luck:

Expand|Select|Wrap|Line Numbers
  1. $postArray=$_POST ['bone'], ['ctwo'], ['lthree'];
  2. foreach($postArray as $varname => $value)
  3. $formVars[$varname]=$value;
  4.  
...............and
Expand|Select|Wrap|Line Numbers
  1. $postArray=$_POST [' '];
  2. foreach($postArray as $varname => $value)
  3. $formVars[$varname]=$value;
  4.  
...............and
Expand|Select|Wrap|Line Numbers
  1. $postArray=$_POST;
  2. foreach($postArray as $varname => $value)
  3. $formVars[$varname]=$value;
  4. $db1=mysql_connect("XXXXXX","XXXXXX","XXXXXX");
  5. mysql_select_db("tracking");
  6. echo "Record updated<br><a href=\"tracking.php\">click here</a> to update another record<br>";
  7. $query="UPDATE job_status set ".
  8. "bone= \"".$formVars["bone"]."\",".
  9. "ctwo= \"".$formVars["ctwo"]."\",".
  10. "lthree= \"".$formVars["lthree"]."\",".
  11. "rfour= \"".$formVars["rfour"]."\",".
  12. "ifive= \"".$formVars["ifive"]."\",".
  13. "fsix= \"".$formVars["fsix"]."\",".
  14. "\" WHERE rid = \"".$formVars["rid"]."\"" or die('<hr />MySQL Error: ' .mysql_error(). '<hr />');
  15. mysql_query($query);
  16. mysql_close($db1);
  17. ?>
  18.  
I don't understand how to pass the data posted from the form (which I thought would just be contained in $_POST) into the foreach() loop. Depending on how I order things I'll get an error message like "unidentified index at line..." at the lines with "bone= \"".$formVars["bone"]."\",". etc... I've been reading the PHP Cookbook and thought I had something... but nothing works.

Every article I come across seems to deal with multiple element forms like checkboxes, radio buttons or drop down fields with and array in the "name=" like you mentioned before. I'm looking for something for several text inputs each with a different "name=".

Any ideas on what/where I should investigate next?
Jan 26 '08 #6

stepterr
100+
P: 157
Ok, looking at your code for your second page it doesn't look like you are doing anything with arrays in your form. So why don't you just have your update statement like this and not use a for loop, since you don't need it, unless I'm completely overlooking something.

[PHP]$query="UPDATE job_status set ".
"bone= \"".$_POST["bone"]."\",".
"ctwo= \"".$_POST["ctwo"]."\",".
"lthree= \"".$_POST["lthree"]."\",".
"rfour= \"".$_POST["rfour"]."\",".
"ifive= \"".$_POST["ifive"]."\",".
"fsix= \"".$_POST["fsix"]."\",".
"\" WHERE rid = \"".$_POST["rid"]."\"" or die('<hr />MySQL Error: ' .mysql_error(). '<hr />');[/PHP]
Jan 27 '08 #7

Atli
Expert 5K+
P: 5,058
After looking more closely at your code, there are a few things on page 3 you should take a closer look at.

First, you use double-quote marks in your query, where you should be using single-quote marks. MySQL does not use double-quote marks.

Second, the die() function that would tell you if your query fails is in the wrong place. It should be placed behind the mysql_query() function, not where you define your query string.

It should look more like this:
Expand|Select|Wrap|Line Numbers
  1. $query="
  2. UPDATE job_status SET
  3.   bone = '{$_POST['bone']}', 
  4.   ctwo = '{$_POST['ctwo']}', 
  5.   lthree = '{$_POST['lthree']}', 
  6.   rfour = '{$_POST['rfour']}', 
  7.   ifive = '{$_POST['ifive']}', 
  8.   fsix = '{$_POST['fsix']}'
  9. WHERE rid = {$_POST['rid']}";
  10. $result = mysql_query($query, $db1) or die('<hr />MySQL Error: ' .mysql_error(). '<hr />');
  11.  
Notice that you don't have to close the string at every new line. PHP will continue to parse the string until it reaches the closing quote-mark, even if it is not in the same line as the opening quote-mark.

Also, you do not have to close strings defined in double-quote marks to add variables. PHP will search the string for variables and replace them with their values.
Array elements can also be added directly into a double-quoted string, like I do in the example above, if they are enclosed in brackets.
As a rule, I always enclose all variables I place in strings inside brackets so they can be spotted more easily.
Jan 27 '08 #8

P: 5
Ok, looking at your code for your second page it doesn't look like you are doing anything with arrays in your form. So why don't you just have your update statement like this and not use a for loop, since you don't need it, unless I'm completely overlooking something.

[PHP]$query="UPDATE job_status set ".
"bone= \"".$_POST["bone"]."\",".
"ctwo= \"".$_POST["ctwo"]."\",".
"lthree= \"".$_POST["lthree"]."\",".
"rfour= \"".$_POST["rfour"]."\",".
"ifive= \"".$_POST["ifive"]."\",".
"fsix= \"".$_POST["fsix"]."\",".
"\" WHERE rid = \"".$_POST["rid"]."\"" or die('<hr />MySQL Error: ' .mysql_error(). '<hr />');[/PHP]
No, I'm not doing anything with arrays in my form... but the info from each input from the form is put into an array in $_POST; if i understand Atli correctly. That $_POST array of data was then to be sent through the foreach() loop which would parse the info into the appropriate places in the UPDATE query. At least that's how I thought it should work -- but doesn't. I've tried the method you have suggested and it unfortunately does not work either. Thanks for the suggestion though! :)
Jan 27 '08 #9

stepterr
100+
P: 157
No, I'm not doing anything with arrays in my form... but the info from each input from the form is put into an array in $_POST; if i understand Atli correctly. That $_POST array of data was then to be sent through the foreach() loop which would parse the info into the appropriate places in the UPDATE query. At least that's how I thought it should work -- but doesn't. I've tried the method you have suggested and it unfortunately does not work either. Thanks for the suggestion though! :)

Strantron, no, for what you are doing you won't need the foreach loop. I think the reason why it didn't work has more to do with your syntax, which I didn't correct when I looked through it the first time, sorry about that. Try this one.

[PHP]$query="UPDATE job_status set bone= '".$_POST['bone']."', ctwo= '".$_POST['ctwo']."', lthree= '".$_POST['lthree']."', rfour= '".$_POST['rfour']."', ifive= '".$_POST['ifive']."', fsix= '".$_POST['fsix']."' WHERE rid = '".$_POST['rid']."'"
$result = mysql_query($query, $db1) or die('<hr />MySQL Error: ' .mysql_error(). '<hr />');[/PHP]
Jan 27 '08 #10

P: 5
Earlier today I was reading through some other examples and I figured it out. I found a much simpler way than the other example I was trying to learn from. It was something more like the way Stepterr was recommending. I actually ended up making a few changes to all three of the pages I was referencing initially. Atli, thank you for the assistance in the syntax of things.

Thanks to everyone for all of the assistance with my new adventure in mySQL and PHP!
Jan 28 '08 #11

stepterr
100+
P: 157
Earlier today I was reading through some other examples and I figured it out. I found a much simpler way than the other example I was trying to learn from. It was something more like the way Stepterr was recommending. I actually ended up making a few changes to all three of the pages I was referencing initially. Atli, thank you for the assistance in the syntax of things.

Thanks to everyone for all of the assistance with my new adventure in mySQL and PHP!
Congrats! Glad you were able to get it working.
Jan 28 '08 #12

Post your reply

Sign in to post your reply or Sign up for a free account.