MySQL INSERT

This allows you to add data to a table. You can INSERT through PHPMyAdmin but you will want to create more site-specific and user-friendly pages as well.

Step one is the form for the user to type the data into:

            
<form method="POST" action="formprocess.php">
    <p>
        <label for="cdArtist">Artist/Group: </label><input type="text" id="cdArtist" name="cdArtist" size="30" />
        <label for="cdTitle">Title: </label><input type="text" id="cdTitle" name="cdTitle" size="30" />
        <label for="cdPrice">Price: £</label><input type="text" id="cdPrice" name="cdPrice" size="10" />
        <label for="cdLabel">Record Label: </label><input type="text" id="cdLabel" name="cdLabel" size="30" />
    </p>
    <p><input type="submit"></p>
</form>            
        

Paste that into a new HTML document called form.html (or a PHP one if you want but there is no PHP code in it).

Now you need to process that data so put this into a new PHP file called formprocess.php:

            
$cdArtist=$_POST[cdArtist];
$cdTitle=$_POST[cdTitle];
$cdPrice=$_POST[cdPrice];
$cdLabel=$_POST[cdLabel];

$mysqlserver="localhost";
$mysqlusername="test";
$mysqlpassword="test";
$link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());

$dbname = 'test';
mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());

$insertcdquery="INSERT INTO firsttable (cdArtist, cdTitle, cdPrice, cdLabel) VALUES ('$cdArtist', '$cdTitle', '$cdPrice', '$cdLabel')";
mysql_query($insertcdquery) or die("Query to insert new record to firsttable failed with this error: ".mysql_error());            
        

To explain the code:

The query itself should be fairly obvious although the structure is slightly strange. It tells MySQL to INSERT INTO a named table. It tells MySQL which fields to insert the data into and it sets the VALUES to go into those fields. Note that the values/variables are inside single quotes. This is because although they are variables MySQL does not see the variables. The PHP function mysql_query() replaces the variables with their contents before sending the statement to the server. ECHO $insertcdquery to see how the query looks to MySQL.

Go to PHPMyAdmin and firsttable and the Browse tab to see if the new record made it. Then load firstmysqlpage.php in a browser and see the new data on your own page (that will depend on the WHERE part of the query).Inserting data exercise

Single page form and processing

Those two pages could be merged into just one PHP page with the if(!$_POST) { display form } else { process } structure you learned on the page about single page form display and processing. Do that and save the page as mysqlinsert.php, upload, display in a browser and type in a new CD to test it. You could even add the SELECT section of mysqlfirstpage onto the end of this page so that after the data is INSERTed it is displayed.

submit to reddit Delicious Tweet