Katie Katie - 25 days ago 5
MySQL Question

Inserting reservation record to php database

I have created a reservation form for housing. The user fills the form out and submits it. A reservation record is supposed to be made which is then inserted into a reservations table.

This is the code that is being inserted into the database as a record

$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"]]);


These are the functions that transform this data:

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


When I use print_r($values) to check the array of values it only returns 1. I'm just not sure why the data being inserted is being read wrong.

Answer

Change here , use back tick in cumn and single quotes in values

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