如何使用PHP将数据插入MySQL数据库?

时间:2016-10-29 23:59:46

标签: php mysql database post

我理解这可能不是最有建设性的问题而且我道歉(我是StackOverflow的新手,还在试图弄清楚如何使用它。)基本上我正在尝试创建一个基本的用户管理系统用户可以登录,但无法注册新帐户。只有管​​理员帐户才能添加新员工(用户)。我可以想出那个部分,只需要帮助我的插入脚本。请告诉我哪里出错了。

这是我收到的错误:

  

警告:mysql_query()要求参数1为字符串,对象在第14行的C:\ xampp \ htdocs \ functions \ addemployee.php中给出   错误:

我的表格:

 <?php
    include('../includes/session.php');
    ?>
<html>
    <?php require('../includes/header.php'); ?>
    <body>
        <?php require('../includes/navbar.php'); ?>
        <div class="container">
            <h2>New Employee</h2>
            <!-- Trigger the modal with a button -->
            <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">New Employee</button>
            <!-- Modal -->
            <div class="modal fade" id="myModal" role="dialog">
                <div class="modal-dialog">
                    <!-- Modal content-->
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">New Employee</h4>
                        </div>
                        <div class="modal-body">
                            <form class="form-horizontal" action="../functions/addemployee.php" method="post">
                                <fieldset>
                                    <!-- Text input-->
                                    <div class="form-group">
                                        <label class="col-md-4 control-label" for="firstname">First Name</label>
                                        <div class="col-md-4">
                                            <input id="firstname" name="firstname" type="text" placeholder="First Name" class="form-control input-md" required="">
                                        </div>
                                    </div>
                                    <!-- Text input-->
                                    <div class="form-group">
                                        <label class="col-md-4 control-label" for="lastname">Last Name</label>
                                        <div class="col-md-4">
                                            <input id="lastname" name="lastname" type="text" placeholder="Last Name" class="form-control input-md">
                                        </div>
                                    </div>
                                    <!-- Text input-->
                                    <div class="form-group">
                                        <label class="col-md-4 control-label" for="username">Username</label>
                                        <div class="col-md-4">
                                            <input id="username" name="username" type="text" placeholder="Username" class="form-control input-md">
                                        </div>
                                    </div>
                                    <!-- Password input-->
                                    <div class="form-group">
                                        <label class="col-md-4 control-label" for="password">Password</label>
                                        <div class="col-md-4">
                                            <input id="password" name="password" type="password" placeholder="Password" class="form-control input-md">
                                        </div>
                                    </div>
                                    <!-- Button -->
                                    <div class="form-group">
                                        <label class="col-md-4 control-label" for="submit"></label>
                                        <div class="col-md-4">
                                            <button id="submit" name="submit" class="btn btn-primary">Add Employee</button>
                                        </div>
                                    </div>
                                </fieldset>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

我的插入脚本:

  <?php

 include("../includes/config.php");


$firstname = mysqli_real_escape_string($db,$_POST['firstname']);
$lastname = mysqli_real_escape_string($db,$_POST['lastname']);
$username = mysqli_real_escape_string($db,$_POST['username']);
$password = mysqli_real_escape_string($db,$_POST['password']);


$sql = "INSERT INTO employees (id, firtname, lastname, username, password) VALUES ('', '$firstname', '$lastname', '$username', '$password')";

if (!mysql_query($db,$sql)) {
    die('Error: ' . mysql_error());
}

?>

我的config / db连接脚本

    <?php
   define('DB_SERVER', 'localhost');
   define('DB_USERNAME', 'root');
   define('DB_PASSWORD', '');
   define('DB_DATABASE', 'breitinger');
   $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

0 个答案:

没有答案