数据未插入MySQL数据库,但没有错误

时间:2017-10-01 12:27:52

标签: php mysql xampp connection sql-insert

首先,我是使用php,XAMPP和SQL的新手。我花了很多时间试图自己解决这个问题,但我没有解决它。

我想将我网站上的输入表单中的数据插入到我的数据库中。当我填写表单时,脚本运行没有抱怨,但没有数据插入我的数据库。 myphpadmin中也没有显示错误,只返回0行。

<?php

if (isset($_POST['submit'])) {

    include_once 'database.php';

    $first_name = mysqli_real_escape_string($conn, $_POST['first_name']);
    $last_name = mysqli_real_escape_string($conn, $_POST['last_name']);
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);
    $pin = mysqli_real_escape_string($conn, $_POST['pin']);

    //Errors
    //Check for NOT NULL
    if (empty($first_name) || empty($last_name) || empty($username) || empty($password))
         {
        header("Location: ../signup.php?signup=empty");
        exit();
    } else {
        //check if input charakters are valid
        if (!preg_match("/^[a-zA-Z]*$/", $first_name) || !preg_match("/^[a-zA-Z]*$/", $last_name)) 
            {
                header("Location: ../signup.php?signup=invalid");
                exit();
            } else {
             $sql = "SELECT * FROM user WHERE username='$username'"; 
             $result = mysqli_query($conn, $sql);
             $resultCheck = mysqli_num_rows($result);

             if ($resultCheck > 0) {
                header("Location: ../signup.php?signup=usertaken");
                exit();
               } else {
                //Hashing Password
                $hashedpassword = password_hash($password, PASSWORD_DEFAULT);
                //insert user into database
                $sql = "INSERT INTO user (first_name, last_name, username, password, pin) VALUES ('$first_name', '$last_name', '$username', '$hashedpassword', '$pin');";
                mysqli_query($conn, $sql);
                header("Location: ../signup.php?signup=success");
                exit();
               }
            }
        }

} else {
    header("location: ../signup.php");
    exit();
}

数据库是使用root,localhost和无PW的标准设置

<?php

$dbServername ="localhost";
$dbUsername ="root";
$dbPassword ="";
$dbName ="ntust";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

?>

由于我对SQL不是很熟悉,我开始怀疑问题是否存在于我的表中?

CREATE TABLE user (
id Bigint(20) Primary Key auto_increment,
username varchar(255) NOT NULL unique,
pin varchar(6) NOT NULL,
password varchar(255) NOT NULL,
first_name varchar(255) NOT NULL,
last_name varchar(255),
last_modified_date datetime NOT NULL,
created_date datetime NOT NULL
);

2 个答案:

答案 0 :(得分:1)

数据库连接     

$dbServername ="localhost";
$dbUsername ="root";
$dbPassword ="";
$dbName ="ntust";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

?>

数据库格式

CREATE TABLE user (
id Bigint(20) Primary Key auto_increment,
username varchar(255) NOT NULL unique,
pin varchar(6) NOT NULL,
password varchar(255) NOT NULL,
first_name varchar(255) NOT NULL,
last_name varchar(255),
last_modified_date datetime NOT NULL,
created_date datetime NOT NULL
);

更新代码

<?php

    if (isset($_POST['submit']) && !empty($_POST['first_name']) && !empty($_POST['last_name']) && !empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['pin'])) {


        if (!preg_match("/^[a-zA-Z]*$/", $_POST['first_name']) || !preg_match("/^[a-zA-Z]*$/", $_POST['last_name'])) {
            # code...
             header("Location: ../signup.php?signup=empty");
             exit();
        }
        elseif (!preg_match("/^[a-zA-Z]*$/", $_POST['first_name']) || !preg_match("/^[a-zA-Z]*$/", $_POST['first_name'])) {
            # code.. check if input charakters are valid
             header("Location: ../signup.php?signup=invalid");
             exit();
        }


         include_once 'database.php';

         $first_name = $_POST['first_name'];
         $last_name = $_POST['last_name'];
         $username = $_POST['username'];
         $password = $_POST['password'];
         $pin =  $_POST['pin'];
         $last_modified_date = date("d-m-Y");
         $created_date = date("d-m-Y");

         $sql = "SELECT * FROM user WHERE username='$username'"; 
                 $result = mysqli_query($conn, $sql);
                 $resultCheck = mysqli_num_rows($result);

                 if ($resultCheck > 0) {
                    header("Location: ../signup.php?signup=usertaken");
                    exit();
                   } else {
                    //md5 Password
                    $md5password = md5($password);
                    //insert user into database
                   $sql = "INSERT INTO user (first_name, last_name, username, password, pin,last_modified_date,created_date) VALUES ('$first_name', '$last_name', '$username', '$md5password', '$pin','$last_modified_date','$created_date ');";
                    mysqli_query($conn, $sql);
                    header("Location: ../signup.php?signup=success");
                    exit();
             }



    }
    else {

    $error = " Fill the all the details first";

    if (isset($_POST['submit']) && isset($error)) {

        echo "$error";
    }

    }   
?>  

注意:当我检查你的数据库时,你有一个last_modified_date datetime NOT NULL,created_date datetime NOT NULL所以,根据这个代码更新它,其他一切都很好!!

答案 1 :(得分:0)

根据您的经验,我会说以下最合适的解决方案。

将您的代码更改为:

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

if (!$res) {
  printf("Errormessage: %s\n", mysqli_error($conn));
}

通过这种方式,您可以检查查询是否有错误

  

我建议您开始寻找简单的代码调试技术,并开始搜索phpmysqli

的相应文档

您随时可以查看mysqli_error() w3schools文档here