使用PDO将输入数组插入mysql

时间:2016-07-13 14:50:32

标签: php mysql pdo

在我的表格中有两个输入如下:

<input type="text" class="form-control input-sm" name="topico[]"/>
<textarea rows="2" cols="46" maxlength="120" class="form-control input-sm" name="descricao[]"></textarea>

然后将

分配到类属性中,如下所示:

if ($_POST) {
// seta propriedades do objeto
$topico->nome = $_POST['topico'];
$topico->descricao = $_POST['descricao'];
 if (!empty($_POST['id'])) {
    if ($topico->update()) {
        echo "<div class=\"alert alert-success alert-dismissable\">";
        echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
        echo "Tópico atualizado com sucesso.";
        echo "</div>";
    } else {
        echo "<div class=\"alert alert-danger alert-dismissable\">";
        echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
        echo "Erro ao atualizar tópico.";
        echo "</div>";
    }
}else {
    if ($topico->create()) {
        echo "<div class=\"alert alert-success alert-dismissable\">";
        echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
        echo "Tópico criado com sucesso.";
        echo "</div>";
    } else {
        echo "<div class=\"alert alert-danger alert-dismissable\">";
        echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
        echo "Erro ao criar tópico.";
        echo "</div>";
    }
}

我遇到麻烦的创建方法,因为我不知道如何在数据库中插入这些数据。

public function create() {
    $query = "INSERT INTO " . $this->table_name . " (nome, descricao) values (?, ?)";
    $stmt = $this->conn->prepare($query);
    $this->nome = htmlspecialchars(strip_tags($this->nome));
    $this->descricao = htmlspecialchars(strip_tags($this->descricao));
    $stmt->bindParam(1, $this->nome);
    $stmt->bindParam(2, $this->descricao);
    // execute statement
    if ($stmt->execute()) {
        return true;
    } else {
        return false;
    }
}

我可以告诉我必须使用foreach函数,我该怎么做?

1 个答案:

答案 0 :(得分:0)

我最终循环遍历$ _POST并为每一行调用$ topico-&gt; create虽然这不是最好的方法,但这是我找到的唯一方法。

if ($_POST) {

foreach ($_POST['topico'] as $index => $row) {
    $topico->nome = $_POST['topico'][$index];
    $topico->descricao = $_POST['descricao'][$index];
    $topico->id = $_POST['id'][$index];

    if (!empty($_POST['id'])) {
        if ($topico->update()) {
            echo "<div class=\"alert alert-success alert-dismissable\">";
            echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
            echo "Tópico atualizado com sucesso.";
            echo "</div>";
        } else {
            echo "<div class=\"alert alert-danger alert-dismissable\">";
            echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
            echo "Erro ao atualizar tópico.";
            echo "</div>";
        }
    } else {
        if ($topico->create()) {
            echo "<div class=\"alert alert-success alert-dismissable\">";
            echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
            echo "Tópico criado com sucesso.";
            echo "</div>";
        } else {
            echo "<div class=\"alert alert-danger alert-dismissable\">";
            echo "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button>";
            echo "Erro ao criar tópico.";
            echo "</div>";
        }
    }
}

}