Please login to start chat

ICT Solutions 2021-06-08 19:35:29
Category : php tutorials

Inserting data into table (eg.3)

This step is a little bit more complex. In this step, we also check the User Id already exists or not. But, not too difficult. I hope you will be ok.

1. Create html form.

2. Connect to server.

3. Check form is filled or not.

4. Check the User Id already exists or not.

5. Insert data into table.


<?php

@$server = 'localhost';
@$username = 'root';
@$password = ' ';
@$db = 'TNW';
@$conn = mysqli_connect($server, $username, $password, $db);

if($conn){
if(isset($_POST['UserId']) && isset($_POST['FirstName']) && isset($_POST['LastName']) && isset($_POST['UserName'])&& isset($_POST['Password'])){
$userid = $_POST['UserId'];
$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];
$username = $_POST['UserName'];
$password = $_POST['Password'];

//check form is filled or not
if(!empty($userid) && !empty($firstname) && !empty($lastname) && !empty($username) && !empty($password)){
//check userid already exists or not
$query = "SELECT `UserId` FROM `username` WHERE `UserId` = '".$userid."'";
$query_run = mysqli_query($conn, $query);
$row = mysqli_num_rows($query_run);
if($row == 1){
echo 'User Id already exists. Please choose another User Id.';
}else{
//insert data into table
$query = "INSERT INTO `username` VALUES('".$userid."', '".$firstname."', '".$lastname."',
'".$username."', '".$password."')";
$query_run = mysqli_query($conn, $query);
if($query_run){
echo 'Data is successfully inserted into table.';
}else{
echo 'Inserting Error!';
}
}
}else{
echo 'Please fill all fields.';
}
}
}else{
echo 'Error in Server connection!';
}
?>

<html>
<head>
<title>Inserting Data into table</title>
<style>
.tnwform{
padding: 20px;
}
</style>
</head>
<body>
<form action="inserting_data_into_tabel_3.php" method="POST" class="tnwform">
<label for="UserId">User Id</label>
<input type="text" name="UserId"><br /><br />

<label for="FirstName">First Name</label>
<input type="text" name="FirstName"><br /><br />

<label for="LastName">Last Name</label>
<input type="text" name="LastName"><br /><br />

<label for="UserName">User Name</label>
<input type="text" name="UserName"><br /><br />

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

<input type="submit" value="Submit">
</form>
</body>

Comments:

No comment to show.

Available Services
Download Android Application