You are on page 1of 8

Database Connectivity in PHP/MySQL

PRACTICE EXERCISE: Database name: sitename Table name: users Table Structure:

<?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:

Database Connectivity in PHP/MySQL


<?php Output:

// 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>

<?php

Output:

// 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'); $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...</h1> <p> You are now registered...</p>"; mysql_close(); } ?> <br><br><a href="menu.php">Menu</a>

Database Connectivity in PHP/MySQL


<?php Output:

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

// 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'])) { 3

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 : " . $row[0] . "<br>"; echo "Name : " . $row[1] . " " . $row[2] . "<br>"; echo "Email Address : " . $row[3] . "<br>"; echo "Date registered : " . $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); if ($row) { echo "<form action='updated.php' method='post'>"; echo "User ID : " . "<input type=hidden name=user_id value='$row[0]'>$row[0]" . "<br>"; echo "First Name : " . "<input type=text name=fn value='$row[1]'> <br>"; 4 Output:

Database Connectivity in PHP/MySQL


echo "Last Name echo "Email Address echo "Date registered echo " <p><input type='submit' name='update' value='Update' /></p> </form>"; } else echo "No record found..."; } ?> <?php : " . "<input type=text name=ln value='$row[2]'> <br>"; : " . "<input type=text name=em value='$row[3]'> <br>"; : " . $row[4] . "<br>";

// 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']; $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> Output:

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

Output:

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

echo "<form action='deleted.php' method='post'> <br><h4>Deleting Record.... Are you sure?</h4> <p><input type=hidden name='user_id' value='$_POST[user_id]'> <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 ($_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


// 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'])) 7

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>