简体   繁体   中英

php register form not updating database

So when I sign into my website with existing DB credentials, it logs in perfectly, redirects me to a Welcome.php page and where "Login/register" text usually sits, it now displays the username there, similar to this:

welcome (username) Logout.

Now that all works great. But here is my problem:

I have a register script that once submitted, also redirects me to my Welcome.php page upon a successful registration. BUT the "Login/register" text does not change, (essentially meaning no one is logged in) AND when i check my database, there are no new entries.

To confirm - I can fill out my signup sheet and click "Signup", then i'm redirected to a Welcome.php page... but nothing has changed (no new credentials stored in the db and nothing other than a page redirect)

My new_signup.php script is as follows:

<?php
include "scripts/connection.php";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $myusername = mysqli_real_escape_string($link,$_POST['Username']);
    $myname = mysqli_real_escape_string($link,$_POST['Name']);
    $mypassword = mysqli_real_escape_string($link,$_POST['Password']);
    $myemail = mysqli_real_escape_string($link,$_POST['Email']);
    $myaddress = mysqli_real_escape_string($link,$_POST['Address']);
    $mypostcode = mysqli_real_escape_string($link,$_POST['Postcode']);

    //Checks the database to see if username exists already
    $query = "SELECT * FROM Customer WHERE Customer_Username = '$myusername'";
    $result = mysqli_query($link, $query);
    $nums = mysqli_num_rows($result);

    //Checks the database to see if email address exists already
    $query2 = "SELECT * FROM Customer WHERE Customer_Email = '$myemail'";
    $result2 = mysqli_query($link, $query2);
    $nums2 = mysqli_num_rows($result2);

    if ($nums >= 1)
        //informs user if username already exists
        echo "Username already exists, click <a href = 'user_login.php'>HERE </a> to try again";

    else if ($nums2 >=1)

        //informs user if email already exists
        echo "Email Address already exists, click <a href = 'user_login.php'>HERE </a> to try again";

    else {

        $insert = 'INSERT INTO Customer 
                    (Customer_Username, Customer_Name, 
                    Customer_Password, Customer_Email, Customer_Address, 
                    Customer_Postcode) 
                    VALUES("'.$myname.'","'.$myusername.'","'.$mypassword.
                    '","'.$myemail.'","'.$myaddress.'","'.$mypostcode.'")';

        mysqli_query($link, $insert);
        mysqli_close($link);

        if($insert) {
            $_SESSION['message'] = "Registration Successful";
            header("Location: /Welcome.php");           
        } else {
             $_SESSION['message'] = "Something went wrong";
        }   

    }
}
?>

So what I need to happen is for a user to sign up, be redirected to the welcome.php page and for credentials to be stored in the DB. There are also checks to see if emails/usernames already exist.

Just to add, my login.php script and the above new_signup.php are separate php files. Not sure if doing it my way is easier than keeping both the login and signup scripts in one file

I have triple checked all of my DB fields are correct along with the form fields too. Happy to provide more details if needed.

Thank you for your time.

UPDATE

I have updated the code to show {} and added in some suggested comments, All i get know when I click signup is a white screen.

<?php
    include "scripts/connection.php";
    error_reporting(E_ALL);
ini_set('display_errors', 1);


    if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $myusername = mysqli_real_escape_string($link,$_POST['Username']);
    $myname = mysqli_real_escape_string($link,$_POST['Name']);
    $mypassword = mysqli_real_escape_string($link,$_POST['Password']);
    $myemail = mysqli_real_escape_string($link,$_POST['Email']);
    $myaddress = mysqli_real_escape_string($link,$_POST['Address']);
    $mypostcode = mysqli_real_escape_string($link,$_POST['Postcode']);

    //Checks the database to see if username exists already
    $query = "SELECT * FROM Customer WHERE Customer_Username = '$myusername'";
    $result = mysqli_query($link, $query);
    $nums = mysqli_num_rows($result);

//Checks the database to see if email address exists already
    $query2 = "SELECT * FROM Customer WHERE Customer_Email = '$myemail'";
    $result2 = mysqli_query($link, $query2);
    $nums2 = mysqli_num_rows($result2);

    if ($nums >= 1) {
            //informs user if username already exists
     echo "Username already exists, click <a href = 'user_login.php'>HERE </a> to try again";

        }

    else if ($nums2 >=1) {
            //informs user if email already exists
    echo "Email Address already exists, click <a href = 'user_login.php'>HERE </a> to try again";

         }else{

    $insert = "INSERT INTO Customer (Customer_Username, Customer_Name, Customer_Password, Customer_Email, Customer_Address, Customer_Postcode) 
    VALUES('$myname', '$myusername', '$mypassword', '$myemail', '$myaddress', '$mypostcode')";

         }

        $insertCheck = mysqli_query($link, $insert);

        if($insertCheck) {

            $_SESSION['message'] = "Registration Successful";

            header("Location: /Welcome.php");  exit();

        } else {
             $_SESSION['message'] = "Something went wrong";
        }   
    }

?>

Little edits and you might want to filter your sql queries to prevent injections:

<?php
session_start();
include "scripts/connection.php";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $myusername = mysqli_real_escape_string($link,$_POST['Username']);
    $myname = mysqli_real_escape_string($link,$_POST['Name']);
    $mypassword = mysqli_real_escape_string($link,$_POST['Password']);
    $myemail = mysqli_real_escape_string($link,$_POST['Email']);
    $myaddress = mysqli_real_escape_string($link,$_POST['Address']);
    $mypostcode = mysqli_real_escape_string($link,$_POST['Postcode']);

    //Checks the database to see if username exists already
    $query = "SELECT * FROM Customer WHERE Customer_Username = '$myusername'";
    $result = mysqli_query($link, $query);
    $nums = mysqli_num_rows($result);

    //Checks the database to see if email address exists already
    $query2 = "SELECT * FROM Customer WHERE Customer_Email = '$myemail'";
    $result2 = mysqli_query($link, $query2);
    $nums2 = mysqli_num_rows($result2);

    if ($nums >= 1)
        //informs user if username already exists
        echo "Username already exists, click <a href = 'user_login.php'>HERE </a> to try again";

    else if ($nums2 >=1)

        //informs user if email already exists
        echo "Email Address already exists, click <a href = 'user_login.php'>HERE </a> to try again";

    else {

        $insert = 'INSERT INTO Customer 
                    (Customer_Username, Customer_Name, 
                    Customer_Password, Customer_Email, Customer_Address, 
                    Customer_Postcode) 
                    VALUES("'.$myname.'","'.$myusername.'","'.$mypassword.
                    '","'.$myemail.'","'.$myaddress.'","'.$mypostcode.'")';

        $insertCheck = mysqli_query($link, $insert);
        mysqli_close($link);

        if($insertCheck) {
            $_SESSION['message'] = "Registration Successful";
            header("Location: /Welcome.php");           
        } else {
             $_SESSION['message'] = "Something went wrong";
        }   

    }
}
header("Location: /registerview.php");//-->assuming your registration view

?>

Change

$insert = 'INSERT INTO ... VALUES ("'.$myname.'", ...

to

$insert = "INSERT INTO ... VALUES ('".$myname."', ...

(switching single for double quotes and vice versa)

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