Im trying to write php for a library database with the Title,author,and ISBN as
ID: 3837309 • Letter: I
Question
Im trying to write php for a library database with the Title,author,and ISBN as fields in the databases table but I can't figure out the .phps. I have the login and
registration almost done. Code of what I have is below. Please help.
/////////////////////////////////////////////////////////////////////////////////////////////
LogInForm.html
<html>
<head>
<title>User Login</title>
</head>
<body>
<h2>User LogIn</h2>
<form method="POST" action="loginProcess.php">
Username: <input type = "text" name = "user"><br><br>
Password: <input type = "password" name = "pw"><br><br>
<input type = "submit" value="Log In"><br><br>
New User? <a href="Registration.html"> Click Here</a>
</body>
</html>
/////////////////////////////////////////////////////////////////////////////////////
Registration_Form.php
<?php
require_once 'Registration_process.php';
?>
<html>
<head>
<title>Michael McCann</title>
</head>
<body>
<form method="POST" action="loginProcess.php">
UserName: <input type = "text" name = "num1"><br><br>
Email: <input type = "text" name = "num2"><br><br>
Password: <input type = "password" name = "num3"><br><br>
Confirm Password: <input type = "password" name = "num4"><br><br>
<input type = "submit" value="Sign Up">
</body>
</html>
/////////////////////////////////////////////////////////////////////////////
registration_processes.php
<?php
$Username = $Email =$Password = $ConfirmPassword = "";
if (isset($connection['Username']))
$UserName = mysql_entities_fix_string($connection['UserName']);
if (isset($connection['Email']))
$Email= mysql_entities_fix_string($connection['Email']);
if (isset($connection['Password']))
$Password = mysql_entities_fix_string($connection['Password']);
if (isset($connection['ConfirmPassword']))
$ConfirmPassword = mysql_entities_fix_string($connection['ConfirmPassword']);
$fail .= validate_username($UserName);
$fail .= validate_email($Email);
$fail .= validate_password($Password);
$fail .= validate_email($ConfirmPassword);
?>
Explanation / Answer
dbconnection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phptask";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
setubdb.php
<?php
include('dbconnection.php');
// sql to create table
$sql = "CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
email VARCHAR(30) NOT NULL,
password VARCHAR(50),
confirmed_password VARCHAR(50)
)";
if ($conn->query($sql) === TRUE) {
echo "Table users created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// sql to create table
$sql = "CREATE TABLE library_table (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Title VARCHAR(30) NOT NULL,
author VARCHAR(30) NOT NULL,
ISBN VARCHAR(50)
)";
if ($conn->query($sql) === TRUE) {
echo "Table library_table created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
?>
register.php
<?php
include('dbconnection.php');
if(isset($_POST['submit'])){
$name=$_POST['uname'];
$email=$_POST['email'];
$password=$_POST['pass'];
$confirm_pass=$_POST['cpass'];
$sql="SELECT 1 FROM users WHERE username = '$name'";
if(strlen($name)<5){
$msg="username is minimum 5 characters";
}
elseif(strlen($password)<6){
$msg="password is minimum 6 characters";
}
elseif($password != $confirm_pass){
$msg = "password and confirmed password doesn't match";
}
else{
$sql = "INSERT INTO users (username,email,password,confirmed_password)
VALUES ('$name', '$email', '$password','$confirm_pass')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
$name="";
$email="";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h4>Register Page</h4>
<form action="" method="post">
<table>
<?php
if(isset($msg))
echo $msg;
?>
<tr><td>Username : </td><td><input type="text" name="uname" value="<?php if(isset($name)) echo $name ?>" required></td></tr>
<tr><td>Email : </td><td><input type="email" name="email" value="<?php if(isset($email)) echo $email ?>" required></td></tr>
<tr><td>Password : </td><td><input type="Password" name="pass" required></td></tr>
<tr><td>Confirm Password : </td><td><input type="Password" name="cpass" required></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>
login.php
<?php
include('dbconnection.php');
if(isset($_POST['submit'])){
$name=$_POST['uname'];
$password=$_POST['pass'];
$sel_user = "select * from users where username='$name' AND password='$password'";
$run_user = mysqli_query($conn, $sel_user);
$check_user = mysqli_num_rows($run_user);
if($check_user>0){
$_SESSION['username']=$name;
echo "<script>window.open('index.php','_self')</script>";
}
else {
$msg="username or password is not correct, try again!";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="" method="post">
<h4>Login Page</h4>
<table>
<?php
if(isset($msg))
echo $msg;
?>
<tr><td>Username : </td><td><input type="text" name="uname" value="<?php if(isset($name)) echo $name ?>" required></td></tr>
<tr><td>Password : </td><td><input type="Password" name="pass" required></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>
index.php
<?php
include('dbconnection.php');
// get the records from the database
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<header>
</header>
<div>
<?php
$sql = "select * from library_table ORDER BY id";
$result = mysqli_query($conn, $sql);
// display records in a table
echo "<table border='1' cellpadding='10'>";
// set table headers
echo "<tr><th>ID</th><th>Title</th><th>Author</th><th>ISBN</th><th></th></tr>";
while($row = mysqli_fetch_assoc($result))
{
$id=$row['id'];
$title = $row['Title'];
$author = $row['author'];
$isbn = $row['ISBN'];
// set up a row for each record
echo "<tr>";
echo "<td>" . $id . "</td>";
echo "<td>" . $title. "</td>";
echo "<td>" . $author . "</td>";
echo "<td>" .$isbn . "</td>";
echo "<td><a href="delete.php?id=" . $id . "">Delete</a></td>";
echo "</tr>";
}
echo "</table>";
?>
</div>
<footer>
</footer>
</body>
</html>
add.php
<?php
include('dbconnection.php');
if(isset($_POST['submit'])){
$title=$_POST['title'];
$author=$_POST['author'];
$isbn=$_POST['isbn'];
$sql = "INSERT INTO library_table (title,author,isbn)
VALUES ('$title', '$author', '$isbn')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
$name="";
$email="";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h4>Add Records</h4>
<form action="" method="post">
<table>
<tr><td>Title : </td><td><input type="text" name="title" value="<?php if(isset($title)) echo $title ?>" required></td></tr>
<tr><td>Author : </td><td><input type="text" name="author" value="<?php if(isset($author)) echo $author ?>" required></td></tr>
<tr><td>ISBN : </td><td><input type="text" name="isbn" value="<?php if(isset($isbn)) echo $isbn ?>" required></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit"></td></tr>
</table>
</form>
</body>
</html>
delete.php
<?php
include('dbconnection.php');
$sel_user = "delete * from users where id='id'";
$run_user = mysqli_query($conn, $sel_user);
echo "<script>window.open('index.php','_self')</script>";
?>
logout.php
<?php
session_start();
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
if(isset($_GET['logout']))
{
session_destroy();
unset($_SESSION['user']);
header("Location: login.php");
}
?>