簡體   English   中英

無法使用php將表單數據保存到sql表

[英]Unable to save form data to sql table using php

我正在嘗試使用php將表單數據保存到sql表中。 盡管提交時我沒有出現錯誤,但是數據未顯示在表中。

我的提交按鈕名稱是input_submit這是我的代碼:

if(isset($_POST['input_submit'])){

include 'dbConnection.php';
include 'saveData.php'; 

}

dbConnection.php

<?php

$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-load.php';
include_once $path . '/wp-config.php';


class ConnectDB{

    private $servername;
    private $username;
    private $password;
    private $dbname;


    protected function connect(){

        $this->servername ="localhost";
        $this->username ="root";
        $this->password ="";
        $this->dbname ="testdb";

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

            if($conn -> connect_error) {

                die("connection failed:".$conn-> connect_error);
            }

        return $conn;

        }
}

?>

saveData.php:

<?php

    class saveinput extends ConnectDB {

    public function Savein(){

$date       = $_POST['date'];   
$entry_type = $_POST['entry_type'];
$amount     = $_POST['amount'];

    $sql = $conn->prepare("INSERT INTO wp_myexpenses (date, entry_type, amount) 
    VALUES(?, ?, ?)");

$sql->bind_param("sss",$date, $entry_type, $amount);

$sql->execute();

if ($sql->execute()) { 
  echo "success";
} else {
  echo "failed";
}


    }

}


?>

提交時,表單正在提交。 但是當我檢查數據庫表時,什么都沒有顯示。 我不明白這里出了什么問題。 有人可以指導我嗎?

您應該在var的Savein方法中調用“ connect”方法。 因此,您的Savein方法應為:

public function Savein(){
$conn = parent::connect(); // This is the only thing i've added

$date       = $_POST['date'];   
$entry_type = $_POST['entry_type'];
$amount     = $_POST['amount'];

    $sql = $conn->prepare("INSERT INTO wp_myexpenses (date, entry_type, amount) 
    VALUES(?, ?, ?)");

$sql->bind_param("sss",$date, $entry_type, $amount);

if ($sql->execute()) { 
  echo "success";
} else {
  echo "failed";
}


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM