ZachFlem Posted March 16, 2014 Share Posted March 16, 2014 this page is used to display, add, edit and delete records from a table called 'valid_locations' so far, I can display the contents of the table (2 colums, 'id' and 'location') and the edit/delete button associated with each record. but when I click the edit button, the page displays the "confirm changes" button but no form elements to edit (from the section "//edit form" I'm stuck at this point and don't know where or how to go about moving forward. Cheers Zach <?php $pagetitle="Locations"; $menu="yes"; require 'header.php'; require 'dbvars.php'; require 'dafunc.php'; //set the referrer variable $self=htmlentities($_SERVER['PHP_SELF']); // Make a MySQL Connection $con=mysqli_connect($sqlhost,$sqluser,$sqlpass,$sqldb); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //edit form if(isset($_POST['edit'])) { $id=$_POST['id']; $result = mysqli_query($con,"SELECT * FROM valid_locations WHERE id='$id'"); echo "$id"; if($result === false){ throw new Exception(mysqli_error($con)); } echo "<form action=\"$self\" method=\"post\">"; echo "<table>"; while($row = mysqli_fetch_array($result)) { echo "<tr><td>ID</td><td>" . $row['id'] . "</td></tr>"; echo "<input name=\"id\" type=\"hidden\" value=\"" . $row['id'] ."\" />"; echo "<tr><td>Location</td><td><input name=\"location\" type=\"text\" value=\"". $row['location'] ."\"/></td></tr>"; } echo "</table>"; echo "<input type=\"submit\" name=\"confirm\" id=\"confirm\" value=\"Confirm Changes\" />"; echo "</form>"; } //write the changes to the DB if(isset($_POST['confirm'])) { echo "write the changes to the database!"; } //delete a record if(isset($_POST['delete'])) { echo "delete the record!"; } //display the table containing the valid locations $result = mysqli_query($con,"SELECT * FROM valid_locations"); echo " <form action=\"$self\" method=\"post\"> <table> <tr><th>ID</th><th>Location</th></tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<input type=\"hidden\" name=\"id\" id=\"" . $row['id'] . "\">"; echo "<td>" . $row['location'] . "</td>"; echo "<input type=\"hidden\" name=\"location\" id=\"" . $row['location'] . "\">"; echo "<td><input type=\"submit\" name=\"edit\" id=\"edit\" value=\"Edit\" /></td>"; echo "<td><input type=\"submit\" name=\"delete\" id=\"delete\" value=\"Delete\" /></td>"; echo "</tr>"; } echo "</table>"; echo "</form>"; mysqli_close($con); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/287009-form-elements-not-displayed/ Share on other sites More sharing options...
ginerjm Posted March 16, 2014 Share Posted March 16, 2014 I"m guessing that SINCE you don't check the results of the valid_locations query that it has not run, therefor your while loop doesn't run. Quote Link to comment https://forums.phpfreaks.com/topic/287009-form-elements-not-displayed/#findComment-1472772 Share on other sites More sharing options...
ZachFlem Posted March 17, 2014 Author Share Posted March 17, 2014 I"m guessing that SINCE you don't check the results of the valid_locations query that it has not run, therefor your while loop doesn't run. Thanks for alerting me to the fact that I can even do that! I've ammended the code but I can't see why the $id variable isn't being passed on from the form. (couldn't find anywhere to edit the original post, so posting here) <?php $pagetitle="Locations"; $menu="yes"; require 'header.php'; require 'dbvars.php'; require 'dafunc.php'; //set the referrer variable $self=htmlentities($_SERVER['PHP_SELF']); // Make a MySQL Connection $con=mysqli_connect($sqlhost,$sqluser,$sqlpass,$sqldb); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //edit form if(isset($_POST['edit'])) { $editid=$_POST['id']; echo "DEBUG: $id<br />"; $result = mysqli_query($con,"SELECT * FROM valid_locations WHERE id='$id'"); if($result === false){ throw new Exception(mysqli_error($con)); } IF (mysqli_num_rows($result)==0) { echo"Query Failed!"; } echo "<form action=\"$self\" method=\"post\">"; echo "<table>"; while($row = mysqli_fetch_array($result)) { echo "<tr><td>ID</td><td>" . $row['id'] . "</td></tr>"; echo "<input name=\"id\" type=\"hidden\" value=\"" . $row['id'] ."\" />"; echo "<tr><td>Location</td><td><input name=\"location\" type=\"text\" value=\"". $row['location'] ."\"/></td></tr>"; } echo "</table>"; echo "<input type=\"submit\" name=\"confirm\" id=\"confirm\" value=\"Confirm Changes\" />"; echo "</form>"; } //write the changes to the DB if(isset($_POST['confirm'])) { echo "write the changes to the database!"; } //delete a record if(isset($_POST['delete'])) { echo "delete the record!"; } //display the table containing the valid locations $result = mysqli_query($con,"SELECT * FROM valid_locations"); IF (mysqli_num_rows($result)==0) { echo"Query Failed!"; } echo " <form action=\"$self\" method=\"post\"> <table> <tr><th>ID</th><th>Location</th></tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<input type=\"hidden\" name=\"id\" id=\"" . $row['id'] . "\">"; echo "<td>" . $row['location'] . "</td>"; echo "<input type=\"hidden\" name=\"location\" id=\"" . $row['location'] . "\">"; echo "<td><input type=\"submit\" name=\"edit\" id=\"edit\" value=\"Edit\" /></td>"; echo "<td><input type=\"submit\" name=\"delete\" id=\"delete\" value=\"Delete\" /></td>"; echo "</tr>"; } echo "</table>"; echo "</form>"; mysqli_close($con); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/287009-form-elements-not-displayed/#findComment-1472861 Share on other sites More sharing options...
Ch0cu3r Posted March 17, 2014 Share Posted March 17, 2014 (edited) In the second while loop for displaying all valid_locations returned from the query you need to output a new form for each record. echo " <table> <tr><th>ID</th><th>Location</th></tr>"; while($row = mysqli_fetch_array($result)) { echo "<form action=\"$self\" method=\"post\">"; // create new form for each record echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<input type=\"hidden\" name=\"id\" id=\"" . $row['id'] . "\">"; echo "<td>" . $row['location'] . "</td>"; echo "<input type=\"hidden\" name=\"location\" id=\"" . $row['location'] . "\">"; echo "<td><input type=\"submit\" name=\"edit\" id=\"edit\" value=\"Edit\" /></td>"; echo "<td><input type=\"submit\" name=\"delete\" id=\"delete\" value=\"Delete\" /></td>"; echo "</tr>"; echo "</form>"; // close the form } echo "</table>"; Edited March 17, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/287009-form-elements-not-displayed/#findComment-1472870 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.