0% found this document useful (0 votes)
4 views13 pages

Database Operation Program

The document contains PHP scripts for managing a simple user login system, including creating a database, inserting, selecting, updating, and deleting user records. It establishes a connection to a MySQL database and provides forms for user registration and password management. The scripts handle user input, validate data, and display appropriate messages based on the success or failure of database operations.

Uploaded by

Jeya Selvakumari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Database Operation Program

The document contains PHP scripts for managing a simple user login system, including creating a database, inserting, selecting, updating, and deleting user records. It establishes a connection to a MySQL database and provides forms for user registration and password management. The scripts handle user input, validate data, and display appropriate messages based on the success or failure of database operations.

Uploaded by

Jeya Selvakumari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CREATEQUERY.

PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
//create database
$sql = "CREATE DATABASE miniproject3";
if ($conn->query($sql) === TRUE) {
echo "<br>"."<br>"."1. Database created successfully";
} else {
echo "<br>"."<br>"."1. Error creating database: " . $conn->error;
}
//select database
$sql= "USE miniproject3";
if ($conn->query($sql) === TRUE) {
echo "<br>"."<br>"."2. Database selected successfully";
} else {
echo "<br>"."<br>"."2. Error selecting Database: " . $conn->error;
}
// sql to create table
$sql = "CREATE TABLE tbllogin (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(20) NOT NULL,
password VARCHAR(30) NOT NULL,
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "<br>"."<br>"."3. Table created successfully";
} else {
echo "<br>"."<br>"."3. Error creating table: " . $conn->error;
}
//Connection Closed
$conn->close();

?>

[Link]
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="miniproject3";
$conn = new mysqli($servername, $username, $password, $db);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>

[Link]
<table>
<tr>
<td>
Other database Process
</td>
</tr>
<tr>
<td><a href="[Link]"><button>Insert</button></a>
<a href="[Link]"><button>Select</button></a>
<a href="[Link]"><button>Update</button></a>
<a href="[Link]"><button>Delete</button></a>
</td></tr><tr>
<td colspan="4"> <h4 align="center"><a href="../">Log Out</a></h4>
</td>
</tr>
</table>

