0

我正在开发一个简单的 PHP 网站。我有一个更新值的表格。当通过 PHP(website) 访问该表单时,它应该具有以前的值,但它没有。怎么做 ?

因为,否则,它会显示所有字段为空,并且当更新一两个字段时,其他字段会更新为空。请帮忙 !

<?php

require_once('../includes/config.php');
require_once('../includes/functions.php');
require_once('../includes/session.php');
require_once('../includes/database.php');
require_once('../includes/user.php');
require_once('../includes/photograph.php');
if (!$session->is_logged_in()) { redirect_to("login.php"); }

?>

<?php




 // START FORM PROCESSING
if (isset($_POST['submit'])) { // Form has been submitted.

    $errors = array();

    // perform validations on the form data
    $required_fields = array('lives_in', 'belongs_to', 'college', 'works_at', 'grade', 'grade2', 'hobbies', 'zodiac', 'phone_no', 'facebook', 'company', 'bio');
    $errors = array_merge($errors, check_required_fields($required_fields, $_POST));


           $lives_in                = trim($database->escape_value($_POST['lives_in']));
           $belongs_to              = trim($database->escape_value($_POST['belongs_to']));
           $college                 = trim($database->escape_value($_POST['college']));
           $works_at                = trim($database->escape_value($_POST['works_at']));
           $grade                   = trim($database->escape_value($_POST['grade']));
           $grade2                  = trim($database->escape_value($_POST['grade2']));
           $hobbies                 = trim($database->escape_value($_POST['hobbies']));
           $zodiac                  = trim($database->escape_value($_POST['zodiac']));
           $phone_no                = trim($database->escape_value($_POST['phone_no']));
           $facebook                = trim($database->escape_value($_POST['facebook']));
           $company                 = trim($database->escape_value($_POST['company']));
           $bio                     = trim($database->escape_value($_POST['bio']));



if (empty($errors) ) {
        $query = "UPDATE users

        SET lives_in     = '{$lives_in}',
         belongs_to      = '{$belongs_to}',
         college         = '{$college}' ,
         works_at        = '{$works_at}',
         grade           = '{$grade}',
         grade2          = '{$grade2}',
         hobbies         = '{$hobbies}',
         zodiac          = '{$zodiac}',
         phone_no        = '{$phone_no}',
         facebook        = '{$facebook}',
         company         = '{$company}',
         bio             = '{$bio}'
         WHERE id        =  23 
         LIMIT 1";

        $result = mysql_query($query, $database->connection);
        if ($result) {
            $message = "Account Updated";
        } else {
            $message  =  mysql_error();

        }
    } else {
        if (count($errors) == 1) {
            $message = mysql_error();;
        } else {
            $message = "There were " . count($errors) . " errors in the form.";
        }
    }
} else { // Form has not been submitted.

           $lives_in                = "";
           $belongs_to              = "";
           $college                 = "";
           $works_at                = "";
           $grade                   = "";
           $grade2                  = "";
           $hobbies                 = "";
           $zodiac                  = "";
           $phone_no                = "";
           $facebook                = "";
           $company                 = "";
           $bio                     = "";


}

?>

4

1 回答 1

0

很简单,只要在用php创建表单的时候把之前的值打印出来就行了。

例如:

<input type="text" name="field_name" value="<?php echo $previous_value;?>" />

或者,如果它是一个选择,让我们说:

<select name="field_name">
<?php foreach($posible_values as $value){ ?>
    <option value="<?php echo $value;?>" 
        <?php echo ($value==$previous_value)?'selected="selected"':'';?> >
        <?echo $value?>
    </option>
<?php } ?>
</select>
于 2013-08-18T20:39:38.190 回答