0% found this document useful (0 votes)
37 views11 pages

COS104 Handout Continuation

This document discusses creating PHP files to connect to a MySQL database and perform CRUD operations. It describes: 1) Creating a conn.php file to define the database connection parameters. 2) Creating an insert.php file with a form to add data to the database table. It imports conn.php and inserts the form data using a SQL query. 3) Creating a view.php file to display the database table contents. It imports conn.php, runs a SQL select query, and uses a while loop to output each row of the table in HTML.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views11 pages

COS104 Handout Continuation

This document discusses creating PHP files to connect to a MySQL database and perform CRUD operations. It describes: 1) Creating a conn.php file to define the database connection parameters. 2) Creating an insert.php file with a form to add data to the database table. It imports conn.php and inserts the form data using a SQL query. 3) Creating a view.php file to display the database table contents. It imports conn.php, runs a SQL select query, and uses a while loop to output each row of the table in HTML.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

COS104: Database Application: MySQL Continuation:

As a sample database:

Database Name: DB_AISAT

Table Name: tbl_students

ID Int, PRIMARY KEY, AUTO INCREMENT


Last_Name Varchar 255
First_Name Varchar 255
Middle_Name Varchar 255
Addr Varchar 255
Now we will create a PHP File that uses textboxes and button to add the data to our database table

First, we need to create a file named: conn.php conn.php


will just hold our connection file to our database

<?php
$host = "localhost";
$username = "root";
$pass = "";
$database = "db_aisat";
$connection = mysqli_connect($host,$username,$pass,$database); if
($connection)
{
//now if you sure that your connection is working fine you can remove the message below echo
"Connected";
}
else
{
echo "Error Connecting";
}
?>
Actual Code:

Then next we will create a new file name insert.php

1st we will need to import our connection file to our insert file make sure that your conn.php and
insert.php are in the same folder

<?php

//the code below is just telling php that we will include a file called conn.php so whatever codes /
function inside the conn.php it will be added to our insert file

include 'conn.php';
?>

Note: Why would we need to do the include instead of just copying and pasting the connection file? 1.
Imagine if we make 100+ pages in our website then one day we need to change something in our
connection, if we just copy and paste the connection file we encounter an error we will be editing all the
100+ file. That is much time consuming that is why we are just including a file

Next Step we will be creating textboxes and button for our data, in our database we created a table that
have : ID, Last_Name, First_Name, Middle_Name and Addr

Now we will create 4 textboxes, why not 5? Remember the ID is set as AUTO INCREMENT we don’t need
to put a value to our ID SQL will automatically add 1 per data we submit

<html>
<head>
<title>Insert File</title>
</head>
<body>
<form action ="" method = "POST">
Last Name: <input type = "text" name = "txtLname" required><br>
First Name: <input type = "text" name = "txtFname" required><br>
Middle Name: <input type = "text" name = "txtMname" required><br>
Address <input type = "text" name = "txtAddr" required><br>
<input type = "submit" value = "Add Record" name = "btnSubmit">
</form>
</body>
</html>

The output of the code above should be like this:

Next we will now add the functionality for the textboxes above to be able to add some records to our
database

Below your closing </html> we will add the following code for the functionality
<?php
//same as on the other document I’ve sent the isset function will test if the button is clicked or not, so if
it is clicked it will do whatever code that is inside of the braces
if (isset ($_POST['btnSubmit']))
{
//on this part we will just transfer the data that’s been put to our text box to some variables
//for example the textbox named txtLname whatever data we put on that text box it will be jus
//transferred to the variable $Lname $Lname = $_POST['txtLname'];
$Fname = $_POST['txtFname'];
$Mname = $_POST['txtMname'];
$Addr = $_POST['txtAddr'];

//in this part we will just use the sql syntax INSERT and put it on a variable called $sql
//you will notice that on the code below we didn’t include the ID field, remember as long as it is
//an auto increment field we can just ignore it because SQL will just +1 everytime we add a new
//data
$sql = "INSERT INTO tbl_students (Last_Name,First_Name,Middle_Name,Addr)
VALUES('$Lname','$Fname','$Mname','$Addr')";

//the code below will just test if our data is successfully added or not remember the variable
$connection is from our conn.php file, remember whatever variable conn.php have we can also
use them on our insert.php file as long as we include them on the top of the page
if (mysqli_query($connection,$sql))
{
echo "Record Added";
}
else
{
echo "Error Adding Data";
}

}
?>

The final look of our insert.php should be like this:


MySQL View Data
Now gawa tayo ng isa pang php file na kasama ng conn.php

Yung gagawin nateng file ang pangalan nya is: view.php

Now sa view.php gusto nateng mangyari is yung laman ng database naten is lalabas don in a table form

<?php
include 'conn.php';
?>

