Using PHP to Store Data

Download Report

Transcript Using PHP to Store Data

Storing Data on the Server
Introduction
We are going to look at some working
code
It writes fixed data into a simple onetable database
We will look at the key parts of the code
in detail
Nic Shulver, [email protected]
Storing Data on the Server
Database Structure
 Test database has one table
 “tblWeek6”
 What would happen if we named a table “select” or “update”
or “values” or “table of users”?
 The table has just four columns
 id, an “auto-increment” integer field
 twLastname, text, a varchar(30) field
 twFirstname, text, also a varchar(30) field
 twPassword, text, as above
Nic Shulver, [email protected]
Example Code
Standard HTML header
<html>
<head>
<title>Writing Simple Data</title></head>
<body>
<h2>Writing Simple Data</h2>
Switches into
PHP mode
<?php
//=====================================//
// The data we are going to store
// Fixed data for this simple test.
$sLastname = "Sanchez";
$sFirstname = "Luis";
$sPassword = "lsanchez";
$nID = 65;
Provides some fixed
data to work with –
Normally this would
come from a Form
Example Code
The [action] or die(“message”)
block allows us to view simple
errors
// Connects to a MySQL server
$myID='xy123456';
$mysqli = new mysqli("web.fcet.staffs.ac.uk",$myID,$myID,$myID)
or die("Failed to connect to DB" . $mysqli->error);
//=====================================//
// Inserts new data into our users table
try
try/catch block allows
{ $bItWorked = $mysqli->query(
us to catch errors
"INSERT INTO tblWeek6
(id, twLastname, twFirstname, twPassword)
VALUES
($nID, '$sLastname', '$sFirstname', '$sPassword')" );
Example Code
if($bItWorked)
{ echo "Successfully added the data ($nID, '$sLastname',
'$sFirstname', '$sPassword') to the database.<br />";
}
else
{ echo "Failed to add the data ($nID, '$sLastname',
'$sFirstname', '$sPassword') to the database!<br />";
}
}
catch(Exception $e)
{ echo 'Sorry - There was a problem with adding the data to
the database.<br />';
}
Example Code
// closes the connection, frees up resources
$mysqli->close();
$mysqli = null;
Frees up resources – just
?>
</body>
</html>
good manners here, can be
important in bigger scripts
[why?]
Storing Data on the Server
Key parts
$mysqli = new mysqli("web.fcet.staffs.ac.uk",
$myID, $myID, $myID);
This creates a new connection to our mySQL
database
www.fcet.staffs.ac.uk is the database server
$myID is the username, password and db
name
Nic Shulver, [email protected]
Storing Data on the Server
Key parts
$bItWorked = $mysqli->query(
"INSERT INTO tblWeek6
(id, twLastname, twFirstname, twPassword)
VALUES
($nID, '$sLastname', '$sFirstname', '$sPassword')" );
 Tries to run the SQL in the database (returns FALSE on
failure, TRUE on success)
 May cause an error, which we can trap
 Notice the single quotes around string parameters
 Notice the double quotes allowing “variable interpolation”
Nic Shulver, [email protected]
Storing Data on the Server
Re-use
The highlighted code is worth re-using
Makes sense to copy and paste this
“boilerplate” code and modify it for your needs
Usually only needs the username to be
changed
try/catch blocks are useful in lots of areas, not
just database handling
Nic Shulver, [email protected]
Storing Data on the Server
Summary
We have seen some very simple PHP code
Parts of the application presented can be
used as “boilerplate code”
Database access in PHP is quite simple to do
There are pitfalls for the unwary
Nic Shulver, [email protected]