• Like
Ubi comp27nov04
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,285
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
51
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. PHP/MySQL tutorial
  • 2. 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
  • 3. 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
  • 4. 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.
  • 5. Basic MySQL Syntax
    • SHOW DATABASES;
    • USE database_name;
    • SHOW TABLES;
    • DROP TABLE table_name;
  • 6. Create MySQL Table
    • CREATE TABLE user (name varchar(9) NOT NULL,
    • id int(6) NOT NULL,
    • PRIMARY KEY (id),
    • UNIQUE (id)‏
    • );
  • 7. Add/Delete/Update Table
    • INSERT INTO user VALUES (‘bond’, ‘007’);
    • DELETE FROM user WHERE id=‘007’;
    • UPDATE user SET name=‘BOND’ WHERE id=‘007’;
  • 8. Query Database
    • SELECT * FROM user;
    • SELECT * FROM user WHERE name=‘BOND’;
    • SELECT DISTINCT name FROM user;
    • SELECT name, id FROM user ORDER BY name;
  • 9. 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.
  • 10. 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.
  • 11. 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>
  • 12. 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}
    • ?>
  • 13. 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);
    • ?>
  • 14. 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;;
    • }
  • 15. 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>
  • 16. 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{}
    • ?>
  • 17. 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.