dongzhangji4824 2017-02-24 21:43
浏览 28

尝试将数据插入数据库PHP PDO

I've tried everything and just cant seem to get this working.. it's probably a silly mistake I can't see but any help is appreciated.

As stated in the question I'm trying to insert records into a table via a form. I have a functions.php which includes my database.php with the pdo connection (all working fine) class with the following function in it:

function insertStaffUser($username, $password, $role) {

    include('database.php');
    try {
        $query = "INSERT INTO users (userid, username, password, role) VALUES (default, :username, :password, :role)";
        $stmt->$db->prepare($query);
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':password', $password);
        $stmt->bindParam(':role', $role);

        $result = $stmt->execute();
        if($result) {
            echo "INSERTED SUCCESSFULLY";
        } else {
            echo "error inserting";
        }
    } catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
    }
}

And the following code is the one in my html class which is addUser.php with 3 text fields (new username, password and role).

<?php
if(isset($_POST['submit'])) {
    $new_username = $_POST['username'];
    $new_pass = $_POST['password'];
    $new_role = $_POST['role'];
    insertStaffUser($new_username, $new_pass, $new_role);
}
?>

Can anyone see what's wrong with this or what I'm doing wrong, thanks for the help!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。
    • ¥15 livecharts wpf piechart 属性
    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了