将预订记录插入php数据库

时间:2016-11-13 00:27:52

标签: php mysql database phpmyadmin insert

我已经为住房创建了预订表格。用户填写表单并提交。应该制作预订记录,然后将其插入预订表。

这是作为记录

插入数据库的代码
$reservationsTable = "Reservations";
$r1 = (rand(11111,99999));

createReservationRecord($reservationsTable, [$r1, $date,$dormRecord[id], $_POST["CWID"], $_POST["firstName"], $_POST["lastName"], $_POST["class"], $_POST["gender"], $_POST["fullyEquippedKitchen"], $_POST["laundry"], $_POST["specialNeeds"]]);

这些是转换此数据的函数:

 function createRecord($table, $values) {
    echo "This is working";
    return insertInto($table, ["name","class","specialNeeds","laundry","fullyEquippeKkitchen","roomsAvailable","roomsReserved","roomCapacity"], $values);

}

function createReservationRecord($table, $values) {
    echo "<br> in createReservationRecord(), table is \"$table\", values are ".print_r ($values)."\n<br>";
    return insertInto($table, ["id","reservationTime","ra_id","CWID","firstName","lastName","class", "gender","kitchen", "laundry", "specialNeeds"], $values);
}

function insertInto($table, $columns, $values) {
    $sql = "INSERT INTO $table (" . implode(", ", $columns) . ") VALUES (" . implode(", ", $values) . ")";
    echo "Inserting a new record with SQL Statement: $sql\n<br>";
    return query($sql);
}

当我使用print_r($ values)检查值数组时,它只返回1.我只是不确定为什么插入的数据读错了。

2 个答案:

答案 0 :(得分:-1)

我没有看到代码中的错误,但请尝试

$sql = "INSERT INTO ". $table." (" . implode(", ", $columns) . ") VALUES (" . implode(", ", $values) . ")";

答案 1 :(得分:-1)

在这里更改,在cumn中使用返回tick并在值

中使用单引号
   $sql = "INSERT INTO $table (`" . implode("`, `", $columns) . "`) VALUES ('" . implode("', '", $values) . "')";