2013-01-23 84 views
1

我有一個Codeigniter Form發佈變量未發送到DB - 笨

<?php 
    $attr = array('id'=>'urlSubmit','method'=>'post'); 
    $urlInputAttr = array('name'=>'urlInput','value'=>'yourdomain.com','maxlength'=>'50','size'=>'25'); 
    echo form_open('urlSubmission',$attr); 
    echo form_input($urlInputAttr); 
    echo form_close(); 
?> 

然後controller

$this->load->view('support/header', $urlSubmissionMeta); ## Header info 
    $this->load->view('support/toolbar'); #logo 
    // 
    # Determine whether domain already has been crawled. 
    $this->load->model('domaincheckmodel'); 
     $this->domaincheckmodel->verifyduplicates(); 

    # Then Perform Scan 
    $this->load->model('performscanmodel'); 
     $this->performscanmodel->fetchlinks(); 

一個model,稱爲domaincheckmodel.php:

function verifyduplicates(){ 
     #PREPARE DATA 
     $postedTLD = $_POST["urlInput"]; // Get unsanitized data 
     $sql = "SELECT tld from ClientDomain WHERE tld = ?"; 
     $endquery = $this->db->query($sql,array($this->db->escape_str($postedTLD))); // Query db 

     #CONDITION - if #rows GT 0, load domain already exists. 
     if($endquery->num_rows() > 0){ 
      $this->load->view('err/domainexists'); ##domain already used 
      ## redir to new pagee to .. please login.. 
     } 

     else{ #number of rows must be 0, insert into db 
      $newDomain = "INSERT INTO ClientDomain(tld) VALUES('".$this->db->escape_str($postedTLD)."')"; 
      $this->db->query($newDomain); 

      $this->load->view('success/domainfree'); 
      ## please register to begin saving history of your scans, without doing so you lose results. 
     } 

問題是我提交表格時,if{}else{}語句在模型中正常工作,但變量$postedTLD正在向mysql發送空值。

這裏有任何想法,爲什麼這是發送?

回答

3
$postedTLD = $_POST["urlInput"]; 

應該是:

$postedTLD = $this->input->post('urlInput'); 

關於如何訪問POST值的更多信息,請參見documentation

+0

天哪是一個新手的錯誤。唷 - 謝謝! – CodeTalk

+0

雖然這個答案是正確的,但我建議閱讀與最佳編程習慣有關的答案。 [SO問題](http://stackoverflow.com/a/4096728/1260593) – ekims

+0

兩者有什麼區別? – itachi

0

您可以使用單行代碼將所有發佈數據作爲數組獲取。

$postData=$this->input->post(NULL,TRUE);