PHP MySQL
Create & Test MySQL Database
Parsing Multiple Selections
Very often a form will allow a user to select more than one choice from a list of choices.
Checkboxes and selection boxes would provide this option.
We recommend that before you try this tutorial, you download the birthday package and build the database.
You may also want to experiment with adding some extra fields to the table.
In the form below the user is given the option of selecting desired fields of the database table.
The user may only want to look at names and birthdates without the id field.
This option is generated using a series of checkboxes.
A text input box is also provided to add a condition to the search.
Search for:
u
When the information is selected and the Submit button is depressed a table like the one below is generated:
Name | Jim Jr | Jim Sr |
Birthday | Mar26 | Jun22 |
The Name and Birthday checkboxes would be selected and the name Jim would be entered in the text box. If more than one Jim existed in the database, both would be returned.
Notice also that the Id field doesn't generate because it wasn't selected.
The script that produces the result is shown below. Comments are provided to show the code that is used for some of the operations.
<?php
/*Concat Checkbox Values into One string separated by commas for Select*/
foreach ($searchitem as $value){
$search=$search.",".$value;
}
/* Remove Leading comma*/
$searchit=substr($search,1);
/*Show the selected fields*/
print "$searchit";
$db="newdb";
$link = mysql_connect("localhost");
if (! $link)
die("Couldn't connect to MySQL");
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());
$result=mysql_query( "SELECT $searchit FROM birthdays where name LIKE '%$where%'" );
$num=mysql_num_rows($result);
mysql_close();
print "<b><center>Database Output - $searchitem</center></b><br><br>";
print "<table cellpadding=2 cellspacing=0 border=1>";
/*Check for void or unselected field id*/
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
++$i;
}
/*Print if not void*/
if($id <> ""){
print "<tr><td bgcolor=#000080><p>ID</p></td>";
$i=0;
while ($i < $num) {
/*Convert field to string*/
$id=mysql_result($result,$i,"id");
print "<td align=right>$id</td>";
++$i;
}
print "</tr>";
}
/*Check for void field name*/
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
++$i;
}
/*Print if not void*/
if($name <> ""){
print "<tr><td bgcolor=#000080><p>Name</p></td>";
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"name");
print "<td align=center>$name</td>";
++$i;
}
print "</tr>";
}
/*Check for void field birthday*/
$i=0;
while ($i < $num) {
$birthday=mysql_result($result,$i,"birthday");
++$i;
}
/*Print if not void*/
if($birthday <> ""){
print "<tr><td bgcolor=#000080><p>Birthday</p></td>";
$i=0;
while ($i < $num) {
$birthday=mysql_result($result,$i,"birthday");
print "<td align=left>$birthday</td>";
++$i;
}
print "</tr>";
}
print "</table>";
?>
Note: This script would be enclosed in an html page format and saved with php extension.
The Form
The code for the form would be created as shown below:
<h4>Search for:</h4>
<form method="POST" action="the-script.php">
<input type="checkbox" name="searchitem[]" value="id">ID<br>
<input type="checkbox" name="searchitem[]" value="name">Name<br>
<input type="checkbox" name="searchitem[]" value="birthday">Birthday<br>
<br>
<h4>Where:</h4>
Name Is: <input type="text" name="where" size="20">
<input type="submit" value="Submit"><input type="reset">
</form>
Note: The characters that appear as a box following searchitem are square brackets [ and ]. This signifies that the checkbox items are array items.
MySQL Tutorial
To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com