«^»
7.4. Providing a WWW page to insert a new row

We can proceed in a similar way if we want to insert a new row in this table. Besides a box asking for the name of the item, the HTML form also has a box asking for details of its price. And, as we wish to change the database, we will need to supply the MySQL username and password that is appropriate for this database. So the form also has boxes asking for a username and a password:

0611: <HTML><BODY>
0612: <FORM METHOD="POST" ACTION="pricesinsert.php">
0613:    To insert a new item of consumables,
0614:    type in the following information:
0615:    <P>
0616:    name of MySQL server: <INPUT TYPE="Text"     NAME="server">
0617:    <P>
0618:    your username:        <INPUT TYPE="Test"     NAME="username">
0619:    <P>
0620:    your password:        <INPUT TYPE="Password" NAME="password">
0621:    <P>
0622:    name of the item:     <INPUT TYPE="Text"     NAME="goods">
0623:    <P>
0624:    price, e.g. 18.45:    <INPUT TYPE="Text"     NAME="price">
0625:    <P>
0626:    <INPUT TYPE="Submit" VALUE="Update consumables">
0627: </FORM>
0628: </BODY></HTML>
Go to the WWW form: http://www.dur.ac.uk/barry.cornelius/papers/phpintro/code/pricesinsert.htm

When the person using this WWW form clicks on the Update consumables button, the PHP script in the file pricesinsert.php gets executed. This time, because it wants to update the database, the script's call of mysql_connect needs to provide a username and a password. And, this time, because the query is actually an INSERT, the call of mysql_db_query returns either true or false depending on whether the INSERT was successful or not. The script outputs an appropiate message:

0629: <HTML><BODY>
0630: <?php
0631:    $server   = $_POST["server"];
0632:    $username = $_POST["username"];
0633:    $password = $_POST["password"];
0634:    $goods    = $_POST["goods"];
0635:    $price    = $_POST["price"];
0636:    $c_result = mysql_connect("$server", "$username", "$password");
0637:    $s_result = mysql_select_db("Pdcl0bjc_prices", $c_result);
0638:    $SQLQuery = "INSERT INTO consum SET goods='$goods', price=$price";
0639:    $q_result = mysql_query($SQLQuery, $c_result);
0640:    if ( $q_result == 0) {
0641:       echo "<P>There seems to be a problem with updating the table</P>";
0642:    }
0643:    else {
0644:       echo "<P>The table has been updated.</P>";
0645:    }
0646: ?> 
0647: </BODY></HTML>