Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Auto populate form via PHP from MySQL

Posted on 2007-07-20
26
Medium Priority
?
487 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:valicon
  • 11
  • 7
  • 5
  • +2
26 Comments
 
LVL 11

Expert Comment

by:AlexanderR
ID: 19531281
$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>
0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 19531313
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.
0
 
LVL 12

Author Comment

by:valicon
ID: 19531867
I was hoping not to add a bunch of code to the original script. Isn't there a way that is shorter, code wise?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:AlexanderR
ID: 19531972
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?
0
 
LVL 12

Author Comment

by:valicon
ID: 19532768
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);
   ?>


0
 
LVL 18

Expert Comment

by:Antonio Estrada
ID: 19533531
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
0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 19533637
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?
0
 
LVL 12

Author Comment

by:valicon
ID: 19533687
Vulturous,

I tried it but it does not work. I just get a blank screen, the page does not display any data.
0
 
LVL 17

Expert Comment

by:psimation
ID: 19533862
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>
         ";
   
   }
0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 19533967
what do you get if you just do
echo $ts anywhere after query
0
 
LVL 12

Author Comment

by:valicon
ID: 19534001
Hi psimation :)

I overwrote the original code with your post above and I am getting a blank page, no results.
0
 
LVL 17

Expert Comment

by:psimation
ID: 19534022
Blank page only when you click Edit, or blank page when you load up the first time?

Maybe post the complete code again?

0
 
LVL 12

Author Comment

by:valicon
ID: 19534035
AlexanderR,

Using echo $ts I was just getting the echo statement in the field where the reord should have been.
0
 
LVL 17

Expert Comment

by:psimation
ID: 19534066
You cant "echo" $ts there, it's already in an echo.
0
 
LVL 12

Author Comment

by:valicon
ID: 19534148
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);
?>












0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 19534264
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);
   ?>
0
 
LVL 11

Expert Comment

by:AlexanderR
ID: 19534294
of course i forgot to put all the value variables in your form.
0
 
LVL 17

Accepted Solution

by:
psimation earned 2000 total points
ID: 19534431
You pasted an extra "{" in your 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);
?>
0
 
LVL 12

Author Comment

by:valicon
ID: 19535942
I thought so "{", but I wasn't sure. Should I try the latest code post?
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 19537306
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.
0
 
LVL 6

Expert Comment

by:ebosscher
ID: 19537308
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
0
 
LVL 12

Author Comment

by:valicon
ID: 19546720
ebosscher,

Thank you for your code, however I need a complete answer, PHP is not my forte`.
0
 
LVL 17

Expert Comment

by:psimation
ID: 19546746
have u copied&pasted my code from 4 posts back yet? Should work "out of the box"?

0
 
LVL 12

Author Comment

by:valicon
ID: 19547713
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.
0
 
LVL 12

Author Comment

by:valicon
ID: 19547848
I fixed the fields so they display under the correct headings but the office field is not posting when you do a edit
0
 
LVL 12

Author Comment

by:valicon
ID: 19548108
psimation,

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question