我得到一个:
警告:PDOStatement::execute() [pdostatement.execute]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'classnum' cannot be null in/home/content/49/11554349/html/gb/dev/第 66 行的 post.php
这也是它引用的 php:
public function CheckIfEmptyTutor($class,$classnum,$studentyear){
if(empty($class) && empty($classname) && empty($studentyear)){
echo "All fields are requiered to create the post.";
return true;
}
else{
return false;
}
}
public function TutorPost($class,$classnum,$studentyear){
$stmt = $this->db->prepare("INSERT INTO tutors(class, classnum, studenyear) VALUES(?,?,?)");
$stmt->bindParam(1,$class);
$stmt->bindParam(2,$classnum);
$stmt->bindParam(3,$studentyear);
$stmt->execute();
if($stmt->rowCount() == 1){
echo "Successfully posted!";
}
//For testing purposes.
else{
echo "Something went wrong.";
}
}
这些是我做的功能,1是检查表单是否完整填写,另一个是实际将数据发布到数据库中。
这就是我在 html 中调用它的方式。
if(isset($_POST["submit"])){
$class = $_POST["class"];
$classnum = $_POST["classnum"];
$studentyear = $_POST["studentyear"];
$newpost = new UserPost();
if(!$newpost->CheckIfEmptyTutor($class,$classnum,$studentyear)){
$newpost->BookBoardPost($class,$classnum,$studentyear);
}
}
最佳答案
您的函数正在接受一个名为 $classname 的参数,但您正在绑定(bind) $classnum。由于没有 $classnum,它可能将 null 传递到语句中。
$stmt->bindParam(2,$classname);
关于php - MySQL 或 PHP PDO 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20316956/