Registration Form

Joined
Jun 14, 2018
Messages
103
Reaction score
1
i'm trying to create a registration form for users
to register on our site? basically i've created this script

Code:
<?php
// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get form data and sanitize inputs
    $yourname = filter_input(INPUT_POST, "yourname", FILTER_SANITIZE_STRING);
    $lastname = filter_input(INPUT_POST, "lastname", FILTER_SANITIZE_STRING);
    $username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL);
    $password = $_POST["password"];
    // Validate inputs
    if (empty($yourname) || empty($lastname) || empty($username) || empty($email) || empty($password)) {
        echo "Please fill in all fields.";
    } else {
        // Hash the password (ensure you have proper hashing and salting)
        $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
        // Database connection settings
        $servername = "my_server_name"; // Replace with your actual server name
        $connection_username = "my_username"; // Replace with your actual database username
        $connection_password = "my_password"; // Replace with your actual database password
        $dbname = "my_database_name"; // Replace with your actual database name
        // Create connection
        $conn = new mysqli($servername, $connection_username, $connection_password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        // Prepare and bind the SQL statement to prevent SQL injection
        $stmt = $conn->prepare("INSERT INTO users (yourname, lastname, username, email, password) VALUES (?, ?, ?, ?, ?)");
        $stmt->bind_param("sssss", $yourname, $lastname, $username, $email, $hashedPassword);
        if ($stmt->execute()) {
            echo "Registration successful!";
        } else {
            echo "Error: " . $stmt->error;
        }
        // Close the connection
        $stmt->close();
        $conn->close();
    }
}
?>

now i've been getting alot of help from a friend of mine(OpenAi.com)

i've tested the form and the form information isn't going into my database(phpmyadmin)
i have setup tables for each column but don't know what's going on


i have html here as well
Code:
<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h4>
    <span class="boldp_line"><span></span></span>
    <span class="solid_line"></span>
    <a href="#" class="title_text">Register</a>
</h4>
<form id="registrationForm" method="post">
    yourname: <input type="text" name="yourname"><br>
    lastname: <input type="text" name="lastname"><br>
    username: <input type="text" name="username"><br>
    email: <input type="email" name="email"><br>
    password: <input type="password" name="password"><br>
    <input type="submit" value="Register">
</form>
<div id="responseMessage"></div>
<script>
$(document).ready(function() {
    $("#registrationForm").submit(function(event) {
        event.preventDefault();
        $.ajax({
            url: "form.php", // Update this to match the correct PHP script filename
            method: "POST",
            data: $(this).serialize(),
            success: function(response) {
                $("#responseMessage").html(response);
            }
        });
    });
});
</script>
</body>
</html>

could someone please tell me where i'm going wrong?
i'v obviously set in the php code as my username ect but i have the rite
database credentials
 
