Get best price on Unlocked Galaxy S10 Lite | ||||||||||||
|
||||||||||||
|
MySQL PHP Database Tutorial: Insert DataNext, we need to get some data into the database. To do so, we use an HTML form to collect the data, and then insert the data into the database using PHP. Both these functions can be done on the same page. The form can submit to the same page it is on. This page checks if the POST method is used. If so, insert into the database. I find it easier to have the form and database insert operation on the same page. Makes one less file to keep track of. Here is the HTML for the form:
<form name="fa" action="insert_link.php" method="post">
<b>Category: </b> <input type="text" name="category" size=40> <b>Site Name:</b> <input type="text" name="sitename" size=40> <b>Site URL: </b> <input type="text" name="siteurl" value="http://" size=40> <b>Description: </b> <textarea name="description" rows=5 cols=40></textarea> <p><input type="submit" value="Add Link"></p> </form> The form looks like the one below. This tutorial does not use an actual database, so this form is not "hooked up" to
anything. It is to show you what we are working with. Also note that the above HTML snippet has the HTML formatting table
elements removed because the the snippet is for display purposes only. The actual, underlying HTML used to display the form
below uses an HTML table. This form gathers the data we want to insert into the database and submits back to the same page. Now we need the PHP code to process this information. The first block of code sets up
the connection to the database. This is the same as the code we used on the create table page.
<?php
$usr = "---username---"; $pwd = "---password---"; $db = "linksdb"; $host = "localhost"; $cid = mysql_connect($host,$usr,$pwd); if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); } ?> We need to create our SQL statement for the insert. The SQL format for an INSERT is:
INSERT INTO -tablename-
(column_name1, column_name2, ...) VALUES (data1, data2, ... ) Where the first column name matches to the first data in each of the sets. Remember, PHP variables are automatically initialized with the form field names submitted to the page. So the PHP code to submit the query and check for errors is:
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") { // the following 4 lines are needed if your server has register_globals set to Off $category = $_POST['category']; $sitename = $_POST['sitename']; $siteurl = $_POST['siteurl']; $description = $_POST['description']; $SQL = " INSERT INTO links "; $SQL = $SQL . " (category, sitename, siteurl, description) VALUES "; $SQL = $SQL . " ('$category', '$sitename','$siteurl','$description') "; $result = mysql_db_query($db,"$SQL",$cid); if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); } echo ("New Link Added\n"); } mysql_close($cid); ?>
Putting this together with the HTML code above gives us the complete script. Download this script here. (insert_link.phps) Load the page on your server and insert a couple of links into the database created previously. We will need some data in the database for the next section: View Data from Database Related Links
|
privacy policy || © 1997-2016. astonishinc.com All Rights Reserved. |