View Full Version : Won't UPDATE - returns to a prior Command

08-12-2006, 11:51 AM
I have a table that isn't updating, and I think I have figured out what is happening, but not sure how to fix the problem.

I am going to try to explain how I have it set up to make this a little clearer...

You load Page A and then input and work order number. It loads Page B with all of the values, using this code to laod the values.

$Contact = $_POST['Contact'];
$query=mysql_query("SELECT Work, Sched, Name, Site, Serial, Hours, Starts, Issue, Severity,
Resolution, Assistance, PartsA, PartsB, PartsC, PartsD, PartsE, PartsF, PartsG,
Safety, Image1, Image2, Image3, Image4, FROM workorder WHERE Work='$Contact'");
if (!$query) {
echo mysql_error();
} else {
$workorderInfo = mysql_fetch_assoc($query);

//this is just for testing
if (mysql_num_rows($query) == 0) {
echo "No rows returned from table workorder using ID = '$Work'<br>\n";

This page is working fine to this point.

Now the person reviewing this page can add comments and a purchase order number. Once they are done reviewing it and adding comments they push the submit button and this code is suppose to run and update the form.

//} else
$Work = $_POST["Work"];
$Sched = $_POST["Sched"];
$Name = $_POST["Tech"];
$Site = $_POST["Site"];
$Serial = $_POST["Serial"];
$Hours = $_POST["Hours"];
$Starts = $_POST["Starts"];
$Issue = $_POST["Issue"];
$Severity = $_POST["Severe"];
$Resolution = $_POST["Resolve"];
$Assistance = $_POST["Assist"];
$PartsA = $_POST["PartsA"];
$PartsB = $_POST["PartsB"];
$PartsC = $_POST["PartsC"];
$PartsD = $_POST["PartsD"];
$PartsE = $_POST["PartsE"];
$PartsF = $_POST["PartsF"];
$PartsG = $_POST["PartsG"];
$Safety = $_POST["Safe"];
$Image1 = $_FILE["Image1"];
$Image2 = $_FILE["Image2"];
$Image3 = $_FILE["Image3"];
$Image4 = $_FILE["Image4"];
$Purchase = $_POST["Purchase"];
$Comment = $_POST["Comment"];
$Remarks = $POST_["Remarks"];

mysql_query ("UPDATE `workorder` set Purchase, Comment = '$Purchase', '$Comment' WHERE Work = '$Work'")or die(mysql_error());
echo "This Work Order has been Updated Successfully";
//echo print_r($_POST)

This is where the problem is occurring. The reply when this page is run is

"No rows returned from table workorder using ID =

and this is the comment if their was an error when Page A loaded Page B, with all the original data.

It is almost like it is going back to the top of the page when you press submit and running the first script again. Do I need to put the query to run when you push submit on a separate page and have the submit button direct to that page?

When the UPDATE works correctly it is suppose to echo echo "This Work Order has been Updated Successfully"; How do I add a link that you can push to return to a page of options, or load another page completely, only if it UPDATES properly?

I look forward to what your thoughts are on my troubleshooting ideas on what is happening here.

Thanks in advance,


08-13-2006, 11:23 AM
It is almost like it is going back to the top of the page when you press submit and running the first script again.

Interesting, since that's exactly what happens when you submit a page with an "action=" value of the same page.

PHP Primer:

#1. When a browser requests a page from the server, all of the PHP is processed in that page from the top to the bottom before it is delivered to the browser.
#2. When you submit a form with a method of "get" or "post", the values of the form elements are saved and passed along in the $_GET or $_POST variables, and the page specified in the "action" parameter of the form is requested from the server. See #1.

You must code your PHP pages with this in mind. You need to put if statements in to test if your page just got loaded, or has been reloaded because someone submitted the form. You can test the $_POST variables to differentiate between the two situations.

The other problem you may be having is I believe this particular page is called from the action parameter of another page's form... correct me if I have it wrong. Well... the data that first page sent to this page goes bye-bye once the page is delivered to the browser. That line of code "$Contact = $_POST['Contact'];" I believe is using a $_POST variable from that first page, so if by submitting this page you reload this page, you've lost $_POST['Contact']. You need to store the variable off to a $_SESSION variable (if you're using sessions) or a cookie variable, or reload a hidden form element on this page with that previous page's value like this:

<input type="hidden" name="Contact" value="<?php echo $_POST['Contact']; ?>" />

It's probably confusing but what that does is the first time the page comes from the first page, the first page's "Contact" value is stored in the hidden input. Every time the page is loaded after that, it stores the value of itself into itself again.

08-13-2006, 02:32 PM
That is quite a mouthful you have given me..........and you are right in saying that it is confusing.....:eek:

When I get done with this page (and I haven't ended up in the funny farm:D ) once you submit this page you will be taken to another page that tells you that you have successfully updated the work order and then have a choice of returning to the main menu, or exiting out of the program.... Why I say this is that I don't need the page to reload once you hit submit, I just need the data to make it to the database. I am not sure if that makes a difference as far as what you were saying.

If I understand what you are saying (and I am not sure I do), I need to create the hidden element and then I should have some success as the page will be able to complete the process of going through it from top to bottom.....Am I even close? I really didn't think I was trying to create something that would be so complicated, until I got into it, and then it was too late to back out as my curiousity was getting the better of me. This probably wasn't the best project to start with, as far as learning PHP and MySQL......I sure appreciate all the help I have received from you here ath the Coding Forum!!:)


08-13-2006, 11:19 PM
Why I say this is that I don't need the page to reload once you hit submit, I just need the data to make it to the database.

That is entirely feasible. You don't _have_ to reload the same page when you submit, but that is a common practice. You can redirect via the "action" parameter of your form to any page you like. It sounds like you want your submit button to go to a page that does nothing but update the database and then display a "success" message, and then maybe a link back to the starting page, or even automatically redirect to the starting page after a couple of seconds (or immediately). Any of those options is possible!

I don't know the ins and outs of your entire process. I recommend you draw up a flow diagram that follows your process so you can make some sense of what's happening. That's probably the best advice I can give you at this point... now that you know what can be done, you need to back away a little bit and see if you can draw up a design that works for you and the project.

08-13-2006, 11:27 PM
That's probably the best advice I can give you at this point... now that you know what can be done, you need to back away a little bit and see if you can draw up a design that works for you and the project.

The only thing I am not understanding is what I will need to do to actually get the database to update, If it is going back to the beginning of the page, that is going to cause a problem, so do I need to find a command of some sort that will stop that from happening, or do I need to move the update to the page I redirect it to when you hit submit?


08-13-2006, 11:28 PM
I was going to mention that this whole process isn't as complicated as it may sound.... it really is quite a basic and straightworward process

08-14-2006, 04:33 AM
I would submit to another page that does the update and then automatically redirects to the start.

Len Whistler
08-14-2006, 10:52 PM
I would pass the $_POST to $_SESSION and use 3 pages.

* Page 1 with form is posted to Page 2 as a preview of data entered.
* If data is Ok Submit to Database with Page 3 - If not submit back to Page 1 for editing.
* $_SESSION will send the form data back for editing so only typos have to be corrected instead of starting over. If no editing is required $_SESSION will send the data to the database.

08-15-2006, 01:41 PM
I have been looking over this page for hours (well days) and I have a feeling that the problem is occurring because I have had to comment out the following line. If I don't comment it out, I get the successful page updated comment as soon as I key in the work order number from the first page.

// GEWOManager.php
//if (!isset($_POST['Submit']))

Because I have commented this line out, I also have to comment out this line. Between the two, I am wondering if that is what is causing the page to run properly, but not actually update the database.

// else
$Purchase = $_POST["Purchase"];
$Comment = $_POST["Comment"];

mysql_query ("UPDATE `workorder` set Purchase = '$Purchase', Comment = '$Comment' WHERE work = '$Work'")
or die(mysql_error());
echo "This Work Order has been Updated Successfully";

A couple of notes... I had to change the query above because of a syntax error. Now the error is gone.

I an not getting any errors, so the pages are functioning properly, just something missing to actually update the database. When you run this, everything goes through, the comments and purchase order I have inputted disappear BUT the updated succesful message DOES NOT appear.

08-15-2006, 04:27 PM
if (!isset($_POST['Submit']))

It sounds like you're commenting that line of code out without understanding what it does. If the function of that if statement is foggy to you, I suggest you go through this short tutorial here (http://www.w3schools.com/php/php_post.asp).

08-15-2006, 04:48 PM
Thanks for the link....it makes more sense now.....

At the risk of sounding foolish....can you explain, or tell me where to find the info (looked at w3 with no luck)..... what does the isset() function do, and is there a laymans explaination for it... I keep reading it, but don't really understand it, and have a feelign it could be the missing piece of my puzzle.


08-15-2006, 07:44 PM
isset() basically checks to see if a variable is "set" (i.e. does it exist?)

If you evaluate isset() on $_POST['submit'] you are basically checking to see if the post variable for the submit button has been clicked. When you load the page initially, the post variables are processed. When you click the submit button, it submits the values back to the page.

Try this script out for an example.

if (isset($test))
echo "the variable is set";
echo "the variable is not set";

$test="something... ANYTHING!";

if (isset($test))
echo "the variable is set";
echo "the variable is not set";

08-16-2006, 07:14 PM
Thanks for the clarification.... I am still having a bit of trouble grasping how this all works, so back to a tutorial I shall go......

can you tell me what the ! means when in front of the isset()



08-16-2006, 08:11 PM
"!" means "is this false?"; "!=" means "is this not equal?"

if (!isset($_POST['submit'])) is saying "Is the variable $_POST['submit'] not set? (does it not exist?)"

if ($a != $b) is saying "Is $a not equal to $b?"