将值插入MYSQL数据库

时间:2017-04-16 12:02:42

标签: php mysql forms

我尝试使用OOP概念在PHP中编写Web应用程序,并构建了一个类和一个处理文件来将学生对象添加到数据库中。但它不起作用。这是我的代码。我编写了一个扩展User类的Student类。这是User类。

<?php
 class User
  {
  private $firstName;
  private $lastName;
  private $email;
  private $gender;
  private $phoneNumber;
  private $profilePicture;

  public function getFirstName()
  {
      return $this->firstName;
  }

  public function getLastName()
  {
      return $this->lastName;
  }

  public function getEmail()
  {
      return $this->email;
  }

  public function getPassword()
  {
      return $this->password;
  }

  public function getGender()
  {
      return $this->gender;
  }

  public function getPhoneNumber()
  {
      return $this->phoneNumber;
  }

  public function getProfilePicture()
  {
      return $this->profilePicture;
  }
  public function setFirstName($firstName)
  {
      $this->firstName = $firstName;
  }

  public function setLastName($lastName)
  {
      $this->lastName = $lastName;
  }

  public function setEmail($email)
  {
      $this->email = $email;
  }

  public function setPassword($password)
  {
      $this->password = $password;
  }

  public function setGender($gender)
  {
      $this->gender = $gender;
  }

  public function setPhoneNumber($phoneNumber)
  {
      $this->phoneNumber = $phoneNumber;
  }

  public function setProfilePicture($profilePicture)
  {
      $this->profilePicture = $profilePicture;
  }
}

然后是学生班

<?php
include '../Functions/mainfunction.php';
include 'User.php';
class Student extends User
{
  private $matricNumber;
  private $level;
  private $faculty;
  private $department;
  private $unit;
  private $result;
  private $friends;
  public function getMatricNumber()
  {
      return $this->matricNumber;
  }

  public function getLevel()
  {
      return $this->level;
  }

  public function getFaculty()
  {
      return $this->faculty;
  }

  public function getDepartment()
  {
      return $this->department;
  }

  public function getUnit()
  {
      return $this->unit;
  }

  public function getResult()
  {
      return $this->result;
  }

  public function getFriends()
  {
      return $this->friends;
  }

  public function setMatricNumber($matricNumber)
  {
      $this->matricNumber = $matricNumber;
  }

  public function setFaculty($faculty)
  {
      $this->faculty = $faculty;
  }

  public function setDepartment($department)
  {
      $this->department = $department;
  }

  public function setUnit($unit)
  {
      $this->unit = $unit;
  }

  public function setResult($result)
  {
      $this->result = $result;
  }

  public function setFriends($friends)
  {
      $this->friends = $friends;
  }

  public function setLevel($level)
  {
      $this->level = $level;
  }


  public static function createStudent($student, $password)
  {
      //Open a Database Connection
        $connect = dbcon();
        //Get Student object fields from the create user page
        $firstName = $student->getFirstName();
      $lastName =$student->getLastName();
      $email = $student->getEmail();
      $gender = $student->getGender();
      $faculty = $student->getFaculty();
      $department = $student->getDepartment();
      $unit = $student->getUnit();
      $level = $student->getLevel();
        //Encrypt Password
        $encryptedPassword = password_hash($password, PASSWORD_DEFAULT);

      $matricNumber = $student->getMatricNumber();

        //Insert User to database
        $query = 'INSERT INTO `student` (`First Name`, `Last Name`, `Gender`, `Email Address`, `Password`, `Faculty`, `Department`, `Unit`, `Matric Number`, `Level`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
      $statement = $connect->prepare($query);
      $connect->prepare($query)->bind_param('sssssssssi', $firstName, $lastName, $gender, $email, $encryptedPassword, $faculty, $department, $unit, $matricNumber, $level);
      if ($statement->execute()) {
          $connect->close();
          //  echo $encryptedPassword . 'true';
            return true;
      } else {
          $connect->close();
          //  echo $encryptedPassword . 'false';
            return false;
      }
  }
}

这是添加的代码。

<?php
require('../Classes/Student.php');
$student = new Student;
$firstName  = htmlspecialchars($_POST['firstName']);
$lastName = htmlspecialchars($_POST['lastName']);
$email = htmlspecialchars($_POST['email']);
$matricNumber = htmlspecialchars($_POST['matricNumber']);
$faculty = htmlspecialchars($_POST['faculty']);
$department = htmlspecialchars($_POST['department']);
$unit = htmlspecialchars($_POST['unit']);
$level = htmlspecialchars($_POST['level']);
$gender = htmlspecialchars($_POST['gender']);
$password = htmlspecialchars($_POST['password']);

$student->setFirstName($firstName);
$student->setLastName($lastName);
$student->setEmail($email);
$student->setMatricNumber($matricNumber);
$student->setFaculty($faculty);
$student->setDepartment($department);
$student->setUnit($unit);
$student->setLevel($level);
$student->setGender($gender);

$created = $student::createStudent($student, $password);
if ($created) {
 redirect('../Signed_in/Index.php');
}else{
 redirect('../index.php');

}

最后,我的数据库连接代码和我定义的方法&#34;重定向&#34;

<?php

function redirect($url){
 header("Location:{$url}");
 exit;

}

function dbcon(){
 $connect = new mysqli('localhost', 'username', 'password', 'database-name');
 return $connect;

 }
你可以告诉我哪里出错了吗?它没有给出任何错误信息。这是说我的bind_param函数首先给出了一个布尔值。但是现在代码只是将我重定向到索引页面,其中else语句应该引导您,并且学生没有被添加到我的数据库中,它也没有告诉我原因。 如果有必要,这是我的HTML表单

            <form class="sign_up_form" method="post" action="Logic/create-student.php">
          <input class="sign_up_input" name="firstName" placeholder="First Name" />
          <input class="sign_up_input" name="lastName" placeholder="Last Name" />
          <input class="sign_up_input long" name="email" placeholder="Email Address" />
          <input class="sign_up_input long" name="password" placeholder="Password" type="password" />
          <input class="sign_up_input long" name="matricNumber" placeholder="Matric Number" />
          <input class="sign_up_input" name="faculty" placeholder="Faculty" />
          <input class="sign_up_input" name="department" placeholder="Department" />
          <input class="sign_up_input" name="unit" placeholder="Unit" />
          <select name="level" class="sign_up_input">
            <option>100</option>
            <option>200</option>
            <option>300</option>
            <option>400</option>
            <option>500</option>
          </select>
          <select name="gender" class="sign_up_input">
            <option>Male</option>
            <option>Female</option>
          </select>
          <input class="sign_up_input" placeholder="Validation Pin" />
          <input class="sign_up_submit sign_up_input long" type="submit" value="Sign Up" />
          <div class="special_hr_or long">
            <span class="special_text">Or</span>
          </div>
        </form>

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。我修复了我的代码的三个问题,他们更多的是语法错误而不是逻辑错误。第一个是我使用静态函数&#34; createStudent&#34;我实例化的$ student对象而不是学生类。第二个是我给了一个错误的表名,第三个,我在查询中插入值的列不是我表中的唯一列。我必须在我的表中设置我没有将值插入null的列的默认值,并在我的查询中将它们设置为null,一切正常。我希望这可以帮助那里的人。