Вы находитесь на странице: 1из 8

Database Connectivity in PHP/MySQL

PRACTICE EXERCISE:

Database name: sitename Table name: users

Table Structure:

Database name: sitename Table name: users Table Structure: <?php // Script name: mysql_connect.php // Description:

<?php

// Script name: mysql_connect.php

// Description: To connect to database

$username ="root"; $password=""; $database="sitename";

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die ("Unable to select database"); ?>

<?php

 

// Script name: menu.php

// Description: The page to display the menu

?>

<html>

<body>

<h3>Menu</h3>

<ul>

<li> <a href="login.php">Login</a></li> <li> <a href="register.php">Register</a></li> <li> <a href="search.php">Search Record</a></li> <li> <a href="delete.php">Delete Record</a></li> <li> <a href="update.php">Update Record</a></li> <li> <a href="view.php">View Users</a></li>

</ul>

</body>

</html>

Output:

Users</a></li> </ul> </body> </html> Output: 1

Database Connectivity in PHP/MySQL

<?php

// Script name: register.php

// Description: The page to sign in or to register record in the database ?>

<h2>Register</h2>

<form action="save.php" method="post"> First Name: <input type="text" name="first_name"><br> Last Name: <input type="text" name="last_name"><br> Email Address: <input type="text" name="email"><br> Password: <input type="password" name="password"> <br><br> <input type="submit" name="submit" value="Register"><br> </form> <br><a href="menu.php">Menu</a>

Output:

href="menu.php">Menu</a> Output: <?php // Script name: save.php // Description: To save

<?php

// Script name: save.php

// Description: To save the record in the database

//check if the form has been submitted $fn = $_POST['first_name']; $ln = $_POST['last_name']; $e = $_POST['email']; $p = $_POST['password'];

if (isset($_POST['submit']))

{

include('mysql_connect.php');

Output:

{ include('mysql_connect.php'); Output: $query = "INSERT INTO users(first_name, last_name,

$query = "INSERT INTO users(first_name, last_name, email, password, registration_date) VALUES('$fn', '$ln', '$e', SHA('$p'), NOW())"; $result = @mysql_query($query);

echo "<h1>Thank you

<p> You are now registered

</h1>

</p>";

mysql_close();

}

?> <br><br><a href="menu.php">Menu</a>

Database Connectivity in PHP/MySQL

<?php

// Script name: view.php

// Description: The display/view all records in the database

include('mysql_connect.php');

$query = "select user_id, CONCAT(last_name, ', ',first_name) as name, email, DATE_FORMAT(registration_date,'%M %d, %Y') as dr FROM users"; $result = @mysql_query($query);

mysql_close(); ?> <h2>GUEST INFORMATION</h2>

<?php

$i=1;

while ($row = mysql_fetch_array($result))

{

echo "<b>#:</b>" . $i . "<br>"; echo "<b>ID:</b>" . $row[0] . "<br>"; echo "<b>Name:</b>" . $row[1] . "<br>"; echo "<b>Email:</b>" . $row[2] . "<br>"; echo "<b>Date registered:</b>" . $row[3] . "<br>"; echo "<br>"; $i++;

}

mysql_free_result($result);

Output:

$i++; } mysql_free_result($result); Output: echo "<br> <b>Total Records: " .

echo "<br> <b>Total Records: " . ($i-1) . "<br>"; ?> <br><a href = "menu.php"><center>Menu</a>

Output:

<?php // Script name: search.php // Description: To search for a particular record using user_id
<?php
// Script name: search.php
// Description: To search for a particular record using user_id as the
key field
?>
<h2>Search</h2>
<form action='search.php' method='post'>
<p>User ID: <input type="text" name="user_id" size=20
maxlength=40 /></p>
<p><input type='submit' name='search' value='Search' /></p>
</form>
<?php

if (isset($_POST['search']))

{

Database Connectivity in PHP/MySQL

include('mysql_connect.php'); $id = $_POST['user_id'];

$query = "Select user_id, first_name, last_name, email, registration_date FROM users WHERE user_id='$id' "; $result = @mysql_query($query); $row = mysql_fetch_array($result);

if ($row)

{

echo "User ID echo "Name echo "Email Address echo "Date registered

: " . $row[0] . "<br>"; : " . $row[1] . " " . $row[2] . "<br>"; : " . $row[3] . "<br>"; : " . $row[4] . "<br>";

}

else

echo "No record found

";

}

?> <br><br><a href="menu.php">Menu</a>

<?php

// Script name: update.php

// Description: The page that search for the specific record then will edit/modify the searched record

?>

<h2>Update Record</h2> <form action method='post'> <p>User ID: <input type="text" name="user_id" size=20 maxlength=40 /></p> <p><input type='submit' name='search' value='Search' /></p> </form>

<?php if (isset($_POST['search']))

