Web Design & Development

[ Web Design & Development Topics ]

MySQL and PHP - Part 2

Submitting Data from a Form

First we need a sample form to work with like this:

<form name="f" action="form_webonly.php">
<table border="0" cellspacing="4" cellpadding="4">
<tr>
<td>Author: </td>
<td><input name="author" type="text" id="author"></td>
</tr>
<tr>
<td>title: </td>
<td><input name="title" type="text" id="title"></td>
</tr>
<tr>
<td>Edition:</td>
<td><input name="edition" type="text" id="edition"></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit with just html kickout">
</form>

First lets just try to get the information to go to a Web page. Lets make a PHP form called form_webonly.php as specified in the above form. It should look like this:

<?php
echo "<p>The book titled<b><i>".$title."</i></b> in the ".$edition." edition which was written by <b>".$author."</b> seems to be one that is on your mind for some reason.</p><p>Lets look into this further.</p>";
?>

Submitting Data from a Form to a MySQL Database

First we need a sample form to work with like this:

<html xmlns="http://www.w3.org/1999/xhtml">
<form name="f" action="form_to_table.php" method="post">
<table border="0" cellspacing="4" cellpadding="4">
<tr>
<td>Author: </td>
<td><input name="author" type="text" id="author"></td>
</tr>
<tr>
<td>title: </td>
<td><input name="title" type="text" id="title"></td>
</tr>
<tr>
<td>Edition:</td>
<td><input name="edition" type="text" id="edition"></td>
</tr>
</table>
<P><input type="submit" name="submit" value="Submit to database">
</form>
</html>

Now we need the form_to_table.php to get it to go to the database

<?
extract($_post);
$url="localhost";
$uname="root";
$pword="";
$dbase="cs453";

$conn = mysql_connect($url,$uname,$pword) or
die("unable to connect to the database server at ".$url);

mysql_select_db($dbase,$conn) or
die("unable to connect to the database named ".$dbase." at ".$url);

$sql="insert into books(title,Author,Edition)
values ('$title','$author','$edition')";

$result = mysql_query ($sql);

$affrows=@mysql_affected_rows();
echo "$affrows row(s) was/were affected by your query<br />";

?>