无法从注册表中将数据从PHP插入MYSQL服务器

时间:2018-08-15 22:22:05

标签: javascript php html mysql

我一直在尝试获取一组代码来工作,目前不确定下一步要去哪里。我有HTML,JS和PHP设置,但是我不确定错误在哪里。当我提交表单时,表单会重置,并且我要发送到SQL的所有数据都不会发送到数据库。任何帮助,将不胜感激,谢谢。

我拥有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>

我让JS验证了所有输入,然后是PHP代码,它也进行了验证,但是服务器端则应该将上面的所有信息插入SQL数据库。

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

1 个答案:

答案 0 :(得分:0)

这是我到目前为止所拥有的。这是我在代码顶部包括的错误处理,因此从这一点开始,您可以看到错误或警告,并告诉我您得到了什么,我可以帮助您解决它。但是,由于我没有要调用的功能ShopUser,因此我无法运行完整的代码

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