Joined
Jul 4, 2023
Messages
383
Reaction score
43
$servername = "my_server_name"; // Replace with your actual server name
$connection_username = "my_username"; // Replace with your actual database username
$connection_password = "my_password"; // Replace with your actual database password
$dbname = "my_database_name"; // Replace with your actual database name
What about this data, did you set them correctly, there is no error from
PHP:
 // Create connection
 $conn = new mysqli($servername, $connection_username, $connection_password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

i have setup tables for each column
what do you mean by tables?

in this case this should be one table named users with columns: yourname, lastname, username, email, password
e.g
SQL:
DROP TABLE IF EXISTS users;
CREATE TABLE users (
  id INT AUTO_INCREMENT PRIMARY KEY,
  yourname VARCHAR(50),
  lastname VARCHAR(50),
  username VARCHAR(50) NOT NULL,  -- required field
  email VARCHAR(100) NOT NULL,    -- required field
  password VARCHAR(50) NOT NULL   -- required field
) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
 
Last edited:
Joined
Sep 4, 2022
Messages
129
Reaction score
16
Hello,

some advices for your Php :
--
to handle you input, you can do it in a loop, $_POST is an Array, you can fetch it easy with an index $i, or with 'for each' loop. both to sanitize vars in, and to check their availability.

--
as DB is a common resource and will be so often used,
make a 'class DB' in your php.

Do you know OOP ?
--

you can shorten your code, by making your code more 'active', you have a 'lot of allocations' for nothing.
--

when you ends a SQL query, all (delete, update, insert) needs a 'commit()' to validate the query at db level.

--

when your script is at its end, you can erase all Objects, and vars with th 'unset($var)' function.
your server will like it, you'll gain in speed by a good memory management.
ex :
unset($conn);
unset($stmt);

--

It's few 'best practisse' by Php.
get into it.
it change your code a bit, for a better run-time.
 
Joined
Jul 4, 2023
Messages
383
Reaction score
43
Try to run the php code with the data sent directly from the form without using ajax ...
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Registration Form</title>
    <!-- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> -->
  </head>
  <body>
    <h4>
      <span class="boldp_line"><span></span></span>
      <span class="solid_line"></span>
      <a href="#" class="title_text">Register</a>
    </h4>
    <form action="form.php" id="registrationForm" method="post">
      yourname: <input type="text" name="yourname"><br>
      lastname: <input type="text" name="lastname"><br>
      username: <input type="text" name="username"><br>
      email: <input type="email" name="email"><br>
      password: <input type="password" name="password"><br>
      <input type="submit" value="Register">
    </form>
    <div id="responseMessage"></div>
    <script>
      /*
      $(document).ready(function() {
          $("#registrationForm").submit(function(event) {
              event.preventDefault();
              $.ajax({
                  url: "form.php", // Update this to match the correct PHP script filename
                  method: "POST",
                  data: $(this).serialize(),
                  success: function(response) {
                      $("#responseMessage").html(response);
                  }
              });
          });
      });
      */
    </script>
  </body>
</html>

.. and see what feedback you get
 
Joined
Jun 14, 2018
Messages
103
Reaction score
1
Try to run the php code with the data sent directly from the form without using ajax ...
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Registration Form</title>
    <!-- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> -->
  </head>
  <body>
    <h4>
      <span class="boldp_line"><span></span></span>
      <span class="solid_line"></span>
      <a href="#" class="title_text">Register</a>
    </h4>
    <form action="form.php" id="registrationForm" method="post">
      yourname: <input type="text" name="yourname"><br>
      lastname: <input type="text" name="lastname"><br>
      username: <input type="text" name="username"><br>
      email: <input type="email" name="email"><br>
      password: <input type="password" name="password"><br>
      <input type="submit" value="Register">
    </form>
    <div id="responseMessage"></div>
    <script>
      /*
      $(document).ready(function() {
          $("#registrationForm").submit(function(event) {
              event.preventDefault();
              $.ajax({
                  url: "form.php", // Update this to match the correct PHP script filename
                  method: "POST",
                  data: $(this).serialize(),
                  success: function(response) {
                      $("#responseMessage").html(response);
                  }
              });
          });
      });
      */
    </script>
  </body>
</html>

.. and see what feedback you get
thank you for you're help but i've already done it lol, now i'm stuck on time everytime i run a test on the registration form the timestamp returns 7:00:24 when the time here in uk is 12:24? i'v got
Code:
// Set signup_date to the current date and time
        date_default_timezone_set("Europe/London");
        echo "The time is " . date("h:i:sa");
 
Last edited:
Joined
Jul 4, 2023
Messages
383
Reaction score
43
Check time on server without setting time zone, and on your computer as well, e.g.

PHP:
<?php
  echo '<pre>Time on server: ' . date('H:i:s') . '</pre>';
?>
<script>
  const pre = document.querySelector('pre');
  pre.textContent += '\nTime on computer: ' + new Date().toLocaleTimeString();
</script>
 
Joined
Jun 14, 2018
Messages
103
Reaction score
1
Check time on server without setting time zone, and on your computer as well, e.g.

PHP:
<?php
  echo '<pre>Time on server: ' . date('H:i:s') . '</pre>';
?>
<script>
  const pre = document.querySelector('pre');
  pre.textContent += '\nTime on computer: ' + new Date().toLocaleTimeString();
</script>
i'm in my .htaccess and this is what i'v seen
php_value date.timezone Europe/London
 
Joined
Jul 4, 2023
Messages
383
Reaction score
43
php_value date.timezone Europe/London
try setting this line as a comment (in .htaccess) and check again... how big is the difference between the time on the server and the time on your computer, checking this may be helpful in finding the cause of this error

Apache config:
# php_value date.timezone Europe/London
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,808
Messages
2,569,684
Members
45,446
Latest member
TeddyScull

Latest Threads

Top