{

include('mysql_connect.php'); $id = $_POST['user_id'];

$query = "Select user_id, first_name, last_name, email, registration_date FROM users WHERE user_id='$id' "; $result = @mysql_query($query); $row = mysql_fetch_array($result);

Output:

$row = mysql_fetch_array($result); Output: if ($row) { echo "<form action='updated.php'

if ($row)

{

echo "<form action='updated.php' method='post'>";

echo "User ID echo "First Name

: " . "<input type=hidden name=user_id value='$row[0]'>$row[0]" . "<br>";

: " . "<input type=text name=fn value='$row[1]'> <br>";

Database Connectivity in PHP/MySQL

echo "Last Name echo "Email Address echo "Date registered

: " . "<input type=text name=ln value='$row[2]'> <br>"; : " . "<input type=text name=em value='$row[3]'> <br>"; : " . $row[4] . "<br>";

echo "

<p><input type='submit' name='update' value='Update' /></p> </form>";

}

else

echo "No record found

";

}

?>

<?php

// Script name: updated.php

// Description: To save/update modified record

if (isset($_POST['update']))

{

include('mysql_connect.php'); $id = $_POST['user_id']; $first = $_POST['fn']; $last = $_POST['ln']; $email = $_POST['em'];

Output:

$_POST['ln']; $email = $_POST['em']; Output: $query = "Select * FROM users"; $result =

$query = "Select * FROM users"; $result = @mysql_query($query); $row = mysql_fetch_array($result);

if ($row)

{

$query = "Update users set first_name='$first', last_name='$last', email='$email' WHERE user_id='$id'";

mysql_query($query); echo "<h2>Record has been updated

}

</h2>”;

}

?> <a href = "menu.php">Menu</a>

Database Connectivity in PHP/MySQL

<?php

// Script name: delete.php

// Description: To delete for a particular record using user_id as the key field ?> <h2>Delete Record</h2> <form method='post'> <p>User ID: <input type="text" name="user_id" size=20 maxlength=40 /></p> <p><input type='submit' name='search' value='Search' /></p> </form>

<?php

if (isset($_POST['search']))

{

include('mysql_connect.php'); $id = $_POST['user_id'];

$query = "Select user_id, first_name, last_name, email, registration_date FROM users WHERE user_id='$id' "; $result = @mysql_query($query); $row = mysql_fetch_array($result);

if ($row)

{

echo "User ID echo "Name echo "Email Address echo "Date registered

: " . $row[0] . "<br>"; : " . $row[1] . " " . $row[2] . "<br>"; : " . $row[3] . "<br>"; : " . $row[4] . "<br>";

Output:

: " . $row[4] . "<br>"; Output: echo "<form action='deleted.php'

echo "<form action='deleted.php' method='post'>

<br><h4>Deleting Record

<p><input type=hidden name='user_id' value='$_POST[user_id]'>

Are you sure?</h4>

<input type=radio name='ans' value='Yes'>YES <input type=radio name='ans' value='No'>NO <br><br> <p><input type='submit' name='delete' value='Delete' /></p> </form>";

}

else

echo "No record found

";

}

?> <br><br><a href="menu.php">Menu</a>

Database Connectivity in PHP/MySQL

<?php

// Script name: deleted.php

// Description: Deleted page to delete record in the database

if (isset($_POST['delete']))

if (isset($_POST['delete']))

{

if ($_POST['ans']=="Yes")

{

 

include('mysql_connect.php'); $id = $_POST['user_id'];

$query = "Select * FROM users"; $result = @mysql_query($query); $row = mysql_fetch_array($result);

if ($row)

{

 

$query = "Delete from users WHERE user_id='$id'"; mysql_query($query);

echo "<center><h2>Record has been deleted

</h2></center>";

 

}

}

else

echo "<center><h2>No deletion made

</h2></center>";

}

?> <br><br><a href="menu.php">Menu</a>

<?php

Output:

// Script name: login.php

<?php Output: // Script name: login.php // Description: To log-in using email address and password

// Description: To log-in using email address and password ?>

<h2>Login</h2>

<form action='login.php' method='post'> Email Address: <input type="text" name="email" size=20 maxlength=40><br> Password: <input type="password" name="password" size=20 maxlength=20> <br><br><input type='submit' name='submit' value='Login'> </form> <?php if (isset($_POST['submit']))

{

include('mysql_connect.php');

$e = $_POST['email']; $p = $_POST['password']; if(!empty($_POST['email']) || !empty($_POST['password']))

Database Connectivity in PHP/MySQL

{

 

$query = "Select user_id, first_name FROM users WHERE email='$e' AND password=SHA('$p')"; $result = @mysql_query($query); $row = mysql_fetch_array($result,MYSQL_NUM);

if ($row)

{

echo "<h1>Welcome!

$row[1]</h1><br><br>";

echo '<a href="main.php">Continue'; exit();

}

else

{

echo 'The email address and password entered do not match those on file.';

}

}

else

echo 'Please enter email and/or password mysql_close();

';

}

?> <br><br><a href="menu.php">Menu</a>