kel Posted May 27, 2009 Share Posted May 27, 2009 Hi Guys I'm fairly new to php and haven't quite grasped all of the concepts 100% yet, but have a working script - well mostly. I am having problems with the following Page is meant to display data relating to various equipment in a form and allow me to create a service record for whichever item. The problem is that on the first parse my form displays the relevant data, but on second parse (when the add button is clicked) the only fields in the form that are still populated are asset, next service date and service date. Could anyone help me to find a way to keep the other info such as serial, make, model etc (not inc. comment/username) in the form after the 'add' action has been performed? Everything else on the page seems to work fine. Thanks in advance Here is my code: 1 <Html> 2 3 <Head><B><font size = "18">Printer Servicing</font></Head> 4 <H2>This page is for viewing existing and creating new service records</H2> 5 <Body> 6 <A HREF="maint_index.php">Index page<BR><BR></A> 7 8 <?php $db_link = mysql_connect('localhost', '****', '****');?> 9 <?php if (!$db_link) 10 { 11 die('Could not connect: ' . mysql_error()); 12 } 13 14 $db_selected = mysql_select_db('maint_db', $db_link); // selects the relevant database --> 15 16 17 $date=date("Y-m-d"); 18 19 $asset=$_REQUEST["asset_num"]; 20 $frequency = 90; 21 22 $sched_date=$date; 23 24 if ($_POST['action'] == "add") 25 { 26 $sched_date=date('Y-m-d',(strtotime($date) + $frequency*24*3600)); 27 $sql=("INSERT INTO service(asset_tag, serv_date, comment, username) 28 VALUES($asset, '$sched_date', '$_POST[comment]', '$_POST[username]')"); 29 30 if (!mysql_query($sql,$db_link)) 31 { 32 die('Error: ' . mysql_error()); 33 } 34 35 $sched_upd=("UPDATE item SET sched_date='$sched_date' WHERE asset_tag=$asset"); 36 mysql_query($sched_upd,$db_link); 37 38 echo "1 x service record has been added"; 39 } 40 else 41 { 42 // need to find a way to make the screen refresh - keeping details in form 43 44 $query=mysql_query("SELECT * FROM area a, item i, make m, model mo, workstation w, make_model mm 45 WHERE i.mm_id=mm.mm_id AND mm.model_id=mo.model_id AND a.area_id=w.area_id AND i.ws_id=w.ws_id AND m.make_id=mm.make_id AND i.asset_tag='$asset'") ; 46 47 $row=mysql_fetch_array($query); 48 49 $sched_date=$row['sched_date']; 50 print "sched_date=$sched_date"."<BR>"; 51 $frequency = 90; 52 53 $serial=$row['serial']; 54 } 55 56 ?> 57 58 <FORM ACTION="maint_serv.php" METHOD="POST"> <! this(action bit) says which page(or script) is going to receive data from the form --> 59 60 <INPUT TYPE="HIDDEN" NAME="action" VALUE="add" > 61 <INPUT TYPE="HIDDEN" NAME="asset_num" VALUE="<?php echo $asset;?>"> 62 63 64 <!Populates the table with asset specific values --> 65 <TABLE BORDER=1><TR BGCOLOR="RED"></TR> 66 <TD BGCOLOR="RED"><B>Asset:</B></TD> 67 <TD><?php echo "$asset" ?></TD> 68 <TD BGCOLOR="RED"><B>Area:</B></TD> 69 <TD><?php echo $row['descript']; ?></TD> 70 </TR> 71 <TR> 72 <TD BGCOLOR="RED"><B>Serial:</B></TD> 73 <TD><?php echo $row['serial']; ?></TD> 74 <TD BGCOLOR="RED"><B>Workstation:</B></TD> 75 <TD><?php echo $row['description']; ?></TD> 76 </TR> 77 <TR> 78 <TD BGCOLOR="RED"><B>Printer Name:</B></TD> 79 <TD><?php echo $row['prt_name']; ?></TD> 80 </TR> 81 <TR> 82 <TD BGCOLOR="RED"><B>Make:</B></TD> 83 <TD><?php echo $row['manufacturer']; ?></TD> 84 <TD BGCOLOR="RED"><B>Model:</B></TD> 85 <TD><?php echo $row['model']; ?></TD> 86 </TR> 87 <TR><TD></TD></TR> 88 <TR> 89 <TD BGCOLOR="RED"><B>Next Scheduled Service:</B></TD> 90 <TD><?php echo $sched_date; ?></TD> 91 <TD BGCOLOR="RED"><B>Service Date:</B></TD> 92 <TD><?php echo $date ?><BR></TD> 93 </TR> 94 <TR> 95 <TD BGCOLOR="RED"><B>Comment:</B><BR> (max 255 char)</TD> 96 <TD><TEXTAREA NAME='comment' ROWS=5 COLS=40></TEXTAREA><BR></TD> 97 </TR> 98 <TD BGCOLOR="RED"><B>User:</B></TD> 99 <TD><INPUT TYPE=TEXT NAME='username' SIZE=10><BR></TD> 100 </TR> 101 </TABLE> 102 <BR> 103 104 <INPUT TYPE=SUBMIT NAME="ADD" VALUE="ADD"> 105 <INPUT TYPE=RESET NAME="RESET" VALUE="RESET"> 106 107 </FORM> 108 109 <?php $db_selected = mysql_select_db('maint_db', $db_link); ?> <! selects the relevant database --> 110 <! query --> 111 <?php 112 113 $result = mysql_query("SELECT sv.serv_date, sv.comment, sv.username FROM service sv WHERE sv.asset_tag='$asset'"); 114 // Start table 115 print ("<TABLE BORDER=1>\n"); 116 print ("<TH>SERVICE DATE</TH><TH>COMMENT</TH><TH>SERVICED BY</TH>"); 117 118 while ($row_array = mysql_fetch_array($result)) 119 { 120 $serv_date = $row_array[0]; 121 $comment = $row_array[1]; 122 $Username = $row_array[2]; 123 124 print ("<TR ALIGN=LEFT VALIGN=TOP>"); 125 print ("<TD>$serv_date</TD>"); 126 print ("<TD>$comment</TD>"); 127 print ("<TD>$Username</TD>"); 128 129 print "</TR>"; 130 } 131 132 ?> 133 </TABLE> 134 135 </Body> 136 137 </Html> Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 27, 2009 Share Posted May 27, 2009 is this the "maint_serv.php" page? Or does this page submit to "maint_serv.php"? Quote Link to comment Share on other sites More sharing options...
kel Posted May 27, 2009 Author Share Posted May 27, 2009 Hi maint_serv is the page containing the code I posted - basically the page is passing data back to itself Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 27, 2009 Share Posted May 27, 2009 Ok... So, data is pulled from the DB, shown on the page. After you fill out the information that you can enter, you submit the page. On submission, it essentially reloads itself, but not the DB information. Correct? Quote Link to comment Share on other sites More sharing options...
kel Posted May 27, 2009 Author Share Posted May 27, 2009 I guess so. Initially I am querying the DB and sticking that info into the form, then on action (e.g. clicking button) any user entered data is sent to the database Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 27, 2009 Share Posted May 27, 2009 I need to make sure i ask all the questions i have the first time x.x Is it not loading the existing information, or the information you are adding, or anything at all? Quote Link to comment Share on other sites More sharing options...
kel Posted May 27, 2009 Author Share Posted May 27, 2009 Ok Basically, this maint_serv page is being called by another page - the asset value is being passed via GET. When the page loads it has relevant data for the piece of equipment loaded into the form - via a query. It also should list any previous service records (if any exist) for the equipment below the form. When the user enters some data (comments/username), then clicks add, the relevant data should be sent to the database. All of the previously filled in fields should still hold they're previous values e.g. asset, serial etc. but as soon as add is selected, the script updates the database and blanks out the data that was initially passed to the form by my original query. I know that I should somehow use the POST statement to either keep the fields filled in or repopulate them, but I'm not sure how. Thanks Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 27, 2009 Share Posted May 27, 2009 Here is my thoughts: I think the GET is being cleared by the post... I assume that the GET statement is reflected in the URL of the page, and if it is, the "simple" fix (if it works) is the assign the GET statement to a variable, and call the when you reload the page via the form. I.E. $asset = $_REQUEST["asset_num"]; ... <FORM ACTION="maint_serv.php?=<?php echo $asset; ?>" METHOD="POST"> I'm not sure if that will work for you, but give it a try, and let me know... I have another idea, but it isn't necessary if this works... Quote Link to comment Share on other sites More sharing options...
kel Posted May 27, 2009 Author Share Posted May 27, 2009 Ok, thanks I'll have to look into it in about an hour - I'm stuck in work and have an IT conference call to join in on, Quote Link to comment Share on other sites More sharing options...
kel Posted May 27, 2009 Author Share Posted May 27, 2009 So, do you mean that I should add another GET statement and that should repopulate the table? The only problem is that the GET only holds the 'asset' value and so a query would probably be needed to request the other information - this would be doubling up on code I think Quote Link to comment Share on other sites More sharing options...
jsschmitt Posted May 27, 2009 Share Posted May 27, 2009 Here is what I think is happening: Page loads and GET is completed, filling table based on the GET statement. Page is submitted, placing a POST statement, and killing the GET statement. Page loads after the POST statement is done, but since GET is killed, nothing is loaded. Does your url reflect the GET statement? i.e. maint_serv.php?=monkey Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 27, 2009 Share Posted May 27, 2009 he not using get is he, he using both REQUEST['varable'] use $GET singly, to use post after $GET[] Quote Link to comment Share on other sites More sharing options...
kel Posted May 27, 2009 Author Share Posted May 27, 2009 Ok, I've put these lines inside the POST and the code now does what I wanted. 44 $query=mysql_query("SELECT * FROM area a, item i, make m, model mo, workstation w, make_model mm 45 WHERE i.mm_id=mm.mm_id AND mm.model_id=mo.model_id AND a.area_id=w.area_id AND i.ws_id=w.ws_id AND m.make_id=mm.make_id AND i.asset_tag='$asset'") ; 46 47 $row=mysql_fetch_array($query); It's a bit messy I guess to put the code in more than once, but it seems to do the job Thanks for the help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.