表单提交,php,不断收到数组

时间:2017-11-19 20:12:48

标签: php mysql forms submission

我在一个学校项目(高中)的论坛上工作,我有一个注册表。一切正常,但是当我提交表单时,我得到的设置错误表示"发生错误"。我没有收到任何MYSQL错误,我想知道我的代码有什么问题。请注意,一切都应该工作但由于某种原因它不提交到数据库。我知道HTML很好,所以我只提交php。

register.php

    <?php 
//get required files
include 'inc/config.php';


//get all variables to avoid errors
$firstname = ""; //first name of user
$lastname = ""; //last name of user
$username = ""; //username of user
$email = ""; //email of user
$city = ""; //current city 
$password = ""; //password
$password2 = ""; //confirm password
$date = ""; //signup date
$error_array = array(); //holds all error messages
$ip = ""; //ip address of user
$allowed_cities = array("Toronto","Ottawa","Hamilton","London","Windsor","Kingston"); //holds all cities allowed
$profilepic = ""; //profile photo
$code = ""; //email token to verify account 

//Profile Photo Settings
$allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation
$max_filesize = 9999999999; // Maximum filesize in BYTES - SET IN to a low number for small files
$upload_path = 'data/profilepictures/'; // The place the files will be uploaded to (currently a 'profile pictures' directory)


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

//First Name
    $firstname = strip_tags($_POST['firstname']); //Remove html tags
    $firstname = str_replace(' ', '', $firstname); //remove spaces
    $firstname = ucfirst(strtolower($firstname)); //Uppercase first letter

//Last Name
    $lastname  = strip_tags($_POST['lastname']); //Remove html tags
    $lastname = str_replace(' ', '', $lastname); //remove spaces
    $lastname = ucfirst(strtolower($lastname)); //Uppercase first letter

//Username
    $username = strip_tags($_POST['username']); //Remove html tags
    $username = str_replace(' ', '_', $username); //remove spaces and put a underscore
    //Note: preg match for username done later

//Email
    $email = strip_tags($_POST['email']); //Remove html tags
    $email = str_replace(' ', '', $email); //remove spaces

//City
$city = strip_tags($_POST['city']); //Remove html tags
$city = str_replace(' ', '', $city); //remove spaces
$city = ucfirst(strtolower($city)); //Uppercase first letter

//Password and Confirm Password
$password = strip_tags($_POST['password']); //Remove html tags
$password2 = strip_tags($_POST['password2']); //Remove html tags

//Profile Picture
$profilepic = $_FILES['photo']['name']; // Get the name of the file (including file extension)
$ext = substr($profilepic, strpos($profilepic,'.'), strlen($profilepic)-1); // Get the extension from the profilepic

//Email Verification
$code=substr(md5(mt_rand()),0,15); //token to verify email


//Date and IP Address
$date = date("Y-m-d"); //Current date
$ip = $_SERVER['REMOTE_ADDR']; //IP Address


//Check submissions for errors

    //Check Firstname
    if(strlen($firstname) > 25 || strlen($firstname) < 2) {
        array_push($error_array, "Your first name must be between 2 and 25 characters<br>");
    }

    //Check Lastname
    if(strlen($lastname) > 25 || strlen($lastname) < 2){
        array_push($error_array, "Your last name must be between 2 and 25 characters<br>");
    }

    //Check Username

    $u_check = mysqli_query($con, "SELECT username FROM users WHERE username='$username'");

    $num_rows = mysqli_num_rows($u_check);

    if($num_rows > 0) {

    array_push($error_array, "Username is taken<br>");

    }

    if(strlen($username) > 30 || strlen($username) < 3){
        array($error_array, "Your username must be between 3 and 30 characters<br>");

    }

    if(preg_match('/^[a-z0-9]{6,10}$/', $username)) {
            array_push($error_array, "Your username includes invalid characters<br>");
        }

    //Check Email

    $e_check = mysqli_query($con, "SELECT email FROM users WHERE email='$email'");

    $num_rows2 = mysqli_num_rows($e_check);

    if($num_rows2 > 0){

        array_push($error_array, "Email already in use<br>");


    }

    $allowed_emails = array('student.tdsb.on.ca', 'delasalle.ca', 'ucc.on.ca');

    $explodedEmail = explode('@', $email);
    $domain = array_pop($explodedEmail);

    if ( ! in_array($domain, $allowed_emails))
    {
        array_push($error_array, "Your student email is not allowed<br>");
    }


    //Check City

    if (!in_array($city, $allowed_cities)) {
        array_push($error_array, "Our service is not available in your city<br>");
    }

    //Check Passwords

    if($password != $password2) {
        array_push($error_array,  "Your passwords do not match<br>");
    }

    if(strlen($password > 30 || strlen($password) < 5)) {
        array_push($error_array, "Your password must be betwen 5 and 30 characters<br>");
    }

    if(preg_match('/[^A-Za-z0-9]/', $password)) {
            array_push($error_array, "Your password can only contain english characters or numbers<br>");
        }

    //Check Profile Picture
    if(!in_array($ext,$allowed_filetypes)){
        array_push($error_array, "The file you attempted to upload is not allowed<br>");
    }

    if(filesize($_FILES['photo']['tmp_name']) > $max_filesize){
        array_push($error_array, "The file you attempted to upload is too large<br>");

    }

    if(!is_writable($upload_path)){
        array_push($error_array, "You cannot upload to the specified directory, please CHMOD it to 777<br>");
    }

    if(move_uploaded_file($_FILES['photo']['tmp_name'],$upload_path . $profilepic)){

    }
    else{
        array_push ($error_array, "There was an error during the file upload. Please try again later<br>");
    }

    //Process Data

    if(empty($error_array)) {
        $password = md5($password); //Encrypt password before sending to database

        $query = mysqli_query($con, "INSERT INTO users (firstname,lastname,email,username,password,signup_date,city,profilephoto,email_verified ) VALUES ('$firstname','$lastname','$email','$username','$password','$date','$profilepic','0') ");
        array_push($error_array, "Your acccount has been created. Please check your inbox to verify your account<br>");

}

else{
        array_push($error_array, "An error has occured. Please try again later<br>");
}














}



?>

0 个答案:

没有答案