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

make a php page doing the following that will correspond with a mysql database.

ID: 3727307 • Letter: M

Question

make a php page doing the following that will correspond with a mysql database. what is the code?

This assignment you will be making a form that will do one of three things in a database

-          It will add a record

-          It will update a record

-          It will search for a record

Your database will contain a table for keeping a record of all your friends and family and should contain:

First name

Last name

Phone number

Address

City

State

Zip

Birthdate

Username

Password

The sex of the person

And the relationship

You should also keep a primary key for the user

You will then have two pages a friends and family form page and a results page

The form will ask the user to enter the information and then choose to either

Create a new record

Update and existing record

Search for a record

The results page will display the information that they are requesting with either adding a record, updating it or displaying the results of a record.

Explanation / Answer

First of all, we will create a new MySQL database. Let us name the database as 'test'

create database test;

Then, we will create a new table in database ‘test’. Let us name the table as ‘users‘.

use test;

CREATE TABLE users (
id int(11) NOT NULL auto_increment,
name varchar(100) NOT NULL,
age int(3) NOT NULL,
email varchar(100) NOT NULL,
PRIMARY KEY (id)
);

Now, we will create a config.php file which contains database connection code. This code connects to the MySQL database. This file is included in all PHP pages where database connection is necessary.

config.php

In below code, the database host name is localhost where username=root and password=root. The database test has been selected.

<?php

/*

// mysql_connect("database-host", "username", "password")

$conn = mysql_connect("localhost","root","root")

            or die("cannot connected");

// mysql_select_db("database-name", "connection-link-identifier")

@mysql_select_db("test",$conn);

*/

/**

* mysql_connect is deprecated

* using mysqli_connect instead

*/

$databaseHost = 'localhost';

$databaseName = 'test';

$databaseUsername = 'root';

$databasePassword = 'root';

$mysqli = mysqli_connect($databaseHost, $databaseUsername, $databasePassword, $databaseName);

?>

To add data into database, we need an html form.

add.html

<html>

<head>

    <title>Add Data</title>

</head>

<body>

    <a href="index.php">Home</a>

    <br/><br/>

    <form action="add.php" method="post" name="form1">

        <table width="25%" border="0">

            <tr>

                <td>Name</td>

                <td><input type="text" name="name"></td>

            </tr>

            <tr>

                <td>Age</td>

                <td><input type="text" name="age"></td>

            </tr>

            <tr>

                <td>Email</td>

                <td><input type="text" name="email"></td>

            </tr>

            <tr>

                <td></td>

                <td><input type="submit" name="Submit" value="Add"></td>

            </tr>

        </table>

    </form>

</body>

</html>

Form action on add.html is add.php. It means that the submitted form data will go to add.php. In add.php, we do a simple validation of checking if the entered name, email & age are empty or not. If they are all filled then the data will be inserted into database table.

add.php

<html>

<head>

    <title>Add Data</title>

</head>

<body>

<?php

//including the database connection file

include_once("config.php");

if(isset($_POST['Submit'])) {    

    $name = $_POST['name'];

    $age = $_POST['age'];

    $email = $_POST['email'];

        

    // checking empty fields

    if(empty($name) || empty($age) || empty($email)) {                

        if(empty($name)) {

            echo "<font color='red'>Name field is empty.</font><br/>";

        }

        

        if(empty($age)) {

            echo "<font color='red'>Age field is empty.</font><br/>";

        }

        

        if(empty($email)) {

            echo "<font color='red'>Email field is empty.</font><br/>";

        }

        

        //link to the previous page

        echo "<br/><a href="#">Go Back</a>";

    } else {

        // if all the fields are filled (not empty)             

        //insert data to database

        $result = mysqli_query($mysqli, "INSERT INTO users(name,age,email) VALUES('$name','$age','$email')");

        

        //display success message

        echo "<font color='green'>Data added successfully.";

        echo "<br/><a href="index.php">View Result</a>";

    }

}

?>

</body>

</html>

