DEV Community

Cover image for How to create a Signup page with PHP/MySQL Using ChatGPT: from prompt to result
Jibril Dauda Muhammad
Jibril Dauda Muhammad

Posted on

How to create a Signup page with PHP/MySQL Using ChatGPT: from prompt to result

ChatGPT an artificial intelligence chatbot developed by OpenAI and launched in November 2022

It is built on top of OpenAI's GPT-3.5 and GPT-4 language models and is optimized for dialogue using Reinforcement Learning with Human Feedback
.
In this tutorial you will create a signup page with PHP/MySQL using chatgpt, from prompt to result. the result has been broken down to more than 1 prompt for easier understanding of how the process where works
.

PREREQUISITE

  • Text Editor(Sublime text/Visual studio code)
  • PHP/MySQL install on you system (download XAMPP) to run the project
  • Basic understanding of PHP/MySQL
  • Account with ChatGPT if you want to try the prompt your self

NOTE: prompt might likely return different answer but result will still be the same

PROMPT 1

You will create a signup page using php/MySQL, I will feed you the required prompt first you will create a database with name auth, table user with rows (username, email, password, fullname, and gender). create only the sql file first

CREATE DATABASE auth;

USE auth;

CREATE TABLE user (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL,
    password VARCHAR(255) NOT NULL,
    fullname VARCHAR(100) NOT NULL,
    gender ENUM('Male', 'Female', 'Other') NOT NULL
);

Enter fullscreen mode Exit fullscreen mode

After successfully entering the above prompt it creates a database named auth and a table named user with five columns: id, username, email, password, fullname, and gender. The id column is set to auto-increment so that each new record will automatically be assigned a unique ID number. The username, email, password, fullname, and gender columns are all required and cannot be null. The gender column is an ENUM type, which means it can only take on one of the specified values: 'Male', 'Female', or 'Other'.

PROMPT 2

Create a connection file to the database name it connection.php, only create the connection file

<?php
$host = "localhost"; // Replace with your host name
$user = "root"; // Replace with your database username
$password = ""; // Replace with your database password
$dbname = "auth"; // Replace with your database name

// Create connection
$conn = new mysqli($host, $user, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

Enter fullscreen mode Exit fullscreen mode

Once you have created the database and table, you can create a file called connection.php . The code creates a new mysqli object and passes in the host name, database username, database password, and database name to establish a connection. It then checks if the connection was successful by checking for a connection error. If an error is detected, the script will output a message and terminate.

PROMPT 3

Create the php script that will handle the signup process, include the connection.php file inside, name the file register_handler.php, only create the file

<?php
// Include connection file
include_once 'connection.php';

// Get form data
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$fullname = $_POST['fullname'];
$gender = $_POST['gender'];

// Prepare SQL query
$stmt = $conn->prepare("INSERT INTO user (username, email, password, fullname, gender) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $username, $email, $password, $fullname, $gender);

// Execute query
if ($stmt->execute()) {
    // Registration successful
    header("Location: success.html");
    exit();
} else {
    // Registration failed
    header("Location: error.html");
    exit();
}

// Close statement and connection
$stmt->close();
$conn->close();
?>

Enter fullscreen mode Exit fullscreen mode

The above prompt will write the script for rigister_handler.php page in the code it first includes the connection.php file to establish a connection to the database. It then retrieves the form data submitted via the POST method and stores it in variables.

Next, it prepares an SQL query to insert the form data into the user table using prepared statements to prevent SQL injection attacks. The bind_param method binds the variables to the SQL statement.

The script then executes the query and checks if it was successful. If so, it redirects the user to a success page. If not, it redirects them to an error page.

Finally, it closes the prepared statement and database connection

PROMPT 4

Create the html with all the required form input, the action attribute should reference the register_handler.php file, only create the file and name it index.php

<!DOCTYPE html>
<html>
<head>
    <title>Sign Up</title>
</head>
<body>
    <h2>Sign Up</h2>
    <form method="post" action="register_handler.php">
        <label for="username">Username:</label><br>
        <input type="text" id="username" name="username" required><br>

        <label for="email">Email:</label><br>
        <input type="email" id="email" name="email" required><br>

        <label for="password">Password:</label><br>
        <input type="password" id="password" name="password" required><br>

        <label for="fullname">Full Name:</label><br>
        <input type="text" id="fullname" name="fullname" required><br>

        <label for="gender">Gender:</label><br>
        <select id="gender" name="gender" required>
            <option value="">Select Gender</option>
            <option value="Male">Male</option>
            <option value="Female">Female</option>
            <option value="Other">Other</option>
        </select><br>

        <input type="submit" value="Sign Up">
    </form>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

The above prompt will code HTML. This code creates an HTML form with fields for the username, email, password, fullname, and gender input. The form's method attribute is set to post and the action attribute is set to register_handler.php, which is the file that will handle the form submission.The form includes a submit button that will submit the form data to the register_handler.php file when clicked.

PROMPT 5

Add a touch of styling to the index.php page, name the file style.css only create the style.css

body {
  font-family: Arial, sans-serif;
  background-color: #f2f2f2;
}

h2 {
  color: #333;
  text-align: center;
}

form {
  width: 400px;
  margin: 0 auto;
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
}

label {
  display: inline-block;
  margin-bottom: 5px;
}

input[type="text"],
input[type="email"],
input[type="password"],
select {
  width: 100%;
  padding: 8px;
  border-radius: 3px;
  border: 1px solid #ccc;
  box-sizing: border-box;
  margin-bottom: 10px;
  font-size: 16px;
}

select {
  height: 36px;
}

input[type="submit"] {
  background-color: #4CAF50;
  color: #fff;
  border: none;
  padding: 12px 20px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 16px;
  width: 100%;
}

input[type="submit"]:hover {
  background-color: #3e8e41;
}

input[type="submit"]:focus {
  outline: none;
}

Enter fullscreen mode Exit fullscreen mode

The above prompt will the code that adds some basic styling to the page.

PROMPT 6

Create a sample code for success.html page and error.html page to show result when registration is successful or not

success.html

<!DOCTYPE html>
<html>
<head>
    <title>Registration Successful</title>
</head>
<body>
    <h2>Registration Successful</h2>
    <p>Congratulations! You have successfully registered.</p>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

.
error.html

<!DOCTYPE html>
<html>
<head>
    <title>Registration Error</title>
</head>
<body>
    <h2>Registration Error</h2>
    <p>Sorry, an error occurred while processing your registration. Please try again later.</p>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

RESULT

Source Code
FrontEnd
Success page
Database

Thats wrap,

Have you use ChatGPT to write code? which language did you use? how effective was it? comment below

Top comments (0)