Phi Lipp Phi Lipp - 2 years ago 109
MySQL Question

Data not inserted in MySQL database, but there's no error

First of all, I am new to the use of php, XAMPP and SQL. I put a lot of time in trying to solve this problem on my own but nothing I did solved it.

I want to insert data from a input form on my website into my Database. When I fill out the form, the script runs without complaints but no data is inserted into my database. Also no errors shown in myphpadmin, simply 0 rows returned.

<?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();
}


the database is standard setup with root, localhost and no PW

<?php

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

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

?>


As I am also not very familiar with SQL, I start to wonder if the problem lies in my tables?

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
);

Answer Source

Database Connection

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

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

?>

Database Format

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
);

Updated Code

<?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";
    }

    }   
?>  

Note: as i checked your database you, have an last_modified_date datetime NOT NULL, and created_date datetime NOT NULL So, Update it Based on this code, other every things is fine !!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download