我想在数据库中插入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");
    }
}

?>

最佳答案

在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

10-05 21:02
查看更多