Data from database is fetched and displayed in index.php file. This is our homepage. This file also contains a link to add data. On every row of displayed data, there is also a link to edit and delete data. Below is a sample image of our homepage.

index.php

<?php

//including the database connection file

include_once("config.php");

//fetching data in descending order (lastest entry first)

//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated

$result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC"); // using mysqli_query instead

?>

<html>

<head>    

    <title>Homepage</title>

</head>

<body>

    <a href="add.html">Add New Data</a><br/><br/>

    <table width='80%' border=0>

        <tr bgcolor='#CCCCCC'>

            <td>Name</td>

            <td>Age</td>

            <td>Email</td>

            <td>Update</td>

        </tr>

        <?php

        //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array

        while($res = mysqli_fetch_array($result)) {         

            echo "<tr>";

            echo "<td>".$res['name']."</td>";

            echo "<td>".$res['age']."</td>";

            echo "<td>".$res['email']."</td>";    

            echo "<td><a href="edit.php?id=$res[id]">Edit</a> | <a href="delete.php?id=$res[id]">Delete</a></td>";        

        }

        ?>

    </table>

</body>

</html>

Each row of data can be edited separately. Row ID is passed in the URL of edit.php. ID uniquely identifies the data entry.

While adding data, we had two files: add.html and add.php. While editing data, I have kept the entire thing in a single edit.php file. Edit form in HTML and database update code in PHP are present in the same file.

In the code below, at first a single row entry of data is fetched based on the id. The fetched data is displayed in the edit form. When user edits the data and submits the form, then some simple validation is done for empty data. When everything is correct, then that particular entry of data is updated in database.

edit.php

<?php

// including the database connection file

include_once("config.php");

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

{    

    $id = $_POST['id'];

    

    $name=$_POST['name'];

    $age=$_POST['age'];

    $email=$_POST['email'];    

    

    // checking empty fields

    if(empty($name) || empty($age) || empty($email)) {            

        if(empty($name)) {

            echo "<font color='red'>Name field is empty.</font><br/>";

        }

        

        if(empty($age)) {

            echo "<font color='red'>Age field is empty.</font><br/>";

        }

        

        if(empty($email)) {

            echo "<font color='red'>Email field is empty.</font><br/>";

        }        

    } else {    

        //updating the table

        $result = mysqli_query($mysqli, "UPDATE users SET name='$name',age='$age',email='$email' WHERE id=$id");

        

        //redirectig to the display page. In our case, it is index.php

        header("Location: index.php");

    }

}

?>

<?php

//getting id from url

$id = $_GET['id'];

//selecting data associated with this particular id

$result = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id");

while($res = mysqli_fetch_array($result))

{

    $name = $res['name'];

    $age = $res['age'];

    $email = $res['email'];

}

?>

<html>

<head>    

    <title>Edit Data</title>

</head>

<body>

    <a href="index.php">Home</a>

    <br/><br/>

    

    <form name="form1" method="post" action="edit.php">

        <table border="0">

            <tr>

                <td>Name</td>

                <td><input type="text" name="name" value="<?php echo $name;?>"></td>

            </tr>

            <tr>

                <td>Age</td>

                <td><input type="text" name="age" value="<?php echo $age;?>"></td>

            </tr>

            <tr>

                <td>Email</td>

                <td><input type="text" name="email" value="<?php echo $email;?>"></td>

            </tr>

            <tr>

                <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>></td>

                <td><input type="submit" name="update" value="Update"></td>

            </tr>

        </table>

    </form>

</body>

</html>

Each row of data can be deleted separately. Row ID is passed in the URL of delete.php. ID uniquely identifies the data entry. After deletion, the user is redirected to homepage (index.php)

delete.php

//including the database connection file

include("config.php");

//getting id of the data from url

$id = $_GET['id'];

//deleting the row from table

$result = mysqli_query($mysqli, "DELETE FROM users WHERE id=$id");

//redirecting to the display page (index.php in our case)

header("Location:index.php");