Jump to content

Recommended Posts

Ok, so I have a form for employee information, and it gets stored in the database after they submit their info... I need to be able to repopulate that info back into the form so that they can update it if they need to, how do I go about doing that?  Please help :-[! Here is the mysql query for the employee's info...

$employee_info = mysql_query("SELECT * FROM `employee_info` WHERE `employee_info`.`myusername` = '$myusername'") or die(mysql_error());

and here is a segment of code from the form itself...

<input type="text" name="hire_date" id="hire_date" value="<?php echo isset($_POST['hire_date']) ? $_POST['hire_date'] : '';?>" size="34" maxlength="50" />

Link to comment
https://forums.phpfreaks.com/topic/157839-solved-how-to-repopulate-form-fields/
Share on other sites

Not quite. You may want the post value there if there are form errors

<input type="text" name="hire_date" id="hire_date" value="<?php echo isset($_POST['hire_date']) ? $_POST['hire_date'] : $rows['hire_date'];?>" size="34" maxlength="50" />

You are using the $_POST variable incorrectly. This element has no value until the form is processed or reloaded by a form event (i.e. pressing a button to process the data).

 

With this type of processing, you appear to be using 2 views: initial population of the form elements and then processed form element after changes made. In order to facilitate this, I have had to use a standard page variable and then push the value from the applicable source (database fetch or $_POST) into that variable to be displayed.

 

Also, I am assuming that the user name is being populated on the page via a url such as: www.mypage.com/index.php?username=myusername, or as a $_SESSION variable as so:

 

<?php
    if(isset($_POST['myusername'] && !empty($_POST['myusername']){
         $user = $_POST['myusername'];
         $hiredate = $_POST['hire_date'];
    }
    else{
        $user = $_GET['myusername'];
// Or if using $_SESSION variables, the code is
         $user = $_SESSION['myusername'];
         $hiredate='';
    }
?>

 

Now do the db stuff and populate the next form variable (hiredate):

<?php
$employee_info = mysql_query("SELECT * FROM `employee_info` WHERE `employee_info`.`myusername` = '$myusername'") or die(mysql_error());
// retrieve the array
$empl = mysql_fetch_row($employee_info);
$hiredate=$empl[1];
?>

 

<?php
<input type="text" name="hire_date" id="hire_date" value="<?php echo strlen($hire_date)>0 ? $hire_date : '';?>" size="34" maxlength="50" />
?>

Yes I am using session variables to apply myusername to that user.  Question... so that code will have to be placed before every form variable?and concerning the last piece of code you put in, I don't think you can have <?php within another like so... <?php <?php ?> ?> Are you storing the rows in an array so they can be selected in order?

The above code is not needed at all. 2 simple facts

1. If you are editing data from the database then the field will be prefilled with the database value

2. If the user resubmits the data and there is a form error you want the value of the $_POST array to remain in the field value rather than the database value.

 

The initial shorthand if statement does just this

Thanks neil, but I have another question for you, how does : $rows['state'] work?  Do I need to assign rows and then use them as you have said, or do i need to create a global variable rows or something? It's not working is the reason i ask, do I need to place php code above the form so that I can use the shorthand  if statements you showed me?

This comes from your SQL query so:

$employee_info = mysql_query("SELECT * FROM employee_info WHERE employee_info.myusername = '$myusername'") or die(mysql_error());
$rows = mysql_fetch_assoc($employee_info);

// you can then use the result set i.e
print $rows['state'];

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.