我似乎无法将 $_SESSION 变量的值写入具有自动递增主键的数据库表的记录中。我在论坛上找了很多地方,但找不到解决我问题的方法。我的预感是我需要添加一些东西才能使自动增量起作用,但我不确定。我正在使用的代码如下所示。我究竟做错了什么?
<?php
// SESSION FILE
include ("sessionstart.inc");
echo "<html>
<head><title>Reports-SESSION</title></head>
<body>";
include ("misc.inc"); //THIS SETS $cxn AS MY VARIABLE TO CONNECT TO THE DATABASE
// SELECT THE DATABASE TO USE
mysqli_select_db($cxn,"carregistration") or die(mysqli_error($cxn));
// SET TEST VALUES IN $_SESSION VARIABLE
$_SESSION['OwnerCarIdentifier']='6Gh7J8';
$_SESSION['CarType']='station car';
// TEST TO SEE WHAT VALUES ARE NOW IN $_SESSION
foreach ($_SESSION as $field => $value)
{
echo "$field = $value<br />\n";
}
// THIS CONFIRMS THAT THE TWO $_SESSION VALUES ARE THERE AND AS SET ABOVE
echo "<br />";
// NOW WRITE VALUES FROM $_SESSION INTO A NEW RECORD IN THE DATABASE
mysqli_query($cxn,"INSERT INTO 'car' (OwnerCarIdentifier,CarType) VALUES ('$_SESSION[OwnerCarIdentifier]','$_SESSION[CarType]')");
//ERROR CHECKING TO SEE IF THE DATA WERE SUCCESSFULLY RECEIVED BY THE DATABASE OR NOT.
if (mysqli_connect_errno()) {
echo "The connection with the database failed." . mysqli_connect_error();
exit();
}
// CLOSE THE RECORD
mysqli_close($cxn);
//DESTROY THE SESSION: THIS STATEMENT SHOULD APPEAR AT THE VERY END WHEN YOU HAVE STORED ALL THE FIELDS FOR A ROW IN THE DATABASE
session_destroy();
?>
</body>
</html>