无法将php中的数据从注册表插入mysql服务器

k2fxgqgv  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(290)

我一直在尝试让一套代码工作,目前还不确定下一步该去哪里。我有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代码,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();
    }}?>
mznpcxlj

mznpcxlj1#

好吧,这就是我目前所拥有的。这里是,我包括在代码顶部的错误处理,所以从这一点上你可以看到错误或警告,让我知道你得到什么,我可以帮助你修复它。但是,我无法运行完整的代码,因为我没有您正在调用的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();
    }
} 
?>

相关问题