php+sql: view and adding,

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

Upload: gaetan

Post on 21-Mar-2016

77 views

Category:

Documents


1 download

DESCRIPTION

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. Mechanism for HTLM PHP and SQL. HTML FORM with textboxes, radio buttons, etc. PHP gets important values, and executes SQL commands. SUBMIT BUTTON. - PowerPoint PPT Presentation

TRANSCRIPT

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

Page 2: 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

Page 3: PHP+SQL:  View and Adding,

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.

Page 4: PHP+SQL:  View and Adding,

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

Page 5: PHP+SQL:  View and Adding,

Remember the SELECT command in SQL>?

SELECTSELECT * FROM table_name WHERE [Conditions]

Eg.SELECT * INTO friends WHERE Lastname

= ‘Gener’;

Page 6: PHP+SQL:  View and Adding,

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

Page 7: PHP+SQL:  View and Adding,

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

Page 8: PHP+SQL:  View and Adding,

What to do?

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

Page 9: PHP+SQL:  View and Adding,

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>

Page 10: PHP+SQL:  View and Adding,

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

Page 11: PHP+SQL:  View and Adding,

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

Page 12: PHP+SQL:  View and Adding,

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

Page 13: PHP+SQL:  View and Adding,

Check database connectionGet the data from the previous formProcess data: (in this process, we use the insert function)Give feedback to user.

Inserting Data

Page 14: PHP+SQL:  View and Adding,

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

Page 15: PHP+SQL:  View and Adding,

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’);

Page 16: PHP+SQL:  View and Adding,

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>

Page 17: PHP+SQL:  View and Adding,

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

Page 18: PHP+SQL:  View and Adding,

Result_form.php (cont)

.

.

.// get Data$firstname = $_POST[‘firstname’];$lastname = $_POST[‘lastname’];$midname = $_POST[‘midname’];$age = $_POST[‘age’];$gender = $_POST[‘sex’];...

Page 19: PHP+SQL:  View and Adding,

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);?>

Page 20: PHP+SQL:  View and Adding,

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

Page 21: PHP+SQL:  View and Adding,
Page 22: PHP+SQL:  View and Adding,

Assignment # 2

Make a Telephone Catalogue Firstname Lastname Address_House_Number Address_Street Address_Barangay Address_City Telephone_number

Page 23: PHP+SQL:  View and Adding,

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

Page 24: PHP+SQL:  View and Adding,

Assignment # 2