How to connect HTML Register Form to MySQL Database with PHP (2020)
Youtube Videos
How to connect HTML Register Form to MySql Database with PHP
form.php
<!DOCTYPE html>
<html>
<head>
<title>Register Form</title>
</head>
<body>
<form action="insert.php" method="POST">
<table>
<tr>
<td>Name: </td>
<td><input type="text" name="username" required></td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="password" required></td>
</tr>
<tr>
<td>Gender :</td>
<td>
<input type="radio" name="gender" value="m" required>Male
<input type="radio" name="gender" value="f" required>Female
</td>
</tr>
<tr>
<td>Email :</td>
<td><input type="email" name="email" required></td>
</tr>
<tr>
<td>Phone no:</td>
<td>
<select name="phoneCode" required>
<option selected hidden value="">Select Code</option>
<option value="+91">+91</option>
</select>
<input type="phone" name="phone" required>
</td>
</tr>
<tr>
<td><input type="submit" name="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
Create Database
insert.php
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$phoneCode = $_POST['phoneCode'];
$phone = $_POST['phone'];
if (!empty($username) || !empty($password) || !empty($gender) || !empty($email) || !empty($phoneCode) || !empty($phone)) {
$conn = new mysqli('localhost', 'root', '', 'tutorial');
if (mysqli_connect_error()) {
die('Connect Error('.mysqli_connect_errno().')'. mysqli_connect_error());
}else {
$SELECT = "SELECT email From register Where email = ? Limit 1";
$INSERT = "INSERT Into register (username, password, gender, email, phoneCode, phone) VALUES (?, ?, ?, ?, ?, ?)";
//Prepare statement
$stmt = $conn->prepare($SELECT);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($email);
$stmt->store_result();
$rnum = $stmt->num_rows;
if ($rnum==0) {
$stmt->close();
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("ssssii", $username, $password, $gender, $email, $phoneCode, $phone);
$stmt->execute();
echo "Inserted Successfully!";
}else {
echo "Someone already register using the email";
}
$stmt->close();
$conn->close();
}
}else{
echo "All field are required";
die();
}
?>
Comments
Post a Comment