[Link]
<?php
if(!empty($_POST["insert"])) {

require_once("[Link]");
if($_POST['pwd'] != $_POST['cpwd']){
?><script type="text/javascript">alert("Password not
Match");</script><?php
}
else{
$username = $_POST['uname'];
$slquery = "SELECT username FROM tbllogin WHERE username =
'$username'";
$result = mysqli_query($conn, $slquery);
if (mysqli_num_rows($result) > 0) {
?><script type="text/javascript">alert("Username Already
Exists");</script><?php
}
else {
$query = "INSERT INTO tbllogin (username, password) VALUES
('" . $_POST["uname"] . "', '" . $_POST["pwd"]. "')";
if (mysqli_query($conn, $query)) {
?><script type="text/javascript">alert("Username & Password
Created");</script><?php
} else {
?><script type="text/javascript">alert("<?php echo
"Error: " . $query . "<br>" . mysqli_error($conn);?>");</script><?php

}
}
}

}
?>
<html>
<head>
<title>Insert</title>
</head>
<body>
<center>
<form method="POST" name="createmsg" action="">
<table border="0">
<tr><td colspan="2" bgcolor="#CBD884">Register Username &
Password (Insert)</td></tr>

<tr>
<td>Username</td>
<td><input type="text" name="uname"
placeholder="Enter Username" required></td>
</tr>
<tr>
<td>Password</td>
<td><input type="Password" name="pwd"
placeholder="Enter Password" required></td>
</tr>
<tr>
<td>Confrim Password</td>
<td><input type="Password" name="cpwd"
placeholder="Enter Confirm Password" required></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="insert"
value="Submit">
<a href="[Link]" style="text-decoration: none;
color: red;">Forgot Password</a></td>
</tr>
</table>

</form>
<?php include '[Link]';?>
</center>

</body>
</html>

[Link]
<html>
<head>
<title>Select | Delete</title>
</head>
<body>
<center>
<form method="post" action="">
<table border="0">
<tr><td colspan="5" bgcolor="#CBD884">View or Delete
Password (Select || Delete)</td></tr>
<tr>
<td>Username</td>
<td><input type="text" name="uname"
placeholder="view or Delete password"></td>
<td ><input type="submit" name="delete"
value="Delete"></td>
<td ><input type="submit" name="select"
value="view"></td>
<td ><input type="submit" name="selectall" value="View
All"></td>
</tr>
</table>
</form>
<?php
/*==========VIEW============ */
if(!empty($_POST["select"])) {
require_once("[Link]");
$username = $_POST['uname'];
$query = "SELECT * FROM tbllogin WHERE username='$username'";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0){
echo "<center><table border=1>";
echo "<tr>";
echo "<td colspan=4 bgcolor=#84D8A6>Viewing data from
database</td>";
echo "<tr>";

echo "<th>Username</th>";
echo "<th>Password</th>";
echo "<th>Reg Date Time</th>";
echo "</tr>";
while($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['reg_date'] . "</td>";

echo "</tr>";
}
} else{
echo "<center><table border=1>";
echo "<tr><td colspan=4 bgcolor=#84D8A6>No Matching
Records</td></tr></table>";

}
}
/*==========VIEW ALL============ */
if(!empty($_POST["selectall"])) {
require_once('[Link]');

$query = "SELECT * FROM tbllogin";


$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0){
echo "<center><table border=1>";
echo "<tr>";
echo "<td colspan=4 bgcolor=#84D8A6>Viewing data from
database</td>";
echo "<tr>";

echo "<th>Username</th>";
echo "<th>Password</th>";
echo "<th>Reg Date Time</th>";
echo "</tr>";
while($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['reg_date'] . "</td>";

echo "</tr>";

}
} else{
echo "<center><table border=1>";
echo "<tr><td colspan=4 bgcolor=#84D8A6>No Matching
Records</td></tr></table>";

}
/*==========DELETE============ */
if(!empty($_POST["delete"])) {
require_once('[Link]');
$username = $_POST['uname'];
$sql = "DELETE FROM tbllogin WHERE username='$username'";

if (mysqli_query($conn, $sql)>0) {
?><script type="text/javascript">alert("Record deleted
successfully");</script><?php

} else {
?><script type="text/javascript">alert(<?php echo "Error deleting
record: " . mysqli_error($conn); ?>);</script><?php

}
}
?>

<?php include('[Link]');?>
</center>

</body>
</html>

[Link]

<?php
if(!empty($_POST["update"])) {
require_once("[Link]");
if($_POST['pwd'] != $_POST['cpwd']){
?><script type="text/javascript">alert("Password not
Match");</script><?php
}
else{
$username = $_POST['uname'];

$slquery = "SELECT username FROM tbllogin WHERE


username = '$username'";
$result = mysqli_query($conn, $slquery);
if (mysqli_num_rows($result)) {
$sql = "UPDATE tbllogin SET password='" . $_POST["pwd"]. "'
WHERE username='$username'";
if (mysqli_query($conn, $sql)) {
?><script type="text/javascript">alert("Password
updated successfully");</script><?php
} else {
?><script type="text/javascript">alert(<?php echo "Error
updating Password. Check username is valid or not".
mysqli_error($conn);?>);</script><?php
}
}else{
?><script type="text/javascript">alert("Username
Not Exists");</script><?php
}

}
}

?>
<html>
<head>
<title>Forgot Password</title>
</head>
<body>
<center>
<form method="post" name="createmsg" action="">
<table border="0">
<tr><td colspan="2" bgcolor="#CBD884">Change
Password (Update)</td></tr>

<tr>
<td>Username</td>
<td><input type="text" name="uname"
placeholder="Enter existing Username" required></td>
</tr>
<tr>
<td>New Password</td>
<td><input type="Password" name="pwd"
placeholder="We hope this is your own id" required></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="Password" name="cpwd"
placeholder="Enetr Confirm password" required></td>
</tr>
<tr>
<td colspan="2"><input type="submit"
name="update" value="Update">
</td>
</tr>
</table>
</form>
<?php include('[Link]');?>
</center>

</body>
</html>

You might also like