Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.

Similar presentations


Presentation on theme: "Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP."— Presentation transcript:

1 Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP

2 Mark Dixon Page 2 Questions: HTML in PhP Are these correct (assume variables and fields exist)? $f = $f. $r["Description"] $h = $h. $r[" Name"] $a = " " + $a " " $html = $html. $h = " ". $h. " "   

3 Mark Dixon Page 3 Questions: Databases How many primary keys? How many foreign keys? 3 2

4 Mark Dixon Page 4 Session Aims & Objectives Aims –To introduce the fundamental ideas involved in using server-side code to write data to databases Objectives, by end of this week’s sessions, you should be able to: –create an ASP web page that allows the user to store data in database

5 Mark Dixon Page 5 Example: Person v1 (Specification) User requirement: –Display people's details from database online –need 2 pages: smith jones dixon list of people jones sally person's details

6 Mark Dixon Page 6 Example: PeopleList.php v1 <?php $c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c); $s = ''; while ($r = mysql_fetch_array($q)){ $s = $s. $r['Surname']. ' '; } mysql_free_result($q); ?> <?php Echo $s; ?>

7 Mark Dixon Page 7 Example: PeopleList.php v2 <?php $c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $q = mysql_query('SELECT * FROM Person;'); mysql_close($c); $s = ''; while ($r = mysql_fetch_array($q)){ $s = $s. ' '; $s = $s. $r['Surname']. ' '; } mysql_free_result($q); ?> <?php Echo $s; ?> now links

8 Mark Dixon Page 8 Example: Person.php v2 <?php $c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $sql = "SELECT * FROM Person WHERE id=". $_GET["id"] ; $q = mysql_query($sql); mysql_close($c); $s = ''; if ($r = mysql_fetch_array($q)){ $s = $r['Surname']; } mysql_free_result($q); ?> Back to People List Surname: " /> reads querystring (from previous page) displays data for selected record only

9 Mark Dixon Page 9 Example: Person v2 (Specification) User requirement: Display person’s details from database online –Change surname and save to database

10 Mark Dixon Page 10 Changing Data use SQL –INSERT: inserts a new record and makes it current –UPDATE: sends changes back to DB UPDATE Person SET Surname = 'Jones' WHERE ID = 1 –DELETE: deletes currently selected record

11 Mark Dixon Page 11 Example: Person.php v3 <?php $c = mysql_connect('localhost', 'root', ''); mysql_select_db('People'); $id = $_GET["id"]; if(isset($_POST["btnSave"])){ $sn = $_POST["txtSurname"]; $sql = "UPDATE Person SET Surname = '". $sn. "'"; $sql = $sql. " WHERE ID =". $id; mysql_query($sql); } $sql = "SELECT * FROM Person WHERE id=". $id; $q = mysql_query($sql); mysql_close($c); $s = ''; if ($r = mysql_fetch_array($q)){ $s = $r['Surname']; } mysql_free_result($q); ?> Back to People List Surname: " /> Save button works now

12 Mark Dixon Page 12 Tutorial Exercise: Person Task 1: Get the Person (v1) example from the lecture working. Task 2: Modify your code, so that forename is displayed as well as surname (use a table). Task 3: Get the Person (v2 and v3) example from the lecture working. Task 3: Modify your code, so that a line of text is displayed confirming that data has been saved. Task 4: Modify your code, so that an add button is included, which allows a new record to be added. Task 5: Modify your code, so that a delete button is included, which allows the current record to be deleted.


Download ppt "Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP."

Similar presentations


Ads by Google