使用phpMyAdmin将用户添加到php中的MySQL数据库

时间:2016-03-14 05:54:16

标签: php mysql phpmyadmin

我认为我通过以下方式成功连接到我的数据库:

<?php
    $user = 'root';
    $pass = '9KSroMDjEqNmEYY4';
    $db = 'chatservice';
    $host = '127.0.0.1';

    $conn = new mysqli($host, $user, $pass, $db, 3306) or die("Unable to connect");
    if ($conn->connect_error){
        die("Connection failed: " . $conn->connect_error);
    }
?>

我的问题是如何使用注册码成功将用户添加到数据库。当我输入表格时按下注册表我没有收到任何错误消息,说明注册没有成功。似乎初始连接后没有访问php代码。我是php和mySQL的新手,所以关于格式化的任何提示都会很好!

<?php
    require('connect.php');
    if(isset($_POST['user']) && isset($_POST['password'])){
        $user = $_POST['user'];
        $id = $_POST['IDNUM'];
        $password = $_POST['password'];

        $query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', '$id', '$password')";
        $result = mysqli_query($query);
        if($result){
            $msg = "Registered Sussecfully";
            echo $msg;
        }
        else
            $msg = "Error Registering";
            echo $msg;
    }
?>


<div class="register-form">
    <title>Chat Page Start</title>
    <form action="" methods="POST">
        <p>
        <label>Username: </label>
        <input id="user" type="text" name="user" placeholder="user" />
        </p>

        <p>
        <label>ID: </label>
        <input id="IDNUM" type="text" name="IDNUM" placeholder="ID number" />
        </p>

        <p>
        <label>Password: </label>
        <input id="password" type="password" name="password" placeholder="password" />
        </p>

        <a class="btn" href="login.php">Login</a>
        <input class="btn register" type="submit" value="Register" />
    </form>
</div>

另一件事是我如何检查数据库连接的状态以及我应该在哪里检查此状态?

2 个答案:

答案 0 :(得分:0)

您正在使用mysqli连接数据库:

$conn = new mysqli('localhost', $user, $pass, $db, 3306) or die("Unable to connect");

使用mysql执行查询:

$query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', '$IDNUM', '$password')";
        $result = mysql_query($query);

答案 1 :(得分:0)

您的数据库连接是mysqli_connect,并且您在mysql_query中执行查询是不正确的。

 <?php
    require('connect.php');
    if(isset($_POST['user']) && isset($_POST['password'])){
        $user = $_POST['user'];
        $id = $_POST['IDNUM'];
        $password = $_POST['password'];

        $query = "INSERT INTO 'users' (user ,IDNUM ,password) VALUES('$user', ' $id ', '$password')";
        $result = mysqli_query($query,$conn);
        if($result){
            $msg = "Registered Sussecfully";
        }
        else
            $msg = "Error Registering";
    }
?>