The short story: I am using PHP Essentials as a learning tool. I am running RH7.2 MySQL and Apache.
What I can do: I can create/access a database/table with PHP via a web page(which I think is slick)
Problem: I want to add records to a table. In the PHP Essentials book I think the author makes an assumption. That is, I have to create an HTML doc with the field names(statically) in order to populate the rows with information.
I expect to create quite a few tables for a project I am doing and would like PHP to create on the fly the field names I will be using when I access the table for use.
Otherwise I would need to edit the html doc each time I changed a field name in a table..
Confused?
Heres the PHP sample for creating a table:
<?
$sql = "CREATE TABLE $table_name (";
for ($i = 0; $i < count($field_name); $i++) {
$sql .= "$field_name[$i] $field_type[$i]";
if ($field_length[$i] != "") {
$sql .= " ($field_length[$i]),";
} else {
$sql .= ",";
}
}
$sql = substr($sql, 0, -1);
$sql .= ")";
$connection = mysql_connect("host","user","pass")
or die("Couldn't connect to server.");
$db = mysql_select_db("working", $connection)
or die("Couldn't select database.");
$sql_result = mysql_query($sql,$connection)
or die("Couldn't execute query.");
if (!$sql_result) {
echo "<P>Couldn't create table!";
} else {
echo "<P>$table_name has been created!";
}
?>
I create a table call working with values I created in a previous form( A table can have one field or 100 fields), all is good.
Then
The author has me create an HTML doc that has static assignments for the field names created(This is my problem, I don't want to create an HTML doc each time I create a table, unless of course I must)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Add a Record</TITLE>
</HEAD>
<BODY>
<h1>Adding a Record to MY_PRODUCTS</h1>
<FORM method="POST" action="do_addrecord.php3">
<table cellspacing=5 cellpadding=5>
<tr>
<td valign=top><strong>Item ID:</strong></td>
<td valign=top>
<INPUT type="text" name="item_id" size=5 maxlength=5>
</td>
</tr>
<tr>
<td valign=top><strong>Item Title:</strong></td>
<td valign=top>
<INPUT type="text" name="item_title" size=30 maxlength=50>
</td>
</tr>
<tr>
<td valign=top><strong>Item Description:</strong></td>
<td valign=top>
<TEXTAREA name="item_desc" cols=30 rows=5></TEXTAREA>
</td>
</tr>
<tr>
<td valign=top><strong>Item Price:</strong></td>
<td valign=top>
$ <INPUT type="text" name="item_price" size=10>
</td>
</tr>
<tr>
<td align=center colspan=2>
<INPUT type="submit" value="Add Record">
</td>
</tr>
</table>
</FORM>
</BODY>
</HTML>
Ok, there are 4 static input types and then finally lets add some records
<?
if ((!$item_id) || (!$item_title) || (!$item_desc) || (!$item_price)) {
header("Location: http://www.yourserver.com/show_addrecord.html");
exit;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>Add a Record</TITLE>
</HEAD>
<BODY>
<h1>Adding a Record to MY_PRODUCTS</h1>
<?
$sql = "INSERT INTO MY_PRODUCTS (ITEM_ID, ITEM_TITLE, ITEM_DESC, ITEM_PRICE)
VALUES ('$item_id', '$item_title', '$item_desc', '$item_price')";
$connection = mysql_connect("hostname","username","password")
or die ("Couldn't connect to server.");
$db = mysql_select_db("database_name", $connection)
or die("Couldn't select database.");
$sql_result = mysql_query($sql,$connection)
or die("Couldn't add record.");
if (!$sql_result) {
echo "<P>Couldn't add record!";
} else {
echo "
<P>Record added!</p>
<table cellspacing=5 cellpadding=5>
<tr>
<td valign=top><strong>Item ID:</strong></td>
<td valign=top>$item_id</td>
</tr>
<tr>
<td valign=top><strong>Item Title:</strong></td>
<td valign=top>$item_title</td>
</tr>
<tr>
<td valign=top><strong>Item Description:</strong></td>
<td valign=top>$item_desc</td>
</tr>
<tr>
<td valign=top><strong>Item Price:</strong></td>
<td valign=top>\$ $item_price</td>
</tr>
</table>
";
}
?>
</BODY>
</HTML>
Its all well and good and it works but I would like the Field Names to be placed automatically on the Record Adding form.