使用HTML表单插入数据库

时间:2014-05-01 15:04:15

标签: php mysql templates

我想在我的数据库中插入HTML表单中的某些值。 现在,当我尝试这个时,它会给我一个空白屏幕,并且数据库中没有插入任何内容。 在实例化$ model或部分时似乎有问题:$this->model->update_klant($k);

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");
    }
}

?>

1 个答案:

答案 0 :(得分: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