tellivision Posted July 11, 2007 Share Posted July 11, 2007 Hello everyone, I've basically created a form to edit records in a database. The problem is that when it updates the record, all but the id number are all wiped when the submit button is clicked on. I've pasted the code below - can anyone see where my error is; I'm new to creating forms to edit database entries so it's probably something really obvious to the rest of you. Thanks in advance. Code: if ($_POST) { foreach($_POST as $k => $v) { $v = trim($v); $$k = $v; } // build UPDATE query $update = "UPDATE webbietask SET taskid='$id', task='$task', datelog='$datelog', assign='$initialassign', defassign='$defassign', estdate='$estdate', completed='$compli' WHERE taskid=$id"; // execute query and check for success if (!mysqli_query($cxn, $update)) { $msg = "Error updating data"; } else { $msg = "Record successfully updated:"; // write table row confirming data $table_row = <<<EOR <tr> <td>$task</td> <td>$datelog</td> <td>$initialassign</td> <td>$defassign</td> <td>$estdate</td> <td>$compli</td> </tr> EOR; } // if not posted, check that an Id has been passed via the URL } else { if (!IsSet($_GET['id'])) { $msg = "No record selected!"; } else { $id = $_GET['id']; // build and execute the query $select = "SELECT taskid, task, datelog, assign, defassign, estdate, completed FROM webbietask WHERE taskid=$id"; $result = mysqli_query($cxn, $select); // check that the record exists if (mysqli_num_rows($result)<1) { $msg = "No task with that ID has been found"; } else { // set variables for form code $form_start = "<FORM METHOD=\"post\"ACTION=\"". $_SERVER['PHP_SELF'] . "\">"; $form_end = <<<EOF <tr> <td colspan="7"><input type="submit" value="Submit changes" /></td> </tr> <tr> <td colspan="7"><input type="reset" value="Cancel" /></td> </tr> </form> EOF; //assign the results to an array while ($row = mysqli_fetch_array($result)) { $id = $row ['taskid']; $task = $row ['task']; $datelog = $row ['datelog']; $initialassign = $row ['assign']; $defassign = $row ['defassign']; $estdate = $row ['estdate']; $compl = $row ['completed']; //write table row with form fields $table_row = <<<EOR <tr> <td><input type='text' name='Task ID' value='$id' /></td> <td><input type='text' name='Task Details' value='$task' /></td> <td><input type='text' name='Date Entered' value='$datelog' /></td> <td><input type='text' name='Initially Assigned To:' value='$initialassign' /></td> <td><input type='text' name='Task Confirmed By:' value='$defassign' /></td> <td><input type='text' name='Est. Date Completion' value='$estdate' /></td> <td><input type='text' name='Completed?' value='$compl' /></td> </tr> EOR; } //end 'if records exists' if } // end 'if ID given in URL' if } // end 'if form posted' if } // close connection mysqli_close($cxn); //print error/success message echo (IsSet($msg)) ? "<div class=\'error\'>$msg</div>" : ""; ?> <table align="center" cellpadding="0" cellspacing="1" width="98%"> <tr> <th class="colhdg" colspan=7><b>Edit Tasks</b></th> </tr> <? echo (IsSet($form_start)) ? $form_start : ""; ?> <input type="hidden" name="id" value="<? echo $id ?>" /> <tr> <th class="colhdg" width="10%">Task ID</th> <th class="colhdg" width="30%">Task Details</th> <th class="colhdg" width="10%">Date Entered</th> <th class="colhdg" width="15%">Initially Assigned To</th> <th class="colhdg" width="15%">Task confirmed by</th> <th class="colhdg" width="10%">Est. Date Completion</th> <th class="colhdg" width="10%">Completed?</th> </tr> <?echo (IsSet($table_row))?$table_row : ""; ?> <?echo (IsSet($form_end))? $form_end : ""; ?> </table> Quote Link to comment Share on other sites More sharing options...
marcus Posted July 11, 2007 Share Posted July 11, 2007 The variables aren't defined when you're updating the table. They're defined when you call the form itself. Do print_r($_POST) and tell us what you get. Quote Link to comment Share on other sites More sharing options...
tellivision Posted July 11, 2007 Author Share Posted July 11, 2007 Where would I be putting print_r($_POST) in the code? Quote Link to comment Share on other sites More sharing options...
marcus Posted July 11, 2007 Share Posted July 11, 2007 When you pass the data by clicking the submit button. Quote Link to comment Share on other sites More sharing options...
tellivision Posted July 11, 2007 Author Share Posted July 11, 2007 Do you mean, like this?: $form_start = "<FORM METHOD=\"post\"ACTION=\"". $_SERVER['PHP_SELF'] . "\">"; $form_end = <<<EOF <tr> <td colspan="7"><input type="submit" value="Submit changes" /></td> </tr> <tr> <td colspan="7"><input type="reset" value="Cancel" /></td> </tr> </form> EOF; print_r($_POST) Quote Link to comment Share on other sites More sharing options...
marcus Posted July 11, 2007 Share Posted July 11, 2007 After: if ($_POST) Quote Link to comment Share on other sites More sharing options...
tellivision Posted July 11, 2007 Author Share Posted July 11, 2007 I'm not getting anything different, it's still wiping everything . Quote Link to comment Share on other sites More sharing options...
marcus Posted July 11, 2007 Share Posted July 11, 2007 Try removing the spaces from the variables $row ['taskid'] //to $row['taskid'] Quote Link to comment Share on other sites More sharing options...
tellivision Posted July 11, 2007 Author Share Posted July 11, 2007 Ok, if I put it straight after if($POST) then I get: Parse error: syntax error, unexpected '{' Quote Link to comment Share on other sites More sharing options...
marcus Posted July 11, 2007 Share Posted July 11, 2007 I thought you would have caught on to putting it after you called the if statement... if($_POST){ print_r($_POST); } Quote Link to comment Share on other sites More sharing options...
tellivision Posted July 11, 2007 Author Share Posted July 11, 2007 Try removing the spaces from the variables $row ['taskid'] //to $row['taskid'] Now even the taskid is being wiped Quote Link to comment Share on other sites More sharing options...
tellivision Posted July 12, 2007 Author Share Posted July 12, 2007 Ok, I've checked through the code in the book I've been using as reference to write this script and I can't see that there is any error that I've made (although obviously there is one floating about otherwise it'd work ). Anyways, what basically happens is that the script does draw up the selected record with all its information but when the information is resubmitted (i.e. the submit changes button is pressed) all the information in that record is wiped away, leaving a blank record so I'm assuming it's the resubmitting that's the problem. With that in mind, can anyone else see the error? Sorry for being a bit hopeless with this, any reply to this may have to explain exactly where the changes need to be as I'm still finding my feet with php. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
tellivision Posted July 12, 2007 Author Share Posted July 12, 2007 I thought you would have caught on to putting it after you called the if statement... if($_POST){ print_r($_POST); } *sigh* As I said, I'm still getting used to php and I haven't written a form of this type before so please excuse me if I'm making mistakes that seem silly to you - it's all part of the learning process. Now, I'm am assuming (perhaps wrongly) that I put the print_r($_POST); right after the end of the initial if($_POST) statement because otherwise I get an error message about one of the lines which happens to include and ELSE statement. In putting the print_r($_POST); at that point, it comes up with Array (...) - inside the brackets is the data that have been entered. However, the table underneath which is supposed to show the updated record comes up blank and when I return to the page with the table of current records, the record is also blank there so I can assume that while the script is picking up the data then it's simply not storing it. Any ideas? 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.