2009-10-23 59 views
1

我有一個註冊頁面。當頁面加載時,它每次嘗試將值插入默認爲空的文本框中。請幫忙。我的代碼被附加在網頁的每次加載時插入空記錄

register.template.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>RBlog - For the Consumers - By the Sellers</title> 
<?php include("dbclass/register_class.php");?> 
</head> 
<body> 
<?php include("includes/header.php"); ?> 
    <div class="afterpicer_total"> 
    <?php include("includes/menu.php"); ?> 
    <div class="wrapper"> 
      <div class="cont"> 
       <?php include("includes/left_menu.php"); ?> 
       <div class="reg_cont"> 
        <form name="add_user" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>"> 

         <div class="reg_label">Name</div> 
         <div class="reg_tbox"><input type="text" size="32" name="name" class="reg_tbox_style" ></input></div><br/> 
         <div class="reg_label">Email ID</div> 
         <div class="reg_tbox"><input type="text" size="32" name="email" class="reg_tbox_style"></input></div><br/> 
         <div class="reg_label" >Confirm Email ID</div> 
         <div class="reg_tbox"><input type="text" size="32" name="cemail" class="reg_tbox_style"></input></div><br/> 
         <div class="reg_label" >Password</div> 
         <div class="reg_tbox"><input type="text" size="32" name="pass" class="reg_tbox_style"></input></div><br/> 
         <div class="reg_label" >Confirm Password</div> 
         <div class="reg_tbox"><input type="text" size="32" name="cpass" class="reg_tbox_style"></input></div><br/> 
         <div class="reg_label" >Mobile No</div> 
         <div class="reg_tbox"><input type="text" size="32" name="mobile" class="reg_tbox_style"></input></div><br/> 
         <? 

          $adduser=new Users(); 
       $adduser->addDB(); 
         ?> 
         <div class="reg_tbox"><input type="submit" value="Register" class="reg_but_style"style="margin-left:155px;"></input></div> 


        </form> 

       </div> 
      </div> 
      <div class="adspace"> Advertisement Space</div> 
     </div> 
    </div> 

</body> 
</html> 

register_class.php

<?php 
ob_start(); 

    include("common/class.Database.php"); 
    include("common/varDeclare.php"); 

class Users extends Database 
{ 
    var $fields_val = array(); 
    var $fields_value = array(); 
    var $state_name = array(); 
    var $db; 
    function addDB() 
    { 

     try{ 
      $reg_date   =date("Y-m-d G:i:s"); 

      $username   =$_POST[name]; 
      $email    =$_POST[email]; 
      $password   =$_POST[pass]; 
      $mobile    =$_POST[mobile]; 


         $user_query = "insert into register set email = '".$email."', pwd = '".$password."', mobile = '".$mobile."', reg_date = '".$now."'"; 
      $insertUser=$this->do_query($user_query); 


      $retVal=true; 

       if(!$insertUser){ 
        $this->errMsg = REG_FAILED; 
        $retVal = false; 
       }  
      return $retVal; 
     } 
     catch(Exception $e) 
     { 
      echo 'Caught exception: ', $e->getMessage(), "\n"; 
     } 
    } 

// ---- End of addDB()------    

} 
////////End of class///////////// 

回答

2

空值插入每次都因爲在register.template.php,你還沒有插入僅當表單已提交時才執行addDB()的條件語句。你可以通過改變

 
$adduser=new Users(); 
$adduser->addDB(); 

解決這

 
if ($_SERVER['REQUEST_METHOD'] === 'POST') { 
    $adduser=new Users(); 
    $adduser->addDB(); 
} 

另外,如果您打算將它們插入到一個數據庫,這樣,你真的應該逃避所有$_POST值與mysql_real_escape_string

+0

百萬感謝,它的工作 – Rajasekar 2009-10-23 07:13:23