无法在数据库表中插入值

时间:2017-03-29 05:26:43

标签: php database oop mysqli

我可以在数据库中建立连接,我可以创建表但是我不能在该表中插入任何值, 我的代码是:

<?php
if (!file_exists('config.php')) {
    header('Location: install.php?page=introduction');
}
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo enqueue_style('install', GET_URI_DIRECTORY . '/includes/css/install.css', '1.0');
echo enqueue_style('bootstrap', GET_URI_DIRECTORY . '/includes/css/bootstrap.min.css', '3.3.7');
echo enqueue_style('merriwheather-font', GET_URI_DIRECTORY . '/includes/css/font/merriwheather.css');
?>
<div class="container install-container">
    <div class="panel-group">
        <div class="panel panel-primary">
            <div class="panel-heading">CONFIGURATION</div>
            <div class="panel-body">
                Here you need to submit some information related to your login and site
                <form method="post" class="form-horizontal">
                    <div class="form-group">
                        <label class="control-label col-sm-2" for="fname">First Name</label>
                        <div class="col-sm-10">
                            <input type="text" name="fname" class="form-control" id="fname" placeholder="First Name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2" for="lname">Last Name</label>
                        <div class="col-sm-10">
                            <input type="text" name="lname" class="form-control" id="lname" placeholder="Last Name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2" for="uname">User Name</label>
                        <div class="col-sm-10">
                            <input type="text" name="uname" class="form-control" id="uname" placeholder="User Name for login">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2" for="pwd">Password</label>
                        <div class="col-sm-10">
                            <input type="text" name="pwd" class="form-control" id="pwd" placeholder="Password for login">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2" for="sname">Site Name</label>
                        <div class="col-sm-10">
                            <input type="text" name="sname" class="form-control" id="sname" placeholder="Site Name">
                        </div>
                    </div>

                    <button type="submit" class="btn btn-primary btn-block">Submit</button>
                    <b>Note:-</b> <small>This will create your first admin account</small>
                </form>
            </div>
        </div>
    </div>
</div>
<?php

if (isset($_POST['fname'])) {
    $firstname = $_POST['fname'];
}
if (isset($_POST['lname'])) {
    $lastname = $_POST['lname'];
}
if (isset($_POST['uname'])) {
    $username = $_POST['uname'];
}
if (isset($_POST['pwd'])) {
    $password = password_hash($_POST['pwd'], PASSWORD_DEFAULT);
}
$capability = 'admin';
if (isset($_POST['sname'])) {
    $sitename = $_POST['sname'];
}

if (isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['uname']) && isset($_POST['pwd']) && isset($_POST['sname'])) {
    if (execute_table_making_sql() === true) {

        function configure_admin_login() {
            global $dbconn, $table_prefix, $firstname, $lastname, $username, $password, $capability;

            $firstname = mysqli_real_escape_string($firstname);
            $lastname = mysqli_real_escape_string($lastname);
            $username = mysqli_real_escape_string($username);
            $password = mysqli_real_escape_string($password);
            $capability = mysqli_real_escape_string($capability);

            $query = "INSERT INTO '{$table_prefix}'users (`FirstName`, `LastName`, `Capability`, `UserName`, `Password`) VALUES ('{$firstname}', '{$lastname}', '{$capability}', '{$username}', '{$password}')";

            if ($dbconn->query($query) === true) {
                return true;
            }
            $dbconn->error;
        }

        configure_admin_login();
    }
}
echo enqueue_script('bootstrap', GET_URI_DIRECTORY . '/includes/js/bootstrap.min.js', '3.3.7');
echo enqueue_script('jquery', GET_URI_DIRECTORY . '/includes/js/jquery-3.1.1.min.js', '3.1.1');
echo enqueue_script('smoothscroll', GET_URI_DIRECTORY . '/includes/js/smoothscroll.js', '1.2.1');

和制作表格的代码是:

