如何使用php将数据插入mysql表

时间:2018-04-08 10:38:34

标签: php mysql

我正在尝试使用PHP在MySQL表中输入数据。以下是我的代码。我错过了什么?原因是,没有插入数据。

数据库连接。

<?php
class Dbh{
    private $servername = "localhost";
    private $username = "username";
    private $password = "password";
    private $dbname = "oop";
    protected function connect(){
        $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
        return $conn;
    }
}
?>

插入用于将数据插入MySql表的类

<?php
include_once 'Dbh.php';
class Insert{
    public function insertData(){
        $uid = $_POST['uid'];
        $pwd = $_POST['pwd'];

        $sql = "INSERT INTO user(uid, pwd) VALUES ('uid', 'pwd')";
        mysqli_query($sql);
        header("Location: ../index.php?data=inserted");
    }
}
?>

用于输入数据的HTML表单。

<?php
include_once('Insert.php');
$data = new Insert();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Insert data into database</title>
</head>

<body>
<form action="classes/Insert.php" method="POST">
    <input type="text" name="uid" placeholder="user name">
    <br>
    <input type="password" name="pwd" placeholder="password">
    <br>
    <button type="submit" name="submit">Insert data</button>
</form>
</body>
</html>

0 个答案:

没有答案