2013-08-18 73 views
0

我正在開發一個簡單的PHP網站。我有一個更新值的表單。當這個表單通過PHP(網站)訪問時,它應該有以前的值,但它沒有。怎麼做 ?如何使表單值保留在表單

因爲,否則它會顯示所有字段爲空,並在更新一個或兩個時更新,其他更新爲空。請幫忙 !

<?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

發佈您的代碼! –

+0

剛剛做到了。請檢查請! – Vineet

回答

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> 
+0

試過了。它不工作! – Vineet

+0

它仍然存在,但是當我再次移動到另一個頁面時,comenack。他們不在那裏。它是一個更新表單。價值觀應該已經存在。而且它的值是在數據庫中。 – Vineet

+0

@Vineet然後嘗試使用會話。 –

相關問題