PHP Multipurpose Forms with data binding and cross table

To administrate data has to be efficient and easy to handle. If you look for example to a publishers company with several authors who have written different books it makes no sense to present the data on different web pages. That's not lucid. A bit of advanced PHP and MySql programming and a cross table can help you out. The cross table holds the id-data from the author and book table. All the action will take place on one web page, which is sometimes quiet tricky to realize (see screenshots below). Show data, add data, edit data and delete data: We take the authors from a DropDownList (First Part), using arrays (one for the id and two for the authors first and last name) and ensure with 'in_array()' the control is still in the right position, when an author was activated. In the Second Part the author data will displayed, more interesting is the Third Part: In the query we ask for the book data using the cross table in which the author_id is allocated to the book_id. To add or edit author or books data we open new controls, only the author table allows you to change data without open a new control. If a new author has been added his data will be presented immediately in the main form. Authors can not de deleted if there is a corresponding book. A new book entry corresponds always to the author and adds the data in the books and cross table using 'SELECT LAST_INSERT_ID()'. All actions hold the chosen data, only after adding a new author or after calling the '->Reset all'-action the page will be reset. The design of the page can be improved - the better the presentation of data is organized, the better your are in control.

The scripts snippets you see here (the complete script is downloadable) is only a small part of the complete application. That's it. The easy extendable database action and other stuff is all prepared for your convenience (create-table-, css- and connection-file included). Best regards Claudio Biesele


First Part:

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='POST' name='searchid'> Choose Paperbooks author:
<p><select class='controls'
name='searchid' size='1' onchange='this.form.submit()'>
<option value='-1'>Choice</option> <?php
$searchid = $_POST['searchid'];
$result = mysql_query ("SELECT id, vorname, nachname FROM autor", $verbindung);
while (list ($id, $vorname, $nachname) = mysql_fetch_row ($result))
{
$namenid = array($id);
$vorname = array($vorname);
$vorn = substr($vorname[0], 0, 1);
$name = array($nachname);
foreach($namenid as $key)
if (in_array($searchid, $namenid))
{
echo ("<option selected='selected' value=$key>$vorn. $name[0]</option>;");
}
else
{
echo ("<option value=$key>$vorn. $name[0]</option>;");
}}
?>
</select>


Second Part:

<?php
if(isset($_POST['searchid']))
{
if($_POST['searchid'] == -1)
{
$meldung = "Please make your choice.";
}
else
{
$vorname = $_POST['vorname'];
$nachname = $_POST['nachname'];
$disabling = 'enabled';
$searchid = $_POST['searchid'];
$abfrage = 'SELECT * FROM autor WHERE id=' . $searchid;
$ergebnis = mysql_query($abfrage);
$datensatz = mysql_fetch_array($ergebnis, MYSQL_ASSOC);
}}
?>


 

Third Part:

<?php
if(isset($searchid))
{
$ergebnis= mysql_query (" SELECT a.buch_id, b.* from autorbuch a, buecher b
WHERE a.autor_id = '". $searchid. "'
AND a.buch_id = b.id
ORDER BY buch_id", $verbindung);
while (list ($autor_id,$buch_id,$buchname,$jahrgang,$preis) = mysql_fetch_row ($ergebnis))
{
echo(" <tr bgcolor='#bbbbbb'>
<td>$buch_id</td>
<td>$buchname</td>
<td>$jahrgang</td>
<td>$preis</td>
<td align='center'><form action='MultipurposeForm.php' method='POST' name='FormDeleteName'>
<input type='hidden' name='delete_id' value='$buch_id'>
<input type='hidden' name='searchid' value='$datensatz[id]'>
<input type='submit' class='button2' name='submitDeleteBuch' value='->ok'>
</form></td>
<td align='center'><form action='MultipurposeForm.php' method='POST' name='FormEditName'>
<input type='hidden' name='buchname' value='$buchname'>
<input type='hidden' name='jahrgang' value='$jahrgang'>
<input type='hidden' name='preis' value='$preis'>
<input type='hidden' name='editid' value='$buch_id'>
<input type='hidden' name='searchid' value='$datensatz[id]'>
<input type='submit' class='button2' name='submitEditBook' value='->ok'>
</form></td>
</tr>
<td> ");}}
?>

Picture pub_form020606_1.jpg
1. Example: Multipurpose form in standby modus.
Picture pub_form020606_2.jpg
2. Example: User has chosen an author, corresponding books will be shown too.
Picture pub_form020606_3.jpg
3. Example: User likes to enter a new book entry corresponding to author.
Picture pub_form020606_4.jpg
4. Example: User likes to modify a book entry corresponding to author.
Picture pub_form020606_5.jpg
5. Example: User likes to add a new author to the list.
Picture pub_form020606_6.jpg
6. Example: Authors with books to sell will not be deleted.
Download the full script at www.fastproject.ch. The downloaded script is free from Copyright restrictions, Zurich, 11th of November 2015.
Donate with PayPal:
placeholder