Ubi comp27nov04
Upcoming SlideShare
Loading in...5
×
 

Ubi comp27nov04

on

  • 1,537 views

 

Statistics

Views

Total Views
1,537
Views on SlideShare
1,537
Embed Views
0

Actions

Likes
0
Downloads
51
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Ubi comp27nov04 Ubi comp27nov04 Presentation Transcript

  • PHP/MySQL tutorial
  • Introduction
    • PHP (Hypertext Preprocessor)‏
    • - Open source, server-side, scripting language.
    • - Supports databases such as MySQL and Oracle.
    • - http://www.w3schools.com/php/default.asp
    • MySQL (Structured Query Language)‏
    • - Open source, speedy, scalable, reliable database technology.
    • - http://dev.mysql.com/doc/mysql/en/Tutorial.html
  • Tutorial Overview
    • Database (MySQL)‏
    • - DB creation
    • - Add/delete tables
    • - Add/delete/update records
    • - View/query records
    • Web (PHP)‏
    • - User front-end
    • - Add & query code
    • - Delete & update code
    View slide
  • MySQL & TCD
    • ‘ PuTTY ’ into wilde.cs.tcd.ie on port 22 w/ SSH
      • - Authenticate with your TCD username and password
    • (2) ssh macneil.cs.tcd.ie (password as above)‏
    • (3) Login into your MySQL account
    • ‘ mysql –uUSERNAME –p’
    • Enter your MySQL username and password
    • (4) Use MySQL syntax to create and view table(s), records, etc.
    View slide
  • Basic MySQL Syntax
    • SHOW DATABASES;
    • USE database_name;
    • SHOW TABLES;
    • DROP TABLE table_name;
  • Create MySQL Table
    • CREATE TABLE user (name varchar(9) NOT NULL,
    • id int(6) NOT NULL,
    • PRIMARY KEY (id),
    • UNIQUE (id)‏
    • );
  • Add/Delete/Update Table
    • INSERT INTO user VALUES (‘bond’, ‘007’);
    • DELETE FROM user WHERE id=‘007’;
    • UPDATE user SET name=‘BOND’ WHERE id=‘007’;
  • Query Database
    • SELECT * FROM user;
    • SELECT * FROM user WHERE name=‘BOND’;
    • SELECT DISTINCT name FROM user;
    • SELECT name, id FROM user ORDER BY name;
  • PHP User Front-End
    • <html>
    • <body>
    • <?php
    • $variable=“271004&quot;;
    • echo $variable;
    • ?>
    • </body>
    • </html>
    • Script is executed server side and presented to user via a browser.
    • PHP code is rendered as plain HTML.
  • PHP Configuration File
    • <?
    • // configuration parameters
    • // database configuration
    • $host = &quot;macneill.cs.tcd.ie&quot;;
    • $user = “username&quot;;
    • $pass = “password&quot;;
    • $db = “username_db&quot;;
    • // default contact person
    • $def_contact = “Karl&quot;;
    • ?>
    • Use a securely positioned ‘config’ file to store variables.
    • Other PHP pages can link to it and use the variables as their own.
  • PHP Add to DB Code 1
    • <table cellspacing=&quot;5&quot; cellpadding=&quot;5&quot;>
    • <form action=&quot;addUpdate.php&quot; method=&quot;POST&quot;>
    • <tr>
    • <td valign=&quot;top&quot;><b><font size=&quot;-1&quot;>Title</font></b></td>
    • <td><textarea name=&quot;title&quot; cols=&quot;40&quot; rows=&quot;2&quot;></textarea></td>
    • </tr>
    • <tr>
    • <td valign=&quot;top&quot;><b><font size=&quot;-1&quot;>Authors</font></b></td>
    • <td><textarea name=&quot;authors&quot; cols=&quot;40&quot; rows=&quot;2&quot;></textarea></td>
    • </tr>
    • <inut type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Add&quot;></td></tr>
    • </form>
    • </table>
  • PHP Add to DB Code 2
    • <?
    • include(&quot;conf.php&quot;);
    • // form submitted so start processing it
    • $title = $_POST[&quot;title&quot;];
    • $authors = $_POST[&quot;authors&quot;];
    • // set up error list array & validate text input fields
    • $errorList = array();
    • $count = 0;
    • if (!$title) { $errorList[$count] = &quot;Invalid entry: Title&quot;; $count++; }
    • // set default value for contact person
    • if (!$contact) { $contact = $def_contact; }
    • // check for errors & if none found...
    • if (sizeof($errorList) == 0)‏
    • {
    • $connection = mysql_connect($host, $user, $pass) or die (&quot;Unable to connect!&quot;);
    • mysql_select_db($db) or die (&quot;Unable to select database!&quot;);
    • $query = &quot;INSERT INTO papers (title, authors, description, comment, super, bibtex, url, genre)
    • VALUES ('$title', '$authors', '$description', '$comment', '$super','$bibtex','$url','$genre')&quot;;
    • $result = mysql_query($query) or die (&quot;Error in query: $query. &quot; . mysql_error());
    • echo &quot;<font size=-1>Addition successful.<br><br> <a href=papers.php>Go back to the main page</a> | <a href=http://www.cs.tcd.ie/Karl.Quinn/>home</font>&quot;;
    • // close database connection
    • mysql_close($connection);
    • }
    • else {// errors occurred}
    • ?>
  • PHP Query Code
    • include(&quot;conf.php&quot;);
    • $connection = mysql_connect($host, $user, $pass) or die ();
    • mysql_select_db($db) or die (&quot;Unable to select database!&quot;);
    • $query = &quot;SELECT * FROM papers&quot;;
    • $result = mysql_query($query) or die (&quot;Error in query”);
    • ?>
    • <table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; border=&quot;0&quot; width=&quot;622&quot;>
    • <tr><td bgcolor=&quot;990000&quot;><img src=&quot;images/spacer.gif&quot; alt=&quot;&quot; height=&quot;2&quot;></td></tr>
    • <?
    • // if records present
    • if (mysql_num_rows($result) > 0)‏
    • {
    • // iterate through resultset & print title with links to edit and delete scripts
    • while($row = mysql_fetch_object($result))‏
    • {
    • ?>
    • <font size=&quot;-2&quot;><a href=&quot;edit.php?id=<? echo $row->id; ?>&quot;>edit/view</a> | <a href=&quot;delete.php?id=<? echo $row->id; ?>&quot;>delete</a></font><p>
    • <font size=&quot;-1&quot;><b><? echo $row->title; ?></b><br>
    • <font size=&quot;-1&quot;><b>-<? echo $row->authors; ?></b>
    • <br><a href=&quot;<? echo $row->url; ?>&quot; target=&quot;_blank&quot;> pdf</a>
    • <br><br><br> </font>
    • <table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; border=&quot;0&quot; width=&quot;622&quot;>
    • <tr><td bgcolor=&quot;990000&quot;><img src=&quot;images/spacer.gif&quot; alt=&quot;“ height=&quot;2&quot;></td></tr>
    • <?
    • }
    • }
    • // if no records present
    • else{}
    • mysql_close($connection);
    • ?>
  • PHP Delete Code
    • include(&quot;conf.php&quot;);
    • // form not yet submitted, display initial form with values pre-filled
    • $id=$_GET['id'];
    • {
    • // open database connection
    • $connection = mysql_connect($host, $user, $pass) or die (&quot;Unable to connect!&quot;);
    • // select database
    • mysql_select_db($db) or die (&quot;Unable to select database!&quot;);
    • // generate and execute query
    • $query = &quot;DELETE FROM papers WHERE id = '$id'&quot;;
    • $result = mysql_query($query) or die (&quot;Error in query: $query. &quot; . mysql_error());
    • // close database connection
    • mysql_close($connection);
    • // print result
    • echo &quot;<font size=-1>Deletion successful. <br><br><a href=papers.php>Go back to the main page</a> | <a href=http://www.cs.tcd.ie/Karl.Quinn/>home</font>&quot;;
    • }
  • PHP Update Code 1
    • $id=$_GET['id'];
    • if (!$submit)‏
    • {
    • $connection = mysql_connect($host, $user, $pass) or die (&quot;Unable to connect!&quot;);
    • mysql_select_db($db) or die (&quot;Unable to select database!&quot;);
    • $query = &quot;SELECT title, authors, description, comment, super, bibtex, url, genre FROM papers WHERE id = '$id'&quot;;
    • $result = mysql_query($query) or die (&quot;Error in query: $query. &quot; . mysql_error())
    • if (mysql_num_rows($result) > 0)‏
    • {
    • $row = mysql_fetch_object($result);
    • // print form with values pre-filled
    • ?>
    • <table cellspacing=&quot;5&quot; cellpadding=&quot;5&quot;>
    • <form action=&quot;Update.php&quot; method=&quot;POST&quot;>
    • <input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;<? echo $id; ?>&quot;>
    • <tr>
    • <td valign=&quot;top&quot;><b><font size=&quot;-1&quot;>Title</font></b></td>
    • <td><textarea name=&quot;title&quot; cols=&quot;40&quot; rows=&quot;2&quot;><? echo $row->title; ?></textarea></td>
    • </tr>
    • <tr>
    • <td valign=&quot;top&quot;><b><font size=&quot;-1&quot;>Authors</font></b></td>
    • <td><textarea name=&quot;authors&quot; cols=&quot;40&quot; rows=&quot;2&quot;><? echo $row >authors; ?></textarea></td>
    • </tr>
    • <tr>
    • <td colspan=2>
    • <input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Update&quot;></td></tr>
    • </form>
    • </table>
  • PHP Update Code 2
    • include(&quot;conf.php&quot;);
    • // form submitted so start processing it
    • $title = $_POST[&quot;title&quot;];
    • $authors = $_POST[&quot;authors&quot;];
    • $id = $_POST[&quot;id&quot;];
    • // set up error list array
    • $errorList = array();
    • $count = 0;
    • // validate text input fields
    • if (!$title) { $errorList[$count] = &quot;Invalid entry: Title&quot;; $count++; }
    • if (!$contact) { $contact = $def_contact; }
    • // check for errors, if none found...
    • if (sizeof($errorList) == 0)‏
    • {
    • $connection = mysql_connect($host, $user, $pass) or die (&quot;Unable to connect!&quot;);
    • mysql_select_db($db) or die (&quot;Unable to select database!&quot;);
    • $query = &quot;UPDATE papers SET title = '$title', authors = '$authors', description = '$description', comment = '$comment', super = '$super', bibtex = '$bibtex', url = '$url', genre = '$genre' WHERE id = '$id'&quot;;
    • $result = mysql_query($query) or die (&quot;Error in query: $query. &quot; . mysql_error());
    • // print result
    • echo &quot;<font size=-1>Update successful.<br><br> <a href=papers.php>Go back to the main page</a> | <a href=http://www.cs.tcd.ie/Karl.Quinn/>home</a></font>&quot;;
    • // close database connection
    • mysql_close($connection);
    • }
    • else{}
    • ?>
  • Summary
    • Create MySQL database(s) with tables as required.
    • Create PHP powered webpage for adding, deleting, updating and viewing database information.
    • Be aware or security concerns vis-à-vis configuration file contents and database info.