简体   繁体   中英

Cannot insert data from PHP into MYSQL server from a Registration Form

I've been trying to get a set of code to work, and currently am unsure about where to go next. I have the HTML, the JS and the PHP setup, but I am unsure where the error lies. When I submit the form, the form resets, and none of the data that I want sent to the SQL is being sent to database. Any help would be appreciated, thanks.

The form I have for HTML

        <form  id="SignUp" method="post" enctype="multipart/form-data" action="registration.php">
  <div class="container2">
    <h1>Register</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>

    <label ><b>First Name</b></label>  <label id="name1_msg" class="err_msg"></label>
    <input type="text" placeholder="Enter First Name" name="name1" required> <br/>

    <label ><b>Last Name</b></label>  <label id="name2_msg" class="err_msg"></label>
    <input type="text" placeholder="Enter Last Name" name="name2" required> <br/>

    <label ><b>Email</b></label>  <label id="email_msg" class="err_msg"></label>
    <input type="text" placeholder="Enter Email" name="email" required><br/>

    <label ><b>Date of Birth</b></label>  <label id="DOB_msg" class="err_msg"></label>
    <input type="text" placeholder="Enter Date of Birth DD-MM-YYYY" name="DOB" required><br/>

    <label ><b>Password</b></label>  <label id="pswd_msg" class="err_msg"></label>
    <input type="password" placeholder="Enter Password" name="pswd" required><br/>

    <label ><b>Repeat Password</b></label>  <label id="pswdr_msg" class="err_msg"></label>
    <input type="password" placeholder="Repeat Password" name="pswdr" required><br/>

    <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
    <input type="file" name="pic" accept="image/*">

    <hr>

    <p>By creating an account you agree to our <a href="#">Terms + Privacy</a>.</p>
    <input class="btn" type="submit" value="Sign up" name="submit" /> <label id="s_msg" class="err_msg"></label>
    <p>Already have an account? <a href="main.php">Sign in</a>.</p>
  </div>

</form>

