使用PHP获取正确的响应,但MySQL数据库不会更新

时间:2017-09-11 07:46:41

标签: php html mysql phpmyadmin

我正在尝试创建一个简单的注册表单。我有一个连接数据库的文件

conn.php

<?php
 $db_name = "bp_reader";
 $mysql_username = "root";
 $mysql_password = "";
 $server_name = "localhost";
 $conn = mysqli_connect ($server_name, $mysql_username, $mysql_password, $db_name);

/*the connection here is fine*/
if($conn){ echo "connected"; }else{ echo "not connected"; }

?>

注册php

<?php
require "conn.php";

$name = $_POST["name"];
$email = $_POST["email"];
$age = $_POST["age"];
$height = $_POST["height"];
$weight = $_POST["weight"];
$password = $_POST["password"];

//check if user exists
$sql = "select * from user_profile where user_email like '".$email."';";

$result = mysqli_query($conn, $sql);

$response = array();

if(mysqli_num_rows($result) > 0){

    $code = "registration failed";
    $message = "User already exists";


    array_push($response, array("code"=>$code, "message"=>$message));

    echo json_encode($response);

}else {
    $sql = "insert into user_profile values ('".$name."', '".$email."', '".$age."', '".$height."', '".$weight."', '".$password."');";

    $result = mysqli_query($conn, $sql);

    $code = "registration Success";
    $message = "Thank you for registration... you can login now..";

    //jason data
    array_push($response, array("code"=>$code, "message"=>$message));

    echo json_encode($response);
}

mysqli_close($conn);
?>

我有一个简单的html注册表单

<html>

<body>
<form action="register.php" method="post">
    <table>
        <tr>
            <td>Name:</td>
            <td>
                <input type="text" name="name" /> </td>
        </tr>
        <tr>
            <td>Email:</td>
            <td>
                <input type="email" name="email" /> </td>
        </tr>
        <tr>
            <td>DOB:</td>
            <td>
                <input type="date" name="age" /> </td>
        </tr>
        <tr>
            <td>height:</td>
            <td>
                <input type="number" name="height" /> </td>
        </tr>
        <tr>
            <td>weight:</td>
            <td>
                <input type="number" name="weight" /> </td>
        </tr>
        <tr>
            <td>Password:</td>
            <td>
                <input type="password" name="password" /> </td>
        </tr>
        <tr>
            <td>
                <input type="submit" value="Register" /> </td>
        </tr>
    </table>
</form>
</body>
</html>

问题是当我填写表单并提交时,我得到正确的响应,注册成功但是当我在phpMyAdmin上检查我的数据库时,数据库保持不变。我无法弄清楚问题出在哪里。

1 个答案:

答案 0 :(得分:0)

如果你有id字段作为标识符和自动增量,你应该设置它:

   $sql = "insert into user_profile values ('', '".$name."', '".$email."', '".$age."', '".$height."', '".$weight."', '".$password."');";