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');