Jump to content

Recommended Posts

<?php


error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
require_once 'ConnectorCode.php';

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

    $FName = mysqli_real_escape_string($conn, $_POST['First_Name']);
    $LName = mysqli_real_escape_string($conn, $_POST['Last_Name']);
    $Email = mysqli_real_escape_string($conn, $_POST['Email']);
    $UName = mysqli_real_escape_string($conn, $_POST['User_Name']);
    $Password = mysqli_real_escape_string($conn, $_POST['Password']); 

    $sql = "SELECT * FROM tbl_Users WHERE Email='$Email'"; 
    $result = mysqli_query($conn, $sql);
    if(count($result) == 0)
    {
         $insert_sql = "INSERT INTO tbl_Users (First_Name,Last_Name,Email,User_Name,Password) VALUES ('$FName','$LName','$Email','$UName','$Password')";
         if(mysqli_query($conn, $insert_sql)) 
         {
             echo "Thank you for registering";
             header('Location: index.php'); 
             exit; 
         }
    }
    else
    {
        echo "Sorry, that email already exists!";
    }
    mysqli_close($conn); 

}

?>

Long story short. I'm trying to set up a registration form for users to use to create an account in which will then send the information they enter into my html form into my Users table on my MySQL database. I have everything set up properly but all I seem to get now is the else echo message which is:

else
    {
        echo "Sorry, that email already exists!";
    }

It always pops up whenever submit is clicked! I don't know why! Why is it doing this I'm so close to having this file finished!

Register.php

I've put var_dump($result) after the $result= mysqli_query($conn, $sql) line but nothings coming up. and there is no data being inserted! That's another issue, it won't even input any of the data I'm putting into the fields!

$sql = "SELECT * FROM tbl_Users WHERE Email='$Email'"; 
    $result = mysqli_query($conn, $sql);

the $result value in this case is the mysqli object, but as mac_gyver previously mentioned: it is not the actual result of the query. You will still need another mysqli_fetch_* function to retrieve the data.

 

Since all you need the above function to perform is return a count, you can implement that in the SQL itself, which is far more efficient. You should look into editing your query to implement a COUNT, as well as adding a mysqli_fetch_* function to return said count. Which mysqli_fetch_ function you should use, you can lookup in the documentation.

 

A good practise would be to make sure the returned COUNT works before trying to insert anything into the database. It makes debugging a lot easier.

upon further review (i stopped looking the first time when i discovered the OP wasn't operating on the value correctly), the only thing to OP should be doing in this code is running the INSERT query.

 

the user_name column and the email column should be set up as unique indexes, to prevent duplicate values in those columns. the INSERT query will fail with a duplicate key error (you can look up what error number this is and then check what error number is returned when the query fails) if either the user_name or the email already exist in the table.

 

you should also be hashing the password using the php password_hash() function and store the hashed password in the database table.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.