I have JS verifying all of the inputs, which then follows with the PHP code, which also verifies it, but server side, which SHOULD then insert all of the information above into the SQL database.

    <?php
    $validate = true;
    $name1 = trim($_POST["name1"]);
    $name2 = trim($_POST["name2"]);
    $email = trim($_POST["email"]);
    $DOB = trim($_POST["DOB"]);
    $pswd = trim($_POST["pswd"]);
    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {
    $db = new mysqli("localhost", "root", "root", "myDB");
    if ($db->connect_error)
    {
        die ("Connection failed: " . $db->connect_error);
    }

    $q1 = "SELECT * FROM ShopUser WHERE email = '$email'";
    $r1 = $db->query($q1);

    if($r1->num_rows > 0)
    {
        $validate = false;
    }
    else
    {
        $name1Match = preg_match("/^[a-zA-Z_-]+$/", $name1);
        if($name1 == null || $name1 == "" || $name1Match == false)
        {
            $validate = false;
        }

        $name2Match = preg_match("/^[a-zA-Z_-]+$/", $name2);
        if($name2 == null || $name2 == "" || $name2Match == false)
        {
            $validate = false;
        }

        $emailMatch = preg_match("/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/", $email);
        if($email == null || $email == "" || $emailMatch == false)
        {
            $validate = false;
        }

        $DOBMatch = preg_match("/^(0[1-9]|[12][0-9]|3[01])[\- \/.](?:(0[1-9]|1[012])[\- \/.](19|20)[0-9]{2})$/", $DOB);
        if($DOB == null || $DOB == "" || $DOBMatch == false)
        {
            $validate = false;
        }

        $pswdLen = strlen($pswd);
        $pswdMatch = preg_match("/^(?=.*\d)(?=.*[a-zA-Z]).{8,}$/", $pswd);
        if($pswd == null || $pswd == "" || $pswdLen< 8 || $pswdMatch == false)
        {
            $validate = false;
        }

        $target_dir = "pictures/";
        $target_file = $target_dir . ShopUser($_FILES["fileToUpload"]["name"]);
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

        if($check == false || $_FILES["fileToUpload"]["size"] > 2000000 || 
$imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif")
        {
            $validate = false;
        }

    }

    if($validate == true)
    {
        $dateFormat = date("DD-MM-YYYY", strtotime($DOB));

        $q2 = "INSERT INTO ShopUser (name1, name2, email, DOB, pswd)
                VALUES ('$name1', '$name2', '$email', '$dateFormat', '$pswd')";


        $r2 = $db->query($q2);
        if ($r2 === true)
        {
            header("Location: main.php");
            $db->close();
            exit();
        }
    }
    else
    {
        echo "alert('Error! Something has gone wrong, please try again')";
        $db->close();
    }}?>

Well this is what I have thus far. Here is the, I included error handling at the top of the code so from this point on you can see the error or warning and let me know what your getting and I can help you fix it. However I cannot run the full code as I do not have the function ShopUser that you are calling

<?php
//This Will Report Errors and Warnings
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$validate = true;
$name1 = trim($_POST["name1"]);
$name2 = trim($_POST["name2"]);
$email = trim($_POST["email"]);
$DOB = trim($_POST["DOB"]);
$pswd = trim($_POST["pswd"]);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $db = new mysqli("localhost", "root", "root", "myDB");
    if ($db->connect_error) {
        die ("Connection failed: " . $db->connect_error);
    }

    $stmt = $db->prepare('SELECT COUNT(id) FROM ShopUser WHERE email= ?');
    $stmt->bind_param('s', $email);
    $stmt->execute();
    $stmt->bind_result($num);
    $stmt->fetch();

    if ($num > 0) {
        echo('User Already Exists');
        $validate = false;
    } else {
        echo $validate;

        $name1Match = preg_match("/^[a-zA-Z_-]+$/", $name1);
        if ($name1 == null || $name1 == "" || $name1Match == false) {
            $validate = false;
        }

        $name2Match = preg_match("/^[a-zA-Z_-]+$/", $name2);
        if ($name2 == null || $name2 == "" || $name2Match == false) {
            $validate = false;
        }

        $emailMatch = preg_match("/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/", $email);
        if ($email == null || $email == "" || $emailMatch == false) {
            $validate = false;
        }

        $DOBMatch = preg_match("/^(0[1-9]|[12][0-9]|3[01])[\- \/.](?:(0[1-9]|1[012])[\- \/.](19|20)[0-9]{2})$/", $DOB);
        if ($DOB == null || $DOB == "" || $DOBMatch == false) {
            $validate = false;
        }

        $pswdLen = strlen($pswd);
        $pswdMatch = preg_match("/^(?=.*\d)(?=.*[a-zA-Z]).{8,}$/", $pswd);
        if ($pswd == null || $pswd == "" || $pswdLen < 8 || $pswdMatch == false) {
            $validate = false;
        }

        $target_dir = "pictures/";
        $target_file = $target_dir . ShopUser($_FILES["fileToUpload"]["name"]);
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

        if ($check == false || $_FILES["fileToUpload"]["size"] > 2000000 ||
            $imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif") {
            $validate = false;
        }

    }

    if ($validate == true) {
        $dateFormat = date("DD-MM-YYYY", strtotime($DOB));

        try {
            $q2 = "INSERT INTO ShopUser (name1, name2, email, DOB, pswd)
                VALUES ('$name1', '$name2', '$email', '$dateFormat', '$pswd')";
        } catch (Exception $e) {
            echo $e . 'not Working';
        }


        $r2 = $db->query($q2);
        if ($r2 === true) {
            header("Location: main.php");
            $db->close();
            exit();
        }
    }
    else
    {
        echo "alert('Error! Something has gone wrong, please try again')";
        $db->close();
    }
} 
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM