0

I'm trying to link a register page with my login page to allow new users to register an account to use on my application. I've linked the form up to my tbl_Users table on MySQL in which holds all the information that the users would input into this form. I've properly set everything up using queries and such and the form displays properly at the very least. However when I click submit, the page just refreshed with the fields now empty again and no new data within my table on the database. Where am I going wrong? (Extra-note: I'm still in the process of coding in the safety code to prevent sql-injections)

ConnectorCode.php

<?php

 $conn = mysqli_connect("localhost", "b4014107", "Win1", "b4014107_db2") or die (mysqli_connect_error());

 ?>

Register.php

<?
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
include('ConnectorCode.php');

if(isset($_POST['submit'])) { 
$FName = $_POST['First_Name'];
$LName = $_POST['Last_Name'];
$Email = $_POST['Email'];
$UName = $_POST['User_Name'];
$Password  = $_POST['Password']; 

$FName = mysqli_real_escape_string($conn, $FName);
$LName = mysqli_real_escape_string($conn, $LName);
$Email = mysqli_real_escape_string($conn, $Email);
$UName = mysqli_real_escape_string($conn, $UName);
$Password = mysqli_real_escape_string($conn, $Password);

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

if(mysqli_num_rows($result) == 1)
{
    echo "Sorry, the email you are trying to enter already exists";
}
else
{
    $query = mysqli_query($conn, "INSERT INTO tbl_Users(First_Name, Last_Name, Email, User_Name, Password) VALUES ('$FName', '$LName', '$Email', '$UName', '$Password')");
if($query)
{
 echo "Thank you for registering";
}
header('Location: Index.php');
}

}
?>

<!DOCTYPE HTML>
<head>
<title>Register</title>
</head>
<body>

<h1> Register Page </h1>

<p> Please fill in the form to register <p>

<form method="post" action="">
<fieldset>
First Name: <br />
<input name="First_Name" type="text" class="input" size="25" required /> <br /> <br />
 Last Name:  <br />
 <input name="Last_Name" type="text" class="input" size="25" required /> <br /> <br />
Email: <br />
<input name="Email" type="email" class="input" size="25" required /> <br /> <br />
Username: <br />
<input name="User_Name" type="text" class="input" size"25" required /> <br /> <br />
Password: <br />
<input name="Password" type="password" class="input" size="25" required /> <br /> <br/>
<input type="submit" name="submit" value="Register!" />
</fieldset>
</form>
</body>
</html>
Henry Green
  • 243
  • 3
  • 4
  • 15

2 Answers2

0

You do not meet the condition isset($_POST['VALUES']) because you don't have field with name="VALUES".

Change

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

to

if(isset($_POST['submit'])) {
Akairis
  • 407
  • 2
  • 11
  • I've changed it but I'm still getting no new rows in my table on the database. – Henry Green Apr 01 '16 at 15:27
  • No! That's what's so confusing, no error messages are displayed on the top of the screen, all that happens is the field refresh and no new data is entered into the table. – Henry Green Apr 01 '16 at 15:35
0

You had a lot missing...

<?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']); // why did you need to repeat this all twice?

    $sql = "SELECT * FROM tbl_Users WHERE Email='$Email'"; // ? didn't understand why you was asking for the Email using the Email...
    $result = $conn->query($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($conn->query($insert_sql)) // You forgot to query this
         {
             echo "Thank you for registering";
             header('Location: index.php'); // lowercase i
             exit; // you forgot this
         }
    }
    else
    {
        echo "Sorry, that email already exists!";
    }
    $conn->close(); // you forgot this

}

?>

Hope this helps...

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • Thank you a lot! But now everytime I load up the php file This pops up at the top of the page `query($query)) { echo "Thank you for registering"; header('Location: index.php'); exit; } } else { echo "Sorry, that email already exists!"; } $conn->close(); } ?> ` – Henry Green Apr 01 '16 at 15:51
  • Try that now, edited. Anyway, I'm finishing work now so that should fix your issue. Good luck. – Jaquarh Apr 01 '16 at 15:51
  • Now it's showing this `query($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($conn->query($insert_sql)) { echo "Thank you for registering"; header('Location: index.php'); exit; } } else { echo "Sorry, that email already exists!"; } $conn->close(); } ?> ` – Henry Green Apr 01 '16 at 15:54
  • It's the -> syntax I think, it doesn't seem to like it. – Henry Green Apr 01 '16 at 15:55
  • What PHP version are you running? It may be that $result returns a boolean rather than the query data, just use the depreciated `mysqli_query` rather than the `->` and it should all work. Good luck – Jaquarh Apr 01 '16 at 15:57
  • It's forming an error properly now, it's showing the echo message `"Sorry, that email already exists!"` to any entry now/ – Henry Green Apr 01 '16 at 15:59
  • By the way how can I convert this line into the mysqli because I'm not sure how to `if($conn->query($insert_sql)) ` – Henry Green Apr 01 '16 at 16:03
  • `if(mysqli_query($conn, $insert_sql))` – Jaquarh Apr 04 '16 at 08:05