我正在尝试从我的数据库中的一个表中取一条记录并将其放在另一个表中(作为数据的备份 - 需要保存7年,包括删除的数据)。选择工作,删除工作,但插入不。 怎么了?我该怎么做才能解决它?
代码:
////////// DELETE STUDENT RECORD /////////
///// step 1 - copy all data to exStudentInfo with removal date //////
$studentID = $_REQUEST['sID'];
// pull student data from the database //
$query = "SELECT * FROM studentinfo WHERE studentID=" . $studentID;
$statement = $db->prepare($query);
$statement ->execute();
while($row = $statement->fetchObject())
{
$studentFirst = $row->studentFirst;
$studentLast = $row->studentLast;
$studentDOB = $row->studentDOB;
$studentGrade = $row->studentGrade;
$studentClass = $row->studentClass;
$studentAllergy = $row->studentAllergy;
$studentFam = $row->studentFam;
$removeDate = date("Y-m-d");
}
已解决:问题在于$ query ...我使用了普通的撇号(')而不是正确的反引号(`) - 而且我忘了将照片字段包含在绑定值中。
$query = "INSERT INTO 'sundayschool'.'exstudentinfo' ('studentID' ,'studentFirst' ,'studentLast' ,'famID' ,'studentDOB' ,'studentGrade' ,'studentClass' ,'studentAllergy' ,'studentPhoto' ,'removeDate') VALUES (:id, :first, :last, :fam, :dob, :grade, :class, :allergy, :date)";
正确的表格:
$query = "INSERT INTO `sundayschool`.`exstudentinfo` (`studentID` ,`studentFirst` ,`studentLast` ,`famID` ,`studentDOB` ,`studentGrade` ,`studentClass` ,`studentAllergy` ,`studentPhoto` ,`removeDate`) VALUES (:id, :first, :last, :fam, :dob, :grade, :class, :allergy,:photo,:date)";
$statement = $db->prepare($query);
$statement->bindValue(':id',$studentID, PDO::PARAM_INT);
$statement->bindValue(':first',$studentFirst, PDO::PARAM_STR);
$statement->bindValue(':last',$studentLast, PDO::PARAM_STR);
$statement->bindValue(':fam',$studentFam, PDO::PARAM_INT);
$statement->bindValue(':dob',$studentDOB, PDO::PARAM_STR);
$statement->bindValue(':grade',$studentGrade, PDO::PARAM_STR);
$statement->bindValue(':class',$studentClass, PDO::PARAM_STR);
$statement->bindValue(':allergy',$studentAllergy, PDO::PARAM_STR);
$statement->bindValue(':date',$removeDate, PDO::PARAM_STR);
$statement->execute();
///// step 2 - Delete student from studentinfo table //////
$stmt = $db->prepare("DELETE FROM studentinfo WHERE studentID=:id");
$stmt->bindValue(':id', $studentID);
$stmt->execute();
echo $studentFirst . " " . $studentLast . " has been removed from the system (and backed up)";
echo "<p><a href='admin.php'><< Return to administration page</a></p>";
答案 0 :(得分:5)
让我不断怀疑为什么每个人都渴望为每个简单的插页编写几个代码屏幕。
如果你能得到一个准备好插入执行的数组,那么获取对象有什么意义呢?
$row = $statement->fetch(PDO::FETCH_ASSOC));
$row['removeDate'] = date("Y-m-d");
$query = "INSERT INTO exstudentinfo (studentID ,studentFirst,studentLast,famID,studentDOB,studentGrade,studentClass,studentAllergy,studentPhoto,removeDate) VALUES (:id, :first, :last, :fam, :dob, :grade, :class, :allergy, :date)";
$statement = $db->prepare($query);
$statement->execute($row);
没有双重分配和绑定!
更不用说mysql可以让你这样做in a single query
INSERT INTO exstudentinfo SELECT *, CURDATE() as removeDate FROM studentinfo
答案 1 :(得分:2)
您应该添加正确的错误处理,但问题是您要在10个字段中插入9个值,并且您使用字段和表名称的引号:
$query = "INSERT INTO sundayschool.exstudentinfo
(studentID ,studentFirst ,studentLast ,famID ,studentDOB ,studentGrade ,studentClass ,studentAllergy ,studentPhoto ,removeDate)
// 10 fields
VALUES
(:id, :first, :last, :fam, :dob, :grade, :class, :allergy, :date)";
// 9 values, photo is missing
如果你需要引用例如mysql保留字,你需要使用表格或字段名称的反引号:
$query = "INSERT INTO `sundayschool`.`exstudentinfo`
// etc.
答案 2 :(得分:1)
您错过了在studentPhoto
和allergy
之间插入date
的部分。您正在尝试插入10个字段,但您只提供9个信息。
此外,在这里丢失报价。你不需要它们:
$query = "INSERT INTO `sundayschool`.`exstudentinfo` (studentID ,studentFirst ,studentLast ,famID ,studentDOB ,studentGrade ,studentClass ,studentAllergy ,studentPhoto ,removeDate) VALUES (:id, :first, :last, :fam, :dob, :grade, :class, :allergy, :date)";
请务必添加studentPhoto
答案 3 :(得分:0)
乍一看,你的行:
$query = "INSERT INTO 'sundayschool'.'exstudentinfo' ('studentID' ,'studentFirst' ,'studentLast' ,'famID' ,'studentDOB' ,'studentGrade' ,'studentClass' ,'studentAllergy' ,'studentPhoto' ,'removeDate') VALUES (:id, :first, :last, :fam, :dob, :grade, :class, :allergy, :date)";
需要更改为使用反引号而不是单引号:
$query = "INSERT INTO `sundayschool`.`exstudentinfo`
(
`studentID`,
`studentFirst`,
`studentLast`,
`famID`,
`studentDOB`,
`studentGrade`,
`studentClass`,
`studentAllergy`,
`studentPhoto`,
`removeDate`
)
VALUES (
:id,
:first,
:last,
:fam,
:dob,
:grade,
:class,
:allergy,
:photo,
:date
)";