Jump to content

form elements not displayed


ZachFlem

Recommended Posts

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.