Link to home
Start Free TrialLog in
Avatar of valicon
valiconFlag for United States of America

asked on

Auto populate form via PHP from MySQL

I have a script that functions correctly, I am trying to have the following form auto populate when a user edits a record, here is the code:

if($_GET['action'] == "edit"){

      echo "
      <form name=\"form1\" method=\"POST\" action=\"editdelete.php?action=update\">
      <table><tr>
      <td align=center bgcolor=#EBEBEB>Last Name</td><td><input type=\"text\" name=\"strLastName\" value=\"$ts\"></td></tr>
I added the above --> value=\"$ts\"></td></tr> at the end but it does not auto populate.

   <tr><td align=center bgcolor=#EBEBEB>First Name</td><td><input type=\"text\" name=\"strFirstName\"></td></tr>
   <tr><td align=center bgcolor=#EBEBEB>MI</td><td><input type=\"text\" name=\"strMiddle_Initial\"></td></tr>
   <tr><td align=center bgcolor=#EBEBEB>DOB</td><td><input type=\"text\" name=\"intDOB\"></td></tr>
   <tr><td align=center bgcolor=#EBEBEB>Race</td><td><input type=\"text\" name=\"strRace\"></td></tr>
   <tr><td align=center bgcolor=#EBEBEB>Sex</td><td><input type=\"text\" name=\"strSex\"></td></tr>
   <tr><td align=center bgcolor=#EBEBEB>Address</td><td><input type=\"text\" name=\"strAddress\"></td></tr>

What syntax is required to make it function correctly?  Thanks.
Avatar of AlexanderR
AlexanderR
Flag of Canada image

$defaults = array("LastName"=>"", "FirstName"=>"", "Middle_Initial"=>"","DOB"=>"","Race"=>"","Sex"=>"","Address"=>"");
if($_GET['action']=="edit") {
   $sqlSelectUser = "SELECT LastName, FirstName, Middle_Initial, DOB, Race, Sex, Address FROM users WHERE user_id = ".$_GET["user_id"];
/* $_GET["user_id"] is to be taken from the link where user click on their name to edit it.  So that link would like look <a href="editpage.php?user_id=xx">
   $rsSelectUser = $mysqli_query($db, $sqlSelectUser);
   while ($rwUser = mysqli_fetch_array($rsSelectUser) {
       $defaults["LastName"]=$rwUser["LastName"];
       $defaults["FirstName"]=$rwUser["FirstName"];
       $defaults["Middle_Initial"]=$rwUser["Middle_Initial"];
       $defaults["DOB"] = $rwUser["DOB"];
       $defaults["Race"]=$rwUser["Race"];
       $defaults["Sex"]=$rwUser["Sex"];
       $defaults["Address"]=$rwUser["address"]
   }
}
 echo "
      <form name=\"form1\" method=\"POST\" action=\"editdelete.php?action=update\">
      <table><tr>
      <td align=center bgcolor=#EBEBEB>Last Name</td><td><input type=\"text\" name=\"strLastName\" value=".$defaults["LastName"]."></td></tr>
I added the above --> value=\"$ts\"></td></tr> at the end but it does not auto populate.

   <tr><td align=center bgcolor=#EBEBEB>First Name</td><td><input type=\"text\" name=\"strFirstName\" value=".$defaults["FirstName"]."></td></tr>
   <tr><td align=center bgcolor=#EBEBEB>MI</td><td><input type=\"text\" name=\"strMiddle_Initial\" value=".$defaults["Middle_Initial"]."></td></tr>
   <tr><td align=center bgcolor=#EBEBEB>DOB</td><td><input type=\"text\" name=\"intDOB\" value=".$defaults["DOB"]."></td></tr>
   <tr><td align=center bgcolor=#EBEBEB>Race</td><td><input type=\"text\" name=\"strRace\" value=".$defaults["Race"]."></td></tr>
   <tr><td align=center bgcolor=#EBEBEB>Sex</td><td><input type=\"text\" name=\"strSex\" value=".$defaults["Sex"]."></td></tr>
   <tr><td align=center bgcolor=#EBEBEB>Address</td><td><input type=\"text\" name=\"strAddress\" value=".$defaults["Address"]."></td></tr>
oh yeah dont forget at the end

<input type="hidden" name="user_id" value=\"".$_GET["user_id"]."\"></form>

And i made a mistake.  Wherever it says something like value=".$defaults["FirstName"]." it must say
value=\"".$defaults["FirstName"]."\"

Thats why single quotes are generally better to use, they dont interfear with html double quotes.
Avatar of valicon

ASKER

I was hoping not to add a bunch of code to the original script. Isn't there a way that is shorter, code wise?
No.  You need to get the users details. For that you need to query the database.  Then you got to capture the details (db output) and put them into the form. You also need to do it in such a way that the script does not through errors if it is used as a regular insert. Hence the defaults array.  All these steps require many operations.
Or is the user information to be filled out stored elsewhere?
Avatar of valicon

ASKER

Here is the code in it's entirety, the user info is getting grabbed by the variables $ts, etc:



<?
include("search.php3");
include('db_login.php');

$connection = mysql_connect($db_host, $db_username, $db_password);

if (!$connection){
     die ("Could not connect to the database: <br />". mysql_error());
}

$selected = mysql_select_db("customer",$connection) or die("Could not select database");


//execute my SQL query and return records
$result = mysql_query("SELECT * FROM tblcustomer");

if ($result &&!$_GET['action']) {

   echo "<table width=90% align=center border=1><tr>
   <td align=center bgcolor=#EBEBEB>Last Name</td>
   <td align=center bgcolor=#EBEBEB>First Name</td>
   <td align=center bgcolor=#EBEBEB>MI</td>
   <td align=center bgcolor=#EBEBEB>DOB</td>
   <td align=center bgcolor=#EBEBEB>Race</td>
   <td align=center bgcolor=#EBEBEB>Sex</td>
   <td align=center bgcolor=#EBEBEB>Address</td>
   <td align=center bgcolor=#EBEBEB>Case Number</td>
   <td align=center bgcolor=#EBEBEB>Date</td>
   <td align=center bgcolor=#EBEBEB>Length</td>
   <td align=center bgcolor=#EBEBEB>office</td>
    <td align=center bgcolor=#EBEBEB>Edit</td>
     <td align=center bgcolor=#EBEBEB>Delete</td>
   </tr>";



//grab the data from the database
while ($r =  mysql_fetch_array($result)) {
 $my_id = $r["id"];
 $ts = $r["strLastName"];
 $fname = $r["strFirstName"];
 $minit = $r["strMiddle_Initial"];
 $address = $r["strAddress"];
 $race = $r["strRace"];
 $dob = $r["intDOB"];
 $sx = $r["strSex"];
 $csnum = $r["intCaseNumber"];
 $date = $r["intDate"];
 $len = $r["strLength"];
 $officer = $r["strofficename"];


    echo "<tr>
    <td>$ts</td>
    <td>$fname</td>
    <td>$minit</td>
    <td>$address</td>
    <td>$race</td>
    <td>$dob</td>
    <td>$sx</td>
    <td>$csnum</td>
    <td>$date</td>
    <td>$len</td>
    <td>$officename</td>
    <td><a href=\"editdelete.php?action=edit&id=$my_id\">Edit</a></td>
    <td><a href=\"editdelete.php?action=delete&id=$my_id\" onclick=\"return confirm('Are you sure you want to delete this record?')\">Delete</a></td></tr>";

}

//echo "</table>";
}

   
   
   }
   
   if($_GET['action'] == "edit"){
   
         echo "
         <form name=\"form1\" method=\"POST\" action=\"editdelete.php?action=update\">
         <table><tr>
         <td align=center bgcolor=#EBEBEB>Last Name</td><td><input type=\"text\" name=\"strLastName\" value=\"$ts\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>First Name</td><td><input type=\"text\" name=\"strFirstName\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>MI</td><td><input type=\"text\" name=\"strMiddle_Initial\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>DOB</td><td><input type=\"text\" name=\"intDOB\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Race</td><td><input type=\"text\" name=\"strRace\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Sex</td><td><input type=\"text\" name=\"strSex\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Address</td><td><input type=\"text\" name=\"strAddress\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Case Number</td><td><input type=\"text\" name=\"intCaseNumber\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Date</td><td><input type=\"text\" name=\"intDate\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Length</td><td><input type=\"text\" name=\"strLength\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>office</td><td><input type=\"text\" name=\"strofficename\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB></td><td><input type=\"hidden\" name=\"my_id\" value=\"$_GET[id]\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Update</td><td><input type=\"submit\" name=\"submit\"></td>
      </tr></table</form>
         ";
   
   }
   
   if($_GET['action'] == "delete") {
         $deletesql = "delete from tblTrespass where id = $_GET[id]";
         $resultdelete = mysql_query($deletesql);
         echo "Record Deleted<br>";
         echo '<a href="editdelete.php">Return to Admin page</a><br />';
         //echo $deletesql;
   
   }
   
   if ($_GET['action'] == "update") {
      $updatesql = "update tblTrespass set strLastName = \"$_POST[strLastName]\", strFirstName = \"$_POST[strFirstName]\", strMiddle_Initial=\"$_POST[strMiddle_Initial]\", intDOB = \"$_POST[intDOB]\", strRace = \"$_POST[strRace]\", strSex = \"$_POST[strSex]\", strAddress = \"$_POST[strAddress]\", intCaseNumber = \"$_POST[intCaseNumber]\", intDate = \"$_POST[intDate]\", strLength = \"$_POST[strLength]\", strofficename = \"$_POST[strofficename]\" where id = $_POST[my_id]";
         $resultupdate = mysql_query($updatesql);
         echo "Record Updated<br>";
         echo '<a href="editdelete.php">Return to Admin page</a><br />';
        // echo $updatesql;
   }
   
   //close the connection
   mysql_close($connection);
   ?>


If it's working ok, I'd do it this way:

if($_GET['action'] == "edit"){ ?>
       <form name="form1" method="POST" action="editdelete.php?action=update">
         <table><tr>
         <td align=center bgcolor=#EBEBEB>Last Name</td><td><input type="text" name="strLastName" value=" <? echo($ts); ?> "></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>First Name</td><td><input type="text" name="strFirstName"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>MI</td><td><input type="text" name="strMiddle_Initial"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>DOB</td><td><input type="text" name="intDOB"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Race</td><td><input type="text" name="strRace"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Sex</td><td><input type="text" name="strSex"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Address</td><td><input type="text" name="strAddress"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Case Number</td><td><input type="text" name="intCaseNumber"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Date</td><td><input type="text" name="intDate"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Length</td><td><input type="text" name="strLength"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>office</td><td><input type="text" name="strofficename"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB></td><td><input type="hidden" name="my_id" value=" <? echo($_GET[id]); ?>"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Update</td><td><input type="submit" name="submit"></td>
      </tr></table</form>
     <?
   }

You just need to add more value=" <? echo($VARIABLE); ?>" wherever you need to auto populate.

Good luck.

-V
OK, thanks for that.
Yes, with that code there is nothing to add.  It does the same thing i did as it is.

One thing i dont agree/understand is the query "SELECT * FROM tblcustomer".  Thats going to select all customers.  To get the particular customer you need to have "SELECT * FROM tblcustomer WHERE id = ".$_GET["id"]

Do you get the value for $ts? For example if you type anywhere after the query executes echo $ts does it return the value you need?
Avatar of valicon

ASKER

Vulturous,

I tried it but it does not work. I just get a blank screen, the page does not display any data.
if($_GET['action'] == "edit"){
   $newsql = "select * from tblcustomer where id = $_GET[id]";
   $myresult = mysql_query($newsql);
   $x = mysql_fetch_array($myresult);
 $tsx = $x["strLastName"];
 $fnamex = $x["strFirstName"];
 $minitx = $x["strMiddle_Initial"];
 $addressx = $x["strAddress"];
 $racex = $x["strRace"];
 $dobx = $x["intDOB"];
 $sxx = $x["strSex"];
 $csnumx = $x["intCaseNumber"];
 $datex = $x["intDate"];
 $lenx = $x["strLength"];
 $officerx = $x["strofficename"];

         echo "
         <form name=\"form1\" method=\"POST\" action=\"editdelete.php?action=update\">
         <table><tr>
         <td align=center bgcolor=#EBEBEB>Last Name</td><td><input type=\"text\" name=\"strLastName\" value=\"$tsx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>First Name</td><td><input type=\"text\" name=\"strFirstName\" value=\"$fnamex\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>MI</td><td><input type=\"text\" name=\"strMiddle_Initial\" value=\"$minitx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>DOB</td><td><input type=\"text\" name=\"intDOB\" value=\"$dobx\" ></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Race</td><td><input type=\"text\" name=\"strRace\" value=\"$racex\"  ></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Sex</td><td><input type=\"text\" name=\"strSex\" value=\"$sexx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Address</td><td><input type=\"text\" name=\"strAddress\" value=\"$addressx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Case Number</td><td><input type=\"text\" name=\"intCaseNumber\" value=\"$csnumx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Date</td><td><input type=\"text\" name=\"intDate\" value=\"$datex\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Length</td><td><input type=\"text\" name=\"strLength\" value=\"$lenx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>office</td><td><input type=\"text\" name=\"strofficename\" value=\"$officerx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB></td><td><input type=\"hidden\" name=\"my_id\" value=\"$_GET[id]\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Update</td><td><input type=\"submit\" name=\"submit\"></td>
      </tr></table</form>
         ";
   
   }
what do you get if you just do
echo $ts anywhere after query
Avatar of valicon

ASKER

Hi psimation :)

I overwrote the original code with your post above and I am getting a blank page, no results.
Blank page only when you click Edit, or blank page when you load up the first time?

Maybe post the complete code again?

Avatar of valicon

ASKER

AlexanderR,

Using echo $ts I was just getting the echo statement in the field where the reord should have been.
You cant "echo" $ts there, it's already in an echo.
Avatar of valicon

ASKER

Blank page when it loads the first time.  Here is the code:



<?
include("search.php3");
include('db_login.php');

$connection = mysql_connect($db_host, $db_username, $db_password);

if (!$connection){
     die ("Could not connect to the database: <br />". mysql_error());
}

$selected = mysql_select_db("customer",$connection) or die("Could not select database");


//execute my SQL query and return records
$result = mysql_query("SELECT * FROM tblcustomer");

if ($result &&!$_GET['action']) {

   echo "<table width=90% align=center border=1><tr>
   <td align=center bgcolor=#EBEBEB>Last Name</td>
   <td align=center bgcolor=#EBEBEB>First Name</td>
   <td align=center bgcolor=#EBEBEB>MI</td>
   <td align=center bgcolor=#EBEBEB>DOB</td>
   <td align=center bgcolor=#EBEBEB>Race</td>
   <td align=center bgcolor=#EBEBEB>Sex</td>
   <td align=center bgcolor=#EBEBEB>Address</td>
   <td align=center bgcolor=#EBEBEB>Case Number</td>
   <td align=center bgcolor=#EBEBEB>Date</td>
   <td align=center bgcolor=#EBEBEB>Length</td>
   <td align=center bgcolor=#EBEBEB>office</td>
    <td align=center bgcolor=#EBEBEB>Edit</td>
     <td align=center bgcolor=#EBEBEB>Delete</td>
   </tr>";


//grab the data from the database
while ($r =  mysql_fetch_array($result)) {
 $my_id = $r["id"];
 $ts = $r["strLastName"];
 $fname = $r["strFirstName"];
 $minit = $r["strMiddle_Initial"];
 $address = $r["strAddress"];
 $race = $r["strRace"];
 $dob = $r["intDOB"];
 $sx = $r["strSex"];
 $csnum = $r["intCaseNumber"];
 $date = $r["intDate"];
 $len = $r["strLength"];
 $office = $r["strofficename"];


    echo "<tr>
    <td>$ts</td>
    <td>$fname</td>
    <td>$minit</td>
    <td>$address</td>
    <td>$race</td>
    <td>$dob</td>
    <td>$sx</td>
    <td>$csnum</td>
    <td>$date</td>
    <td>$len</td>
    <td>$officename</td>
    <td><a href=\"editdelete.php?action=edit&id=$my_id\">Edit</a></td>
    <td><a href=\"editdelete.php?action=delete&id=$my_id\" onclick=\"return confirm('Are you sure you want to delete this record?')\">Delete</a></td></tr>";

}

//echo "</table>";
}

if($_GET['action'] == "edit"){
   $newsql = "select * from tblcustomer where id = $_GET[id]";
   $myresult = mysql_query($newsql);
   $x = mysql_fetch_array($myresult);
 $tsx = $x["strLastName"];
 $fnamex = $x["strFirstName"];
 $minitx = $x["strMiddle_Initial"];
 $addressx = $x["strAddress"];
 $racex = $x["strRace"];
 $dobx = $x["intDOB"];
 $sxx = $x["strSex"];
 $csnumx = $x["intCaseNumber"];
 $datex = $x["intDate"];
 $lenx = $x["strLength"];
 $officex = $x["strofficename"];


         echo "
         <form name=\"form1\" method=\"POST\" action=\"editdelete.php?action=update\">
         <table><tr>
         <td align=center bgcolor=#EBEBEB>Last Name</td><td><input type=\"text\" name=\"strLastName\" value=\"$tsx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>First Name</td><td><input type=\"text\" name=\"strFirstName\" value=\"$fnamex\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>MI</td><td><input type=\"text\" name=\"strMiddle_Initial\" value=\"$minitx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>DOB</td><td><input type=\"text\" name=\"intDOB\" value=\"$dobx\" ></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Race</td><td><input type=\"text\" name=\"strRace\" value=\"$racex\"  ></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Sex</td><td><input type=\"text\" name=\"strSex\" value=\"$sexx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Address</td><td><input type=\"text\" name=\"strAddress\" value=\"$addressx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Case Number</td><td><input type=\"text\" name=\"intCaseNumber\" value=\"$csnumx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Date</td><td><input type=\"text\" name=\"intDate\" value=\"$datex\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Length</td><td><input type=\"text\" name=\"strLength\" value=\"$lenx\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>office</td><td><input type=\"text\" name=\"strofficename\" value=\"$officex\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB></td><td><input type=\"hidden\" name=\"my_id\" value=\"$_GET[id]\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Update</td><td><input type=\"submit\" name=\"submit\"></td>
      </tr></table</form>
         ";

   }




}

if($_GET['action'] == "delete") {
      $deletesql = "delete from tblcustomer where id = $_GET[id]";
      $resultdelete = mysql_query($deletesql);
      echo "Record Deleted<br>";
      echo '<a href="editdelete.php">Return to Admin page</a><br />';
      //echo $deletesql;

}

if ($_GET['action'] == "update") {
   $updatesql = "update tblcustomer set strLastName = \"$_POST[strLastName]\", strFirstName = \"$_POST[strFirstName]\", strMiddle_Initial=\"$_POST[strMiddle_Initial]\", intDOB = \"$_POST[intDOB]\", strRace = \"$_POST[strRace]\", strSex = \"$_POST[strSex]\", strAddress = \"$_POST[strAddress]\", intCaseNumber = \"$_POST[intCaseNumber]\", intDate = \"$_POST[intDate]\", strLength = \"$_POST[strLength]\", strofficename = \"$_POST[strofficename]\" where id = $_POST[my_id]";
      $resultupdate = mysql_query($updatesql);
      echo "Record Updated<br>";
      echo '<a href="editdelete.php">Return to Admin page</a><br />';
     // echo $updatesql;
}

//close the connection
mysql_close($connection);
?>












Sorry for misunderstandings.  I just saw whats going on here.  Here's another version of your code based on what i think you are trying to accomplish (i hope)
<?
include("search.php3");
include('db_login.php');

$connection = mysql_connect($db_host, $db_username, $db_password);

if (!$connection){
     die ("Could not connect to the database: <br />". mysql_error());
}

$selected = mysql_select_db("customer",$connection) or die("Could not select database");


if ($_GET['action'] == "") {
      //execute my SQL query and return records
            $result = mysql_query("SELECT * FROM tblcustomer");
            
            if ($result) {
            
               echo "<table width=90% align=center border=1><tr>
               <td align=center bgcolor=#EBEBEB>Last Name</td>
               <td align=center bgcolor=#EBEBEB>First Name</td>
               <td align=center bgcolor=#EBEBEB>MI</td>
               <td align=center bgcolor=#EBEBEB>DOB</td>
               <td align=center bgcolor=#EBEBEB>Race</td>
               <td align=center bgcolor=#EBEBEB>Sex</td>
               <td align=center bgcolor=#EBEBEB>Address</td>
               <td align=center bgcolor=#EBEBEB>Case Number</td>
               <td align=center bgcolor=#EBEBEB>Date</td>
               <td align=center bgcolor=#EBEBEB>Length</td>
               <td align=center bgcolor=#EBEBEB>office</td>
                <td align=center bgcolor=#EBEBEB>Edit</td>
                 <td align=center bgcolor=#EBEBEB>Delete</td>
               </tr>";
            
            
            
            //grab the data from the database
            while ($r =  mysql_fetch_array($result)) {
             $my_id = $r["id"];
             $ts = $r["strLastName"];
             $fname = $r["strFirstName"];
             $minit = $r["strMiddle_Initial"];
             $address = $r["strAddress"];
             $race = $r["strRace"];
             $dob = $r["intDOB"];
             $sx = $r["strSex"];
             $csnum = $r["intCaseNumber"];
             $date = $r["intDate"];
             $len = $r["strLength"];
             $officer = $r["strofficename"];
            
            
                echo "<tr>
                <td>$ts</td>
                <td>$fname</td>
                <td>$minit</td>
                <td>$address</td>
                <td>$race</td>
                <td>$dob</td>
                <td>$sx</td>
                <td>$csnum</td>
                <td>$date</td>
                <td>$len</td>
                <td>$officename</td>
                <td><a href=\"editdelete.php?action=edit&id=$my_id\">Edit</a></td>
                <td><a href=\"editdelete.php?action=delete&id=$my_id\" onclick=\"return confirm('Are you sure you want to delete this record?')\">Delete</a></td></tr>";
            
            }
            
            //echo "</table>";
                  
   }


} elseif($_GET['action'] == "edit"){
   
         $result = mysql_query("SELECT * FROM tblcustomer WHERE id = ".$_GET["my_id"]);
         while ($r =  mysql_fetch_array($result)) {
                $my_id = $r["id"];
                $ts = $r["strLastName"];
                $fname = $r["strFirstName"];
                $minit = $r["strMiddle_Initial"];
                $address = $r["strAddress"];
                $race = $r["strRace"];
                $dob = $r["intDOB"];
                $sx = $r["strSex"];
                $csnum = $r["intCaseNumber"];
                $date = $r["intDate"];
                $len = $r["strLength"];
                $officer = $r["strofficename"];
         }
         
         
   
         echo "
         <form name=\"form1\" method=\"POST\" action=\"editdelete.php?action=update\">
         <table><tr>
         <td align=center bgcolor=#EBEBEB>Last Name</td><td><input type=\"text\" name=\"strLastName\" value=\"$ts\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>First Name</td><td><input type=\"text\" name=\"strFirstName\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>MI</td><td><input type=\"text\" name=\"strMiddle_Initial\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>DOB</td><td><input type=\"text\" name=\"intDOB\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Race</td><td><input type=\"text\" name=\"strRace\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Sex</td><td><input type=\"text\" name=\"strSex\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Address</td><td><input type=\"text\" name=\"strAddress\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Case Number</td><td><input type=\"text\" name=\"intCaseNumber\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Date</td><td><input type=\"text\" name=\"intDate\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Length</td><td><input type=\"text\" name=\"strLength\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>office</td><td><input type=\"text\" name=\"strofficename\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB></td><td><input type=\"hidden\" name=\"my_id\" value=\"$_GET[id]\"></td></tr>
      <tr><td align=center bgcolor=#EBEBEB>Update</td><td><input type=\"submit\" name=\"submit\"></td>
      </tr></table</form>
         ";
   
}
elseif ($_GET['action'] == "delete") {
         $deletesql = "delete from tblTrespass where id = $_GET[id]";
         $resultdelete = mysql_query($deletesql);
         echo "Record Deleted<br>";
         echo '<a href="editdelete.php">Return to Admin page</a><br />';
         //echo $deletesql;
   
} elseif ($_GET['action'] == "update") {
      $updatesql = "update tblTrespass set strLastName = \"$_POST[strLastName]\", strFirstName = \"$_POST[strFirstName]\", strMiddle_Initial=\"$_POST[strMiddle_Initial]\", intDOB = \"$_POST[intDOB]\", strRace = \"$_POST[strRace]\", strSex = \"$_POST[strSex]\", strAddress = \"$_POST[strAddress]\", intCaseNumber = \"$_POST[intCaseNumber]\", intDate = \"$_POST[intDate]\", strLength = \"$_POST[strLength]\", strofficename = \"$_POST[strofficename]\" where id = $_POST[my_id]";
         $resultupdate = mysql_query($updatesql);
         echo "Record Updated<br>";
         echo '<a href="editdelete.php">Return to Admin page</a><br />';
        // echo $updatesql;
}
   
   //close the connection
   mysql_close($connection);
   ?>
of course i forgot to put all the value variables in your form.
ASKER CERTIFIED SOLUTION
Avatar of psimation
psimation
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of valicon

ASKER

I thought so "{", but I wasn't sure. Should I try the latest code post?
Avatar of ebosscher
ebosscher

ummm... ok, first off, please don't ever use that conditional structure ever again, you're testing the same variable multiple times for different conditions.  That section in memory will only ever contain one value at one time, there is no reason to do multiple conditionl tests when you're only looking for one value.

if($result !== false && is_set($_GET["action"])
{
   switch(tolower($_GET['action'])
   {
      case 'edit':
         createEditScreen($result);
         break;
      case 'update':
         updateValues();
         break;
      case 'delete':
         deleteValues();
         break;
      default:
         displayDefaultScreen();

         // just for debugging, lets see why we got here
         echo 'we are displaying the default screen for a $_GET["action"] value of ' . $_GET['action'];
         break;
   }
}
else
{
   displayDefaultScreen();

   // this is just for debugging, to see if i'm right
   if($result === false)
   {
      echo '$result was false, there is an issue with the sql statment being used';
   }
   else
   {
      echo '$result was true, so $_GET["action"] is not set';
   }
}

chances are you weren't seeing anything because ither $result was not equal to true, or because $_GET['action'] was one of the following:  not set, or not in the case you're testing against.

hope that helps

additionally, you'll see that i didn't supply the logic for the routines you'll need to write, but you methods or functions shouldn't be mutliple pages long, it makes it very hard to maintain the code.
sorry, the tolower should be a strtolower in the previous post - man, tryping code without a debugger - catches me sometimes....  there may be additional syntactial errors, but i think you'll get the gist of it
Avatar of valicon

ASKER

ebosscher,

Thank you for your code, however I need a complete answer, PHP is not my forte`.
have u copied&pasted my code from 4 posts back yet? Should work "out of the box"?

Avatar of valicon

ASKER

psimation,

Yes just did it this morning, awesome job :)  The only issue I have is that the fields are out of alignment, i.e. the address is being displayed in the DOB field and the office field is not being written to. Not it the edit form but in the table when the page is loaded. I am trying to find the problem but haven't so far.
Avatar of valicon

ASKER

I fixed the fields so they display under the correct headings but the office field is not posting when you do a edit
Avatar of valicon

ASKER

psimation,

I got it squared. It works great. Thanks again for yet another great job!