PHP+SQL: View and Adding,
description
Transcript of PHP+SQL: View and Adding,
OBJECTIVES
•Learn how to view data using SQL and PHP•Learn how to add new data using SQL and PHP
PHP+SQL: View and Adding,
Mechanism for HTLM PHP and SQL
HTML FORM with
textboxes, radio
buttons, etc.
PHP gets important
values, and
executes SQL
commands
SUBMIT BUTTON
Searching and viewing data
Check database connectionGet the value string from the previous formProcess data: (in this process, we use the select function)Give feedback to user.
Viewing Search Data
This HTML form has a
textbox where the user enters a
search string, and a button to send data
We catch the data from the
HTML’s textbox using either
$_POST / $_GET
SUBMIT BUTTON
Use that data as our value for
the search String.Use the SELECT
function to view data
Execute it using PHP
Remember the SELECT command in SQL>?
SELECTSELECT * FROM table_name WHERE [Conditions]
Eg.SELECT * INTO friends WHERE Lastname
= ‘Gener’;
Remember how we catch data?
<html><body><form name="input" action=“result_form.php" method="post">
message: <input type="text" name= “lastname" >
<input type ="submit" value ="Submit"> </form></body></html>
<?php
$input = $_POST[‘lastname’];echo “You said: <i>$input</i>”;
?>
Html_form.html Result_form.php
Remember the SQL/DB Connector?
1. <?php2. $connection = mysql_connect(‘localhost’,’root’,’password’);3. mysql_select_db('friends') ;4. $query = 'Select * FROM names';5. $result = mysql_query($query);6. echo "<ol>";7. if(mysql_num_rows($result) > 0)8. {9. while($row = mysql_fetch_row($result))10. {11. echo "<li> <b>$row[1]</b>, $row[2]
</li>";12. }13. }14. echo "</ol>";15. mysql_free_result($result);16. mysql_close($connection);17. ?>
What to do?
We need to integrate the Catching of Data and SQL/DB to PHP Connection Mechanism together.
Html_form.html
1.<html>2.<body>3.<form name="input" action=“result_form.php" method="post">
4.message: <input type="text" name= “lastname" >
5.<input type ="submit" value ="Submit"> 6.</form>7.</body>8.</html>
Result_form.php
1. <?php2. $connection = mysql_connect(‘localhost’,’root’,’’);3. mysql_select_db('friends') ;4. $query = 'Select * FROM names';5. $result = mysql_query($query);6. echo "<ol>";7. if(mysql_num_rows($result) > 0)8. {9. while($row = mysql_fetch_row($result))10. {11. echo "<li> <b>$row[1]</b>, $row[2]
</li>";12. }13. }14. echo "</ol>";15. mysql_free_result($result);16. mysql_close($connection);17. ?>
Result_form.php improvement
1. <?php2. $search_value = $_POST[‘lastname’];3. $connection = mysql_connect(‘localhost’,’root’,’’);4. mysql_select_db('friends') ;5. $query = “Select * FROM names where lastname LIKE
‘$search_value%’”;6. $result = mysql_query($query);7. echo "<ol>";8. if(mysql_num_rows($result) > 0)9. {10. while($row = mysql_fetch_row($result))11. {12. echo "<li> <b>$row[1]</b>, $row[2] </li>";13. }14. }15. else16. {17. echo “Record Not found!”;
18. }19. echo "</ol>";20. mysql_free_result($result);21. mysql_close($connection);22. ?>
Step 2: Schema
1st Page:The Site will Ask for the
Lastname
2nd Page:The Site will
Show the Record using a loop
Does the
record
exist?
Error Page:The Site give a
feedbackFalse
True
Check database connectionGet the data from the previous formProcess data: (in this process, we use the insert function)Give feedback to user.
Inserting Data
Viewing Search Data
This HTML form with all
necessary form
elements .
We catch the data from the
HTML’s textbox using either
$_POST / $_GET
SUBMIT BUTTON
Use the INSERT
function (SQL) to Insert Data
to the Database
Execute it using PHP
View, Add, Edit and Delete
INSERTINSERT INTO table_name (column1,
column2, column3,...)VALUES (value1, value2, value3,...)
Eg.INSERT INTO friends (firstname,lastname) VALUES (‘Pancho’,’Trinidad’);
Html_form.html
1. <html>2. <body>3. <form name="input" action=“result_form.php" method="post">4. lastname: <input type="text" name= “lastname" > <br>5. firstname: <input type="text" name= “firstname" > <br>6. midname: <input type="text" name= “midname" > <br>7. age: <input type="text" name= “age" > 8. gender: <input type="radio" name="sex" value="male" /> Male <br />
<input type="radio" name="sex" value="female" /> Female9. <input type ="submit" value ="Submit"> 10.</form>11.</body>12.</html>
Result_form.php
//PART 1: Check Database and connect it to php
<?php$search_value = $_POST[‘lastname’];$connection = mysql_connect(‘localhost’,’root’,’’);mysql_select_db('friends') ;...
Result_form.php (cont)
.
.
.// get Data$firstname = $_POST[‘firstname’];$lastname = $_POST[‘lastname’];$midname = $_POST[‘midname’];$age = $_POST[‘age’];$gender = $_POST[‘sex’];...
Result_form.php (cont)
// process data and run Sql$query = “INSERT into friends
(lastname,firstname,midname,age,gender) values (‘$firstname’,’$lastname’,’$midname’,’age’, ‘gender’)”;
$result = mysql_query($query);// give feedbackEcho “Database Saved”;mysql_free_result($result);mysql_close($connection);?>
Step 2: Schema
1st Page:The page will Ask for the data using form
elements
2nd Page:The page will
Insert records using SQL commands
The Page give a feedback
Assignment # 2
Make a Telephone Catalogue Firstname Lastname Address_House_Number Address_Street Address_Barangay Address_City Telephone_number
Step 2: Schema
Add Record
Insert Record
The Page give a feedback
Look List
Show all list
The Page give a
feedback
Search Record
Get Search Key
Search Record
If recor
d exist?
Show Record
The Page give a feedback
Telephone Directory Menu
Assignment # 2