Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 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,

2 Mechanism for HTLM PHP and SQL HTML FORM with textboxes, radio buttons, etc. PHP gets important values, and executes SQL commands SUBMIT BUTTON

3 Searching and viewing data  Check database connection  Get the value string from the previous form  Process data: (in this process, we use the select function)  Give feedback to user.

4 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

5 Remember the SELECT command in SQL>? SELECT SELECT * FROM table_name WHERE [Conditions] Eg. SELECT * INTO friends WHERE Lastname = ‘Gener’;

6 Remember how we catch data? message: message: <?php $input = $_POST[‘lastname’]; echo “You said: $input ”; ?> <?php $input = $_POST[‘lastname’]; echo “You said: $input ”; ?> Html_form.htmlResult_form.php

7 Remember the SQL/DB Connector? 1. <?php 2. $connection = mysql_connect(‘localhost’,’root’,’password’); 3. mysql_select_db('friends') ; 4. $query = 'Select * FROM names'; 5. $result = mysql_query($query); 6. echo " "; 7. if(mysql_num_rows($result) > 0) 8. { 9. while($row = mysql_fetch_row($result)) 10. { 11. echo " $row[1], $row[2] "; 12. } 13. } 14. echo " "; 15. mysql_free_result($result); 16. mysql_close($connection); 17. ?> 1. <?php 2. $connection = mysql_connect(‘localhost’,’root’,’password’); 3. mysql_select_db('friends') ; 4. $query = 'Select * FROM names'; 5. $result = mysql_query($query); 6. echo " "; 7. if(mysql_num_rows($result) > 0) 8. { 9. while($row = mysql_fetch_row($result)) 10. { 11. echo " $row[1], $row[2] "; 12. } 13. } 14. echo " "; 15. mysql_free_result($result); 16. mysql_close($connection); 17. ?>

8 What to do? We need to integrate the Catching of Data and SQL/DB to PHP Connection Mechanism together.

9 Html_form.html 1. 2. 3. 4. message: 5. 6. 7. 8.

10 Result_form.php 1. <?php 2. $connection = mysql_connect(‘localhost’,’root’,’’); 3. mysql_select_db('friends') ; 4. $query = 'Select * FROM names'; 5. $result = mysql_query($query); 6. echo " "; 7. if(mysql_num_rows($result) > 0) 8. { 9. while($row = mysql_fetch_row($result)) 10. { 11. echo " $row[1], $row[2] "; 12. } 13. } 14. echo " "; 15. mysql_free_result($result); 16. mysql_close($connection); 17. ?>

11 Result_form.php improvement 1. <?php 2. $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 " "; 8. if(mysql_num_rows($result) > 0) 9. { 10. while($row = mysql_fetch_row($result)) 11. { 12. echo " $row[1], $row[2] "; 13. } 14. } 15. else 16. { 17. echo “Record Not found!”; 18. } 19. echo " "; 20. mysql_free_result($result); 21. mysql_close($connection); 22. ?>

12 Step 2: Schema 1 st Page: The Site will Ask for the Lastname 1 st Page: The Site will Ask for the Lastname 2 nd Page: The Site will Show the Record using a loop 2 nd Page: The Site will Show the Record using a loop Does the record exist? Error Page: The Site give a feedback Error Page: The Site give a feedback False True

13  Check database connection  Get the data from the previous form  Process data: (in this process, we use the insert function)  Give feedback to user. Inserting Data

14 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

15 View, Add, Edit and Delete INSERT INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) Eg. INSERT INTO friends (firstname,lastname) VALUES (‘Pancho’,’Trinidad’);

16 Html_form.html 1. 2. 3. 4. lastname: 5. firstname: 6. midname: 7. age: 8. gender: Male Female 9. 10. 11. 12.

17 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') ;.

18 Result_form.php (cont). // get Data $firstname = $_POST[‘firstname’]; $lastname = $_POST[‘lastname’]; $midname = $_POST[‘midname’]; $age = $_POST[‘age’]; $gender = $_POST[‘sex’];.

19 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 feedback Echo “Database Saved”; mysql_free_result($result); mysql_close($connection); ?>

20 Step 2: Schema 1 st Page: The page will Ask for the data using form elements 1 st Page: The page will Ask for the data using form elements 2 nd Page: The page will Insert records using SQL commands 2 nd Page: The page will Insert records using SQL commands The Page give a feedback

21

22 Assignment # 2 Make a Telephone Catalogue  Firstname  Lastname  Address_House_Number  Address_Street  Address_Barangay  Address_City  Telephone_number

23 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 record exist? Show Record The Page give a feedback Telephone Directory Menu

24 Assignment # 2


Download ppt "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,"

Similar presentations


Ads by Google