繁体   English   中英

MySQL或PHP PDO错误

[英]MySQL or PHP PDO error

我得到一个:

警告:PDOStatement :: execute()[pdostatement.execute]:SQLSTATE [23000]:违反完整性约束:1048在/home/content/49/11554349/html/gb/dev/post.php中,列'classnum'不能为null 66行

这也是它所引用的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.";   
    }
}

这些是我做的功能,一种是检查表单是否完整填写,另一种是将数据实际发布到数据库中。

这就是我在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的参数,但您正在绑定$ classnum。 由于没有$ classnum,因此可能会将null传递给该语句。

$stmt->bindParam(2,$classname);

我认为该函数调用存在问题,请打印出发送给该函数的数据以确保其不为null。 如果可以的话,将其张贴在这里或摆弄

你有错误:

public function CheckIfEmptyTutor($class,$classnum,$studentyear){
    if(empty($class) && empty($classname) && empty($studentyear)){

这里应该是

public function CheckIfEmptyTutor($class,$classnum,$studentyear){
    if(empty($class) || empty($classnum) || empty($studentyear)){

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM