PHP: Inserting data FdSc Module 109 Server side scripting and Database design 2011
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
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
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
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" />
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>
Single box Looks similar to: ISBN:
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 />
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
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>
Form appearance You can use CSS to tidy this up (but I haven’t) What will happen when you press the button?
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']; ?>
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 />';
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);
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); } ?>
Check results in PHPMyAdmin
Exercises Create input forms to add data to your help centre database Check they work Use CSS to improve your web pages