0

我想在 PHP 中使用 OOP 插入学生信息。我尝试了以下查询,但未执行 mysql_query。任何人都请帮助我:我的代码如下:

1.数据库连接:

define("host", "something@mail.com");
define("username", "ff");
define("password", "ii");
define("database", "school");
class DbConnection {
    private $connectionObj;

    Public function getConnection(){
    $this->connectionObj=mysql_connect(host, username, password);   

    //use the database
    mysql_select_db(database, $this->connectionObj);

    if (!$this->connectionObj)die("connection to the database not established");

    return $this->connectionObj;
    }
}

2.业务逻辑:

class studentModel{
    private $connection;

    function _construct() {
        if (!$this->connection) {
            $getConnection = new DbConnection();
            $this->connection=$getConnection->getConnection();
        }
    }
function createStudent(Student $student) {
    $query="insert into student_table(student_id, first_name, last_name, dob, gender, email, phone, year, medical) values
            ('".$student->getStudentId()."','".$student->getFirstName()."', '".$student->getLastName()."', '".$student->getDob()."', '".$student->getGender()."', '".$student->getEmail()."',
    '".$student->getPhone()."', '".$student->getYear()."', '".$student->getMedical()."');";
    $result= mysql_query($query);
    //echo $query;
    if(!mysql_query($query)){
        die("query cannot be executed.");
    }
    return true;    

}
}

3.控制器

include_once '../model/createStudent.php';
include_once '../config/Student.php';
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$dob=$_POST['dob'];
$gender=$_POST['gender'];
$dob=$_POST['dob'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$year=$_POST['year'];
$medical=$_POST['medical'];
$student= new Student();
$student->setFirstName($fname);
$student->setLastName($lname);
$student->setDob($dob);
$student->setGender($gender);
$student->setEmail($email);
$student->setPhone($phone);
$student->setYear($year);
$student->setMedical($medical);
$model= new studentModel();
$result=$model->createStudent($student);
if (!$result)die("problem while executing the query.");
header('Location: ../view/display_students.php');
4

0 回答 0