0

我正在尝试从数据库中的一个表中获取记录并将其放在另一个表中(作为数据的备份 - 需要保留 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 中...我使用了普通的撇号 (') 而不是正确的反引号 (`) - 我忘记在绑定值中包含 photo 字段。

$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'>&lt;&lt; Return to administration page</a></p>";
4

4 回答 4

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可以让你在一个查询中做到这一点

INSERT INTO exstudentinfo SELECT *, CURDATE() as removeDate FROM studentinfo
于 2013-03-07T13:40:40.657 回答
2

您确实应该添加正确的错误处理,但问题是您想将 9 个值插入 10 个字段,并且您在字段和表名中使用引号:

$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.
于 2013-03-07T13:19:31.830 回答
1

您缺少在和studentPhoto之间插入的部分。您尝试插入 10 个字段,但您只提供 9 个字段的信息。allergydate

另外,在这里丢失引号。你不需要它们:

$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

于 2013-03-07T13:20:06.410 回答
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
)";
于 2013-03-07T13:23:01.050 回答