if (file_exists('config.php')) {
    $dbconn = database::dbconn($host, $username, $password, $database);
    if ($dbconn !== false) {
        function execute_table_making_sql() {
            global $dbconn, $table_prefix;
            $sql = 'CREATE TABLE '.$table_prefix.'users (
                    FirstName varchar(50) NOT NULL,
                    LastName varchar(50) NOT NULL,
                    Capability varchar(20) DEFAULT "member" NOT NULL,
                    UserName varchar(75) NOT NULL,
                    Password varchar(100) NOT NULL
                    )';
            if($dbconn->query($sql) === true) {
                return true;
            }
        }
    }
}

class database {

    static function dbconn($host, $user, $pass, $db) {
        $connection = new mysqli($host, $user, $pass, $db);

        if ($connection->connect_error) {
            echo "Connection failed: " . $connection->connect_error;
            return false;
        }
        return $connection;
    }
}

我可以在这里制作表格,所以我的数据库连接是真的,但是我不能在表格中插入任何值。我的代码有什么问题请告诉我。帮我调试这段代码。提前谢谢!

注意: 我做的一些常量不是错误,在使用前已成功检查

1 个答案:

答案 0 :(得分:0)

你的问题是你在每次原因没有返回true时创建表。用这个。

页面顶部。

<?php

error_reporting(E_ALL);
if (file_exists('config.php')) {
    $dbconn = database::dbconn($host, $username, $password, $database);
    if ($dbconn !== false) {
        function execute_table_making_sql() {

            global $dbconn, $table_prefix;
            $sql = 'CREATE TABLE '.$table_prefix.'users (
                    FirstName varchar(50) NOT NULL,
                    LastName varchar(50) NOT NULL,
                    Capability varchar(20) DEFAULT "member" NOT NULL,
                    UserName varchar(75) NOT NULL,
                    Password varchar(100) NOT NULL
                    )';
            if(mysqli_num_rows($dbconn->query("SHOW TABLES LIKE '".$table_prefix."users'")) !=1) {
                $dbconn->query($sql);
            }
            return true;
        }
    }
}


class database {

    static function dbconn($host, $user, $pass, $db) {
        $connection = new mysqli($host, $user, $pass, $db);

        if ($connection->connect_error) {
            echo "Connection failed: " . $connection->connect_error;
            return false;
        }
        return $connection;
    }
}

?>

在此代码中只更新mysqli_real_escape_string,因为它除了两个参数。

        if (isset($_POST['fname'])) {
            $firstname = $_POST['fname'];
        }
        if (isset($_POST['lname'])) {
            $lastname = $_POST['lname'];
        }
        if (isset($_POST['uname'])) {
            $username = $_POST['uname'];
        }
        if (isset($_POST['pwd'])) {
            $password = password_hash($_POST['pwd'], PASSWORD_DEFAULT);
        }
        $capability = 'admin';
        if (isset($_POST['sname'])) {
            $sitename = $_POST['sname'];
        }

        if (isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['uname']) && isset($_POST['pwd']) && isset($_POST['sname'])) {

            if (execute_table_making_sql() === true) {

                function configure_admin_login() {
                    global $dbconn, $table_prefix, $firstname, $lastname, $username, $password, $capability;
                    $firstname = mysqli_real_escape_string($dbconn, $firstname);
                    $lastname = mysqli_real_escape_string($dbconn, $lastname);
                    $username = mysqli_real_escape_string($dbconn, $username);
                    $password = mysqli_real_escape_string($dbconn, $password);
                    $capability = mysqli_real_escape_string($dbconn, $capability);

                    $query = "INSERT INTO users (`FirstName`, `LastName`, `Capability`, `UserName`, `Password`) VALUES ('{$firstname}', '{$lastname}', '{$capability}', '{$username}', '{$password}')";

                    if ($dbconn->query($query) === true) {
                        return true;
                    }
                    $dbconn->error;
                }

                configure_admin_login();
            }
        }