Presentation is loading. Please wait.

Presentation is loading. Please wait.

MySQL tutorial.

Similar presentations


Presentation on theme: "MySQL tutorial."— Presentation transcript:

1 MySQL tutorial

2 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

3 Basic MySQL Syntax SHOW DATABASES; USE database_name; SHOW TABLES;
DROP TABLE table_name;

4 Create MySQL Table CREATE TABLE user (name varchar(9) NOT NULL,
id int(6) NOT NULL, PRIMARY KEY (id), UNIQUE (id) );

5 Add/Delete/Update Table
INSERT INTO user VALUES (‘bond’, ‘007’); DELETE FROM user WHERE id=‘007’; UPDATE user SET name=‘BOND’ WHERE id=‘007’;

6 Query Database SELECT * FROM user;
SELECT * FROM user WHERE name=‘BOND’; SELECT DISTINCT name FROM user; SELECT name, id FROM user ORDER BY name;

7 PHP Configuration File
Use a securely positioned ‘config’ file to store variables. Other PHP pages can link to it and use the variables as their own. <? // configuration parameters // database configuration $host = "macneill.cs.tcd.ie"; $user = “username"; $pass = “password"; $db = “username_db"; // default contact person $def_contact = “Karl"; ?>

8 PHP Add to DB Code 1 <table cellspacing="5" cellpadding="5">
<form action="addUpdate.php" method="POST"> <tr> <td valign="top"><b><font size="-1">Title</font></b></td> <td><textarea name="title" cols="40" rows="2"></textarea></td> </tr> <td valign="top"><b><font size="-1">Authors</font></b></td> <td><textarea name="authors" cols="40" rows="2"></textarea></td> <inut type="Submit" name="submit" value="Add"></td></tr> </form> </table>

9 PHP Add to DB Code 2 include("conf.php");
<? include("conf.php"); // form submitted so start processing it $title = $_POST["title"]; $authors = $_POST["authors"]; // set up error list array & validate text input fields $errorList = array(); $count = 0; if (!$title) { $errorList[$count] = "Invalid entry: Title"; $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 ("Unable to connect!"); mysql_select_db($db) or die ("Unable to select database!"); $query = "INSERT INTO papers (title, authors, description, comment, super, bibtex, url, genre) VALUES ('$title', '$authors', '$description', '$comment', '$super','$bibtex','$url','$genre')"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); echo "<font size=-1>Addition successful.<br><br> <a href=papers.php>Go back to the main page</a> | <a href= // close database connection mysql_close($connection); } else {// errors occurred} ?>

10 PHP Query Code include("conf.php"); $connection = mysql_connect($host, $user, $pass) or die (); mysql_select_db($db) or die ("Unable to select database!"); $query = "SELECT * FROM papers"; $result = mysql_query($query) or die ("Error in query”); ?> <table cellpadding="0" cellspacing="0" border="0" width="622"> <tr><td bgcolor="990000"><img src="images/spacer.gif" alt="" height="2"></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="-2"><a href="edit.php?id=<? echo $row->id; ?>">edit/view</a> | <a href="delete.php?id=<? echo $row->id; ?>">delete</a></font><p> <font size="-1"><b><? echo $row->title; ?></b><br> <font size="-1"><b>-<? echo $row->authors; ?></b> <br><a href="<? echo $row->url; ?>" target="_blank"> pdf</a> <br><br><br> </font> <tr><td bgcolor="990000"><img src="images/spacer.gif" alt="“ height="2"></td></tr> } // if no records present else{} mysql_close($connection);

11 PHP Delete Code include("conf.php");
// 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 ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // generate and execute query $query = "DELETE FROM papers WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // close database connection mysql_close($connection); // print result echo "<font size=-1>Deletion successful. <br><br><a href=papers.php>Go back to the main page</a> | <a href= }

12 PHP Update Code 1 $id=$_GET['id']; if (!$submit) {
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); mysql_select_db($db) or die ("Unable to select database!"); $query = "SELECT title, authors, description, comment, super, bibtex, url, genre FROM papers WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()) if (mysql_num_rows($result) > 0) $row = mysql_fetch_object($result); // print form with values pre-filled ?> <table cellspacing="5" cellpadding="5"> <form action="Update.php" method="POST"> <input type="hidden" name="id" value="<? echo $id; ?>"> <tr> <td valign="top"><b><font size="-1">Title</font></b></td> <td><textarea name="title" cols="40" rows="2"><? echo $row->title; ?></textarea></td> </tr> <td valign="top"><b><font size="-1">Authors</font></b></td> <td><textarea name="authors" cols="40" rows="2"><? echo $row >authors; ?></textarea></td> <td colspan=2> <input type="Submit" name="submit" value="Update"></td></tr> </form> </table>

13 PHP Update Code 2 include("conf.php");
// form submitted so start processing it $title = $_POST["title"]; $authors = $_POST["authors"]; $id = $_POST["id"]; // set up error list array $errorList = array(); $count = 0; // validate text input fields if (!$title) { $errorList[$count] = "Invalid entry: Title"; $count++; } if (!$contact) { $contact = $def_contact; } // check for errors, if none found... if (sizeof($errorList) == 0) { $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); mysql_select_db($db) or die ("Unable to select database!"); $query = "UPDATE papers SET title = '$title', authors = '$authors', description = '$description', comment = '$comment', super = '$super', bibtex = '$bibtex', url = '$url', genre = '$genre' WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // print result echo "<font size=-1>Update successful.<br><br> <a href=papers.php>Go back to the main page</a> | <a href= // close database connection mysql_close($connection); } else{} ?>

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


Download ppt "MySQL tutorial."

Similar presentations


Ads by Google