2014-05-01 39 views
0

我想將我放入HTML表單的某些值插入到數據庫中。 現在,當我嘗試這個,它給了我一個空白的屏幕,並沒有插入數據庫中。 實例化$ model或part:$this->model->update_klant($k);似乎有問題。插入到帶有HTML表格的數據庫中

Form extends Model 

<?php include 'header.php'; ?>  

    <div id="content"> 
     <h1 align="center">Certain CRM</h1> 
     <table align="center" border="1"> 
     <tr> 
      <th>id</th> 
      <th>klant</th> 
      <th>relevantie</th> 
      <th>omschrijving</th> 
      <th>eerste contact</th> 
      <th>opmerking</th> 
      <th>afspraak</th> 
     </tr> 
<?php    
    $projects = $projectlist[0]; 
    foreach ($projects as $p) 
    { 
     echo "<tr>"; 
     echo "<td>".$p->id."</td>"; 
     echo "<td>".$p->klant."</td>"; 
     echo "<td>".$p->relevantie."</td>"; 
     echo "<td>".$p->omschrijving."</td>"; 
     echo "<td>".$p->eerste_contact."</td>"; 
     echo "<td>".$p->opmerking."</td>"; 
     echo "<td>".$p->afspraak."</td>"; 
     echo "</tr>"; 
    } 
?> 
     </table> 
    </div> 

    <form align="center" action="form/submit" method="post"> 
     klant: <input type="text" name="klant"><br> 
     relevantie: <input type="number" name="relevantie"><br> 
     omschrijving: <input type="text" name="omschrijving"><br> 
     eerste contact: <input type="date" name="eerste_contact"><br> 
     opmerking: <input type="text" name="opmerking"><br> 
     afspraak: <input type="radio" name="afspraak" value="ja">Ja 
     <input type="radio" name="afspraak" value="nee">Nee<br> 
     <input type="hidden" name="submitted" value="yes"> 
     <input type="submit"> 
    </form> 

<?php 

class Form extends Model { 
    //public $klant; 
    //public $relevantie; 
    //public $omschrijving; 
    //public $eerste_contact; 
    //public $opmerking; 
    //public $afspraak; 

    public function update_klant($klant) 
    { 
     $result = $this->query('INSERT into projects (klant) VALUES ('$klant')'); 
    } 

    public function update_relevantie($relevantie) 
    { 
     $result = $this->query("INSERT into projects (relevantie) VALUES ('$relevantie')"); 
    } 

    public function update_omschrijving($omschrijving) 
    { 
     $result = $this->query("INSERT into projects (omschrijving) VALUES ('$omschrijving')"); 
    } 

    public function update_eerste_contact($eerste_contact) 
    { 
     $result = $this->query("INSERT into projects (eerste_contact) VALUES ('$eerste_contact')"); 
    } 

    public function update_opmerking($opmerking) 
    { 
     $result = $this->query("INSERT into projects (opmerking) VALUES ('$opmerking')"); 
    } 

    public function update_afspraak($afspraak) 
    { 
     $result = $this->query("INSERT into projects (afspraak) VALUES ('$afspraak')"); 
    } 
} 

?> 

<?php 

class Form extends Controller { 

    public function index() { 
     $model = $this->loadModel('Form'); 
    } 

    public function submit() {      

     if (!isset($_POST['submitted'])) 
      return; 

     foreach ($_POST as $g => $k) 
     { 
      if ($g == 'klant') { 
       $this->model->update_klant($k); 
      } 
     } 
     echo ("Sucessful submission"); 
    } 
} 

?> 
+0

此代碼與MVC無關。此外,您的「表抽象」包含SQL注入漏洞。 –

+0

您是否檢查過您的變量值是否填充到窗體類中。也不知道你是否以正確的方式使用繼承。你可以繼承一個類,如果你可以說:每個x都是y。在你的情況下:每個表單都是一個控制器,或者每個表單都是一個模型。 – Kevin

+0

這是所有這一系列文件?如果不是,請把它分成邏輯單元。 – RiggsFolly

回答

0

在你update_klant功能使用的是單引號代替雙引號:

public function update_klant($klant) 
{ 
    $result = $this->query('INSERT into projects (klant) VALUES ('$klant')'); 
} 

正確的使用方法是:

public function update_klant($klant) 
{ 
    $result = $this->query("INSERT into projects (klant) VALUES ('$klant')"); 
} 

您也可以改善這個你插入循環:

foreach ($_POST as $g => $k) 
{ 
    $updt_func = 'update_'.$g; 

    if (method_exists($this->model, $updt_func)) 
    { 
    $this->model->$updt_func($k); 
    } 
} 

瞭解更多關於變量函數的信息:http://www.php.net/manual/en/functions.variable-functions.php

相關問題