如何解决php数据库连接错误?

时间:2019-02-17 11:46:50

标签: php forms

我正在使用oops概念开发具有数据库连接的表单。 我所有的代码都是正确的,但是我找不到错误。 这是我的index.php。

<form class="" method="post" action="" enctype="multipart/form-data" id="">
<label for="name-element">Name</label>
<input type="text" class="" id="" name="rSName"  value=""/>
<label for="mob-element">Name</label>
<input type="text" class="" id="" name="rsMob"  value=""/>
<label for="date-element">Name</label>
<input type="text" class="" id="" name="rsDate"  value=""/>
<label for="email-element">Name</label>
<input type="text" class="" id="" name="rSEmail"  value=""/>
<label for="birdath-element">Name</label>
<input type="text" class="" id="" name="rsBirDate"  value=""/>
<label for="submit-element">Name</label>
<input type="submit" class="" id="" name="submit"  value="submit"/>

这是我的database.php。

class database
{
    private $servername;
    private $username;
    private $password;
    private $database;


    protected function connect()
    {
        $this->servername = "localhost";
        $this->username = "root";
        $this->password = "";
        $this->database = "ooss";

        $conn = new mysqli($this->servername, $this->username, $this->password, $this->database);

        return $conn;


}
} 

并且我有一个用于将数据存储为store.php的文件。

<?php

include "database.php";

/**Store all the data into the database
 * 
 */
class store extends database
{

    public function storeAllData($FullName,  $BirthDay, $Email,$Date,$Mobile)
    {
        $sql = "INSERT INTO feedback(rSName,  rsBirDate, rSEmail,rsDate,rsMob)VALUES('$FullName',  '$BirthDay', '$Email','$Date','$Mobile')";
        $result  = $this->connect()->query($sql);

        return $result;

    }
}

?>

此代码位于index.php文件的顶部。

include 'store.php';
$stores = new store();
if(isset($_POST['submit']))
{
  $FullName = $_POST['rSName'];   
  $BirthDay = $_POST['rsBirDate'];  
  $Email = $_POST['rSEmail']; 
  $Mobile = $_POST['rsMob'];  
  $Date = $_POST['rsDate'];  

 $res = $stores->storeAllData($FullName, $BirthDay, $Email, $Date,$Mobile);
 if($res){
  echo "Successfully inserted";
 }  else {
  echo " Not successfull";
 }  

我运行这段代码,并获得“不成功”的字样。

即使我的代码很好用也很容易,这有点奇怪。

0 个答案:

没有答案