Download presentation
Presentation is loading. Please wait.
Published byHilary Doyle Modified over 6 years ago
1
PHP: Inserting data FdSc Module 109 Server side scripting and
Database design 2011
2
Today’s achievements Demonstrate knowledge of PHP
Accepting Post data from an HTML form Creating a valid SQL Insert statement using the Post data Using PHP functions to update a MySQL database table Creating the input forms and PHP files for the help centre
3
Create an HTML form We will be inserting a new book into the books table of the library database We will need an HTML form to collect the data We will use a submit button to call a PHP file which will process the data and run the SQL Query to insert the data
4
Input process CLIENT SERVER PHP FILE MySQL Post sends data to PHP file
Input box PHP FILE Submit button PHP sends SQL query to MySQL MySQL
5
The form HTML forms begin with: Labels are defined by:
<form> and end with </form> Method = “post” tells the server to send the data Action = “file.php” tells the server which file to run when the form is submitted Labels are defined by: <label for “fieldname“>Your text</label> Input text fields are defined by <input type="text" id="ISBN" name="ISBN" />
6
HTML for the form (one box)
<body> <form method="post" action="addabook.php"> <label for "ISBN">ISBN:</label> <input type="text" id="ISBN" name="ISBN" /> </form> </body>
7
Single box Looks similar to: ISBN:
8
More input boxes We need boxes for all the fields in the books table (apart from bookid). Note the <br /> <label for "ISBN">ISBN:</label> <input type="text" id="ISBN" name="ISBN" /><br /> <label for "title">Title:</label> <input type="text" id="title" name="title" /><br /> <label for "author">Author:</label> <input type="text" id="author" name="author" /><br /> <label for "publisher">Publisher:</label> <input type="text" id="publisher" name="publisher" /><br /> <label for "publisherswebsite">Publisher's Web Site:</label> <input type="text" id="publisherswebsite" name="publisherswebsite" /><br /> <label for "genre">Genre:</label> <input type="text" id="genre" name="genre" /><br />
9
The submit button We add a button to submit the form to the php file we specified in the action <input type="submit" value="Add a book" name="submit" /> It goes at the end of the other inputs and before the end form tag The value appears on the button
10
Submit button added <label for "genre">Genre:</label> <input type="text" id="genre" name="genre" /><br /> <input type="submit" value="Add a book" name="submit" /> </form>
11
Form appearance You can use CSS to tidy this up (but I haven’t) What will happen when you press the button?
12
The PHP file (addabook.php)
Post data is in the array $_POST[‘fieldname'] We assign the values from the form to variables <?php $ISBN = $_POST['ISBN']; $title = $_POST['title']; $author = $_POST['author']; $publisher = $_POST['publisher']; $publisherswebsite = $_POST['publisherswebsite']; $genre = $_POST['genre']; ?>
13
Check that they were received using echo
echo 'Your ISBN number was ' . $ISBN . '<br />'; echo 'Your book title was ' . $title . '<br />'; echo 'Your author was ' . $author . '<br />'; echo 'Your publisher was ' . $publisher . '<br />'; echo 'Your web site was ' . $publisherswebsite . '<br />'; echo 'Your genre was ' . $genre . '<br />';
14
Add to the database $hostname = “localhost"; $username = “MISNumber"; $password = “XXXXXX"; $databaseName = “MISNumber_library"; include("connection.php"); if ($connectionSuccess == 1) { $query = "INSERT INTO books (ISBN,title, author, publisher, publisherswebsite, genre)" . "VALUES ('$ISBN', '$title', '$author', '$publisher', '$publisherswebsite', '$genre')"; $result = mysql_query($query);
15
Complete PHP <?php $ISBN = $_POST['ISBN']; $title = $_POST['title']; $author = $_POST['author']; $publisher = $_POST['publisher']; $publisherswebsite = $_POST['publisherswebsite']; $genre = $_POST['genre']; echo 'Your ISBN number was ' . $ISBN . '<br />'; echo 'Your book title was ' . $title . '<br />'; echo 'Your author was ' . $author . '<br />'; echo 'Your publisher was ' . $publisher . '<br />'; echo 'Your web site was ' . $publisherswebsite . '<br />'; echo 'Your genre was ' . $genre . '<br />'; $hostname = “localhost"; $username = “MISNumber"; $password = “XXXXXX"; $databaseName = “MISNumber_library"; include("connection.php"); if ($connectionSuccess == 1) { $query = "INSERT INTO books (ISBN,title, author, publisher, publisherswebsite, genre)" . "VALUES ('$ISBN', '$title', '$author', '$publisher', '$publisherswebsite', '$genre')"; $result = mysql_query($query); } ?>
16
Check results in PHPMyAdmin
17
Exercises Create input forms to add data to your help centre database Check they work Use CSS to improve your web pages
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.