php - MySQL 或 PHP PDO 错误

标签 php mysql pdo

我得到一个:

警告: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/

相关文章:

php - 捕获每一行的第一个字

mysql - 从 mysql 中获取 mm/dd/yy 格式的记录

mysql - 仅针对客户、项目和发票的 Consolibyte QuickBooks 桌面导入表

php - 从 mysql 迁移到 PDO,不连接到 mysql 服务器

php - 首先获取人名,然后根据该人名检索外部数据 COUNT

php - Joomla 插件 : "Another plugin is already using the named folder"

php - 爬取页面的不同语言,这取决于php中的 session

php - 数据库查询后将变量从一个 PHP 页面发送到另一页面

mysql - jdbc4.MySQLSyntaxErrorException : Unknown database

php - 准备好的语句不适用于 ALTER 表查询