如何在PHP中修复null prepare()函数

时间:2019-04-16 18:38:34

标签: php mysqli server registration

我正在设置应连接到数据库的Android应用程序。我正在为注册界面编写php脚本,但始终收到此错误:

  

“未捕获的错误:在/opt/lampp/htdocs/Android/includes/DbOperations.php:20中对成员函数prepare()的调用为null   堆栈跟踪:

     

0 /opt/lampp/htdocs/Android/v1/RegistrationUser.php(27):DbOperations-> createStudent('GDS0000','5F002','Gloria','Desideri','F','07 / 10/2000”,“ gloriadesideri0 ...”,“ ---- ++++++”)

     

1 {main}

     

第20行上的/opt/lampp/htdocs/Android/includes/DbOperations.php中抛出的

我正在使用 xampp 连接到运行MySql 5.7的 IONOS 服务器 我已经尝试在本地函数中同时声明 $ con $ db ,但是没有用。我正在用邮递员来测试我的代码。

  <?php
      class DbOperations{
  private $con;

  public function _construct()
  {
     require_once dirname(_FILE_).'/DbConnect.php';

     $db= new DbConnect();

     $this->con = $db->connect();
  }


    public function createStudent($IDStudente, $codClasse, 
       $Nome, $Cognome, $Sesso, $DataNascita, $email, $Password)
    {

       $password= md5($Password);

        if($stmt = $this->con->prepare("INSERT INTO Studenti ('IDStudente', 'codClasse', 'Nome', 'Cognome', 'Sesso' , 'DataNascita', 'email', 'Password') VALUES (? , ?, ?, ?, ?, ?, ?, ?);"))
        {
            $stmt->bind_param("ssssssss", $IDStudente, $codClasse, $Nome, $Cognome, $Sesso, $DataNascita, $email, $Password );

        if($stmt->execute())
        {
            return true;
        }
        else{
            return false;
        }
    }

    else
    {
        $error = $mysqli->errno . ' ' . $mysqli->error;
    echo $error;

    }
    }

}

?>

这是DBOperation类,它调用方法

  if($_SERVER['REQUEST_METHOD']=='POST'){
    if(isset($_POST['IDStudente']) and 
        isset($_POST['codClasse']) and 
        isset($_POST['Nome']) and 
        isset($_POST['Cognome']) and 
        isset($_POST['Sesso']) and
        isset($_POST['DataNascita']) and
        isset($_POST['email'])and
        isset($_POST['Password']))
        {

        $db= new DbOperations();

            if($db -> createStudent(
                $_POST['IDStudente'],
                $_POST['codClasse'],
                $_POST['Nome'],
                $_POST['Cognome'],
                $_POST['Sesso'],
                $_POST['DataNascita'],
                $_POST['email'],
                $_POST['Password']
            )){
                $response['error']=false;
                $response['message']="Studente registrato";
            }
            else
            {
                $response['error']=true;
                $response['message']="Studente non registrato";
            }

    }

这是注册中给我带来麻烦的部分。

我期望输出“ Studente registrato”,并且由于我在测试中输入了所有值,因此用户实际上出现在数据库中。但是我一直收到这个错误。

编辑

我通过将$ stmt-> bind_param放在if语句中来处理错误,但是现在我无法让用户注册。我没有收到任何错误,但无法在表中插入值。我更新了代码。

0 个答案:

没有答案
相关问题