我有一个 foreach 代码,它从其他表(例如评估、出勤)收集每个员工 ID 的数据。
现在我想将这些新收集的数据推送到一个名为 dss
的新表中,所以这是我的代码
$servername = "localhost";
$dbname = "ems_db";
// Create connection
$conn = new mysqli($servername, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO dss (emp_name, average, date)
VALUES ($name, $average, $date)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
这是我得到的结果
Error: INSERT INTO dss (emp_name, average, date) VALUES (Francis Ajax, 65, 2016-03-17)
No database selected
Error: INSERT INTO dss (emp_name, average, date) VALUES (Horus Skye, 66, 2016-03-17)
No database selected
Error: INSERT INTO dss (emp_name, average, date) VALUES (Lotus Po, 68, 2016-03-17)
No database selected
有人可以告诉我我做错了什么以及如何解决吗? :(
发现新错误
Error: INSERT INTO dss (emp_name, average, date) VALUES (Lotus Po, 68, 2016-03-17)
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Po, 68, 2016-03-17)' at line 2
我认为这是因为 varchar 需要 ""
或 ' '
但由于我的值来自变量,我真的不知道如何修复它..
我是一名优秀的程序员,十分优秀!