<?php
$sql = "SELECT * FROM tbl_students";
//this variable will execute our mysql syntax
//etong code sa baba is i ra run nya yung mysql code naten na select * from tbl_student
$result = mysqli_query($connection, $sql);

//now gagawa tayo ng HTML Table inside our PHP code lahat
//kapag gagamit tayo ng HTML code inside our php code make sure na ipapasok naten sila sa echo ""
//in this part we will use conditional statement i tetest lang naten if may laman ba yung database table
naten or wala
//kung may laman yung database nyo lalabas yung table if wala pupunta sya sa else
//mysqli_num_rows bibilangin nya yung rows ng database table mo if yung rows naten is greater than 0
which is may laman ipapalabas nya yung table naten if wala proceed sya sa else statement naten
if (mysqli_num_rows($result) > 0)
{
echo "<table border = '1'>";
echo "<tr>";
echo "<td>ID</td>";
echo "<td>Last Name</td>";
echo "<td>First Name</td>";
echo "<td>Middle Name</td>";
echo "<td>Address</td>";
echo "</tr>";

echo "</table>";
}
else
{
//kapag walang laman yung database nyo eto yung lalabas
echo "<center><h1>No Data Found</center></h1>";
}
?>
kapag ni run naten yung code sa taas kapag walang laman yung database table nyo eto yung lalabas
dapat

as of now wala pang laman yung table naten ngayon gagamit tayo ng Looping para maipalabas yung
laman ng table naten from our database

<?php
include 'conn.php';
?>

<?php
$sql = "SELECT * FROM tbl_students";
//this variable will execute our mysql syntax
//etong code sa baba is i ra run nya yung mysql code naten na select * from tbl_student
$result = mysqli_query($connection, $sql);

//now gagawa tayo ng HTML Table inside our PHP code lahat
//kapag gagamit tayo ng HTML code inside our php code make sure na ipapasok naten sila sa echo ""
//in this part we will use conditional statement i tetest lang naten if may laman ba yung database table
naten or wala
//kung may laman yung database nyo lalabas yung table if wala pupunta sya sa else
if (mysqli_num_rows($result) > 0)
{
//Note etong part na to eto yung magiging header naten (Table Header)
echo "<table border = '1'>";
echo "<tr>";
echo "<td>ID</td>";
echo "<td>Last Name</td>";
echo "<td>First Name</td>";
echo "<td>Middle Name</td>";
echo "<td>Address</td>";
echo "</tr>";
//eto namang part nato which is loop
//hanggat may nababasang data yung code naten ay ilalabas nya sa body ng ating table
// hindi naten need i loop yung 1st part ng table naten kase ayaw naten magulit ulit yung header ng table
naten
//mysqli_fetch_assoc($result) etong part na to gagawin nya is babasahin nya yung data sa database table
nyo then ilalagay nya sa variable $row nyo
while ($row = mysqli_fetch_assoc($result))
{
//then dito yung variable $row naten sya yung maghahawak ng database table data naten basta i
include nyo lang yung ['NAME NG DATA NYO']
//etong part lang na to is yung i loloop naten
//mapapansin nyo din dito naka concatenate yung variable row naten sa gitna ng html tags naten
echo "<tr>";
echo "<td>".$row['ID']."</td>";
echo "<td>".$row['Last_Name']."</td>";
echo "<td>".$row['First_Name']."</td>";
echo "<td>".$row['Middle_Name']."</td>";
echo "<td>".$row['Addr']."</td>";
echo "</tr>";

echo "</table>";
}
else
{
//kapag walang laman yung database nyo eto yung lalabas
echo "<center><h1>No Data Found</center></h1>";
}
?>

NOTE: pwede din kayong mag lagay ng HTML Link pa punta dun sa insert.php nyo
Pwede nyo din I customize yung table nyo using CSS

Full code ng view.php

<?php
include 'conn.php';
?>

<?php
$sql = "SELECT * FROM tbl_students";
$result = mysqli_query($connection, $sql);

if (mysqli_num_rows($result) > 0)
{
echo "<table border = '1'>";
echo "<tr>";
echo "<td>ID</td>";
echo "<td>Last Name</td>";
echo "<td>First Name</td>";
echo "<td>Middle Name</td>";
echo "<td>Address</td>";
echo "</tr>";
while ($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['ID']."</td>";
echo "<td>".$row['Last_Name']."</td>";
echo "<td>".$row['First_Name']."</td>";
echo "<td>".$row['Middle_Name']."</td>";
echo "<td>".$row['Addr']."</td>";
echo "</tr>";

echo "</table>";
}
else
{
echo "<center><h1>No Data Found</center></h1>";
}
?>
Actual Notepad++ Code

Output (View.php)
For your Activity create conn.php, insert.php and view.php and send me the sample screenshot

You might also like