使用类函数将记录添加到数据库

时间:2016-04-04 12:52:24

标签: php mysql database-connection

我正在尝试使用以下函数将记录添加到数据库中:

[ERROR ] HHH000388: Unsuccessful: alter table DASH104411.table1 add constraint 
FK_m1vvn3lavq1rjt9oghf0m3x5t foreign key (id_table2) references DASH104411.table2
[ERROR ] DB2 SQL Error: SQLCODE=-1667, SQLSTATE=42858, SQLERRMC=DASH104411.TABLE2;
ORGANIZE BY COLUMN;ENFORCED, DRIVER=4.19.49

并用它来调用它:

require_once('../config/Database.php');

class Category {
    private $conn;
    private $id;
    private $name;
    private $description;
    private $created;

public function __construct($db) {
    $this->conn = $db;
}

public function addRecord($n, $desc) {
        $this->conn->prepare("INSERT INTO categories VALUES('', '$n', '$desc', CURDATE())")->execute();
        $this->name = $n;
        $this->description = $desc;
    }

数据库连接在这里:

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if ( isset( $_POST['addRecord'], $_POST['n'], $_POST['desc']) ) {

    $cat = new Category($conn);

    $n = filter_input ( INPUT_POST , 'n', FILTER_SANITIZE_STRING );
    $desc = filter_input ( INPUT_POST , 'desc', FILTER_SANITIZE_STRING );
    $cat->addRecord($n, $desc);
    }
}
?>
<div style="border: 5px solid black; width: 40%">
<h3>addRecord($n, $desc):</h3>
<form action="" method="post">
<label>Category name :</label>
<input type="text" name="n" id="n" required="required" placeholder="Please Enter Name"/><br /><br />
<label>Category description :</label>
<input type="text" name="desc" id="desc" required="required" placeholder="Please Enter Description"/><br /><br />
<input type="submit" value="addRecord" name="addRecord"/><br />
</form>
</div>

我没有收到任何错误,但它没有工作,当我按下“addRecord”按钮时没有任何事情发生

0 个答案:

没有答案