简体   繁体   English

MySQL或PHP PDO错误

[英]MySQL or PHP PDO error

Im getting a: 我得到一个:

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

This is the php it refers too: 这也是它所引用的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.";   
    }
}

Those are the functions I made, 1 is to check if the form is completely filled out, the other to actually post the data on the database. 这些是我做的功能,一种是检查表单是否完整填写,另一种是将数据实际发布到数据库中。

And this is how im calling it in the html. 这就是我在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);
    }
}    

Your function is accepting a parameter called $classname but you are binding $classnum. 您的函数接受一个名为$ classname的参数,但您正在绑定$ classnum。 Since there is no $classnum it is probably passing null into the statement. 由于没有$ classnum,因此可能会将null传递给该语句。

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

I think the function call has a problem , print out the data you sent to the function to make sure it's not null . 我认为该函数调用存在问题,请打印出发送给该函数的数据以确保其不为null。 Post it here or an a fiddle if you can 如果可以的话,将其张贴在这里或摆弄

you have error: 你有错误:

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

here should be 这里应该是

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