Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(PHP) 1) Create a database with the following requirements: - Name: userdb - Tab

ID: 3686656 • Letter: #

Question

(PHP)

1) Create a database with the following requirements:

- Name: userdb

- Table: usertb

- Fields: username, password

Add these 2 records to the table:

- userone, pwo

- usertwo, pwt

2) Write and test a function that has the following requirements:

- the function accepts 2 variables (username & password)

- the function connect to the database created in section 1 (above)

- the function returns true if the username exist in the database and matches its password else, it returns false

- remember to close the connection to the database before the return so other parts of this program will work properly

3) Create a login interface and 2 additional pages with the following requirements:

- login page (name it as login.php) that uses the function created in section 2 (above) to authenticate the login information submitted by the user

- if the login information passes, then give the user access to the main page (name it as main.php). The main page displays the records (all records) of the table usertb (created in section 1 above)

- the main page also has a link that allows the user to log out to end the session and take the user to the logout page (name it as logout.php)

4) Add the following requirements to the main page

- 2 text boxes and a button that allows the ability to add more records to the usertb table (one record at a time)

- no data validation is required

- this can be done via function or non-function (you choose)

Explanation / Answer

<?php

$link = mysqli_connect("localhost", "root", "");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt create database query execution
$sql = "CREATE DATABASE userdb";
if(mysqli_query($link, $sql)){
echo "Database demo created successfully";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);

?>

<?php

$link = mysqli_connect("localhost", "root", "", "userdb");

// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt create table query execution
$sql = "CREATE TABLE usertb(username VARCHAR(50) NOT NULL PRIMARY KEY,password VARCHAR(50))";
if (mysqli_query($link, $sql)){
echo "Table persons created successfully";
} else {
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
?>


Home.php:

<?php
session_start();
function valdate_unAndPass($name,$pwd){
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('userdb') or die(mysql_error());
if($name!=''&&$pwd!='')
{
$query=mysql_query("select * from usertb where name='".$name."' and password='".$pwd."'") or die(mysql_error());
$res=mysql_fetch_row($query);
if($res)
{
    return true;
}

}
return false;
}

if(isset($_POST['submit'])) /* i.e. the PHP code is executed only when someone presses Submit button in the below given HTML Form */
{
$un = $_POST['user_name'];
$pw = $_POST['password']; // Here $var is the input taken from user.
if(valdate_unAndPass($un,$p)){
$_SESSION['name']=$name;
header('location:login.php');
}else{
echo'You entered username or password is incorrect';
}
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" name="user_name">
<input type="text" name="password">
<input type="submit" name="submit">
</form>

login.php:
<?php
session_start();
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('userdb') or die(mysql_error());
$query=mysql_query("select * from usertb where name='".$name."' and password='".$pwd."'") or die(mysql_error());
$res=mysql_fetch_row($query);
echo ''.$res['username'].' Password: '.$res['password'];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Log out</title>
</head>
<body>
<a href="logout.php">Log Out Btn</a>
</body>
</html>


logout.php:
<?php   
session_start(); //to ensure you are using same session
session_destroy(); //destroy the session
header("location:/index.php"); //to redirect back to "index.php" after logging out
exit();
?>

IsertData.php:

<html>

<head>
<title>Add New Record in MySQL Database</title>
</head>

<body>
<?php
if(isset($_POST['add'])) {
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('userdb') or die(mysql_error());
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
  
if(! get_magic_quotes_gpc() ) {
$un = addslashes ($_POST['user_name']);
$pwd = addslashes ($_POST['password']);
}else {
$un = $_POST['user_name'];
$pwd = $_POST['password'];
}
  
$sql = "INSERT INTO usertb VALUES('$user_name','$pwd')";

$retval = mysql_query( $sql, $conn );
  
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
  
echo "Entered data successfully ";
  
mysql_close($conn);
}else {
?>
  
<form method = "post" action = "<?php $_PHP_SELF ?>">
<table width = "400" border = "0" cellspacing = "1"
cellpadding = "2">
  
<tr>
<td width = "100">User Name</td>
<td><input name = "user_name" type = "text"
id = "emp_name"></td>
</tr>
  
<tr>
<td width = "100">Password</td>
<td><input name = "password" type = "text"
id = "emp_address"></td>
</tr>
  

  
<tr>
<td width = "100"> </td>
<td> </td>
</tr>
  
<tr>
<td width = "100"> </td>
<td>
<input name = "add" type = "submit" id = "add"
value = "Add User">
</td>
</tr>
  
</table>
</form>
  
<?php
}
?>

</body>
</html>