数字海洋邮政上的PHP没有插入MySQL数据库

时间:2017-11-20 11:42:23

标签: php mysql post mysqli digital-ocean

我创建了一个LAMP Digital Ocean Droplet并在/ var / www / html中放置了一些PHP文件。我一直试图用Postman测试一个简单的Register.php,但它总是返回“Some error”。代码如下:

    <?php
    require("password.php");

    $host = 'localhost'; 
    $user = 'user';
    $pass = 'password';
    $db = 'db';

    $con=mysqli_connect($host,$user,$pass,$db);
    // Check connection
    if ($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    } 

    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $dob = $_POST['dob'];
    $gender = $_POST['gender'];

        global $connect, $name, $email, $password, $dob, $gender;
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);

        $statement = mysqli_prepare($con, "INSERT INTO User(NAME, EMAIL, PASSWORD, DOB, GENDER) VALUES(?,?,?,?,?)");
        mysqli_stmt_bind_param($statement, "sssss", $name, $email, $passwordHash, $dob, $gender);

        if ($result = $con->query($statement))
        { 
            echo json_encode("New record created successfully");
        } 
        else 
        {
            echo json_encode("Some error");

        }
?>

我的PHP文件是否存在问题,或者是否需要一些先决条件才能在Digital Ocean上发布到PHP文件。

编辑:

我已将PHP代码更新为以下内容,但现在在Postman中没有返回任何内容:

    require("password.php");

    $host = 'localhost'; 
    $user = 'user';
    $pass = 'password';
    $db = 'db';

    $con=mysqli_connect($host,$user,$pass,$db);
    // Check connection
    if ($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    } 

    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $dob = $_POST['dob'];
    $gender = $_POST['gender'];

        global $connect, $name, $email, $password, $dob, $gender;
        $passwordHash = password_hash($password, PASSWORD_DEFAULT);


        $statement = $mysqli->prepare("INSERT INTO User(NAME, EMAIL, PASSWORD, DOB, GENDER) VALUES(?,?,?,?,?)");
        $statement->bind_param("sssss", $name, $email, $passwordHash, $dob, $gender););


        if ($result = $con->execute($statement))
        { 
            echo json_encode("New record created successfully");
        } 
        else 
        {
            echo json_encode("Some error");

        }

?>

0 个答案:

没有答案