PHP can select records from a MySQL database, and then provide a way to choose selected records using an HTML check box. Check boxes can also be used to selectively delete or amend records. The relevant data can then be updated subject to the selections made on a Web page. Check boxes provide an easy way for users to interact with the data and select only the records she needs to update. PHP will then perform the relevant actions.
Connect to the selected database like so:
$hostname="localhost"; // Host name
$user=""; // Mysql username
$pass=""; // Mysql password
$database_name="sales"; // Database name
$table_name="saleshistory"; // Table name
// Connect to server and select the database as required:
mysql_connect("$hostname", "$user", "$pass") or die ("cannot connect to sales");
mysql_select_db("$database_name") or die ("cannot select the database");
Select records from the table as required:
$salessql="SELECT * FROM $table_name";
Obtain the records selected with a while statement:
The end of the first PHP section is marked with ?> above.
Use HTML and PHP to output the details of the check box
information as follows:
<td align="center" ><input name="checkbox" type="checkbox" salesid="checkbox" value="<? echo $rows['salesid']; ?>"></td>
<td ><? echo $rows['salesid']; ?></td>
<td ><? echo $rows['customerfirstname']; ?></td>
<td ><? echo $rows['customerlastname']; ?></td>
<td ><? echo $rows['customeremail']; ?></td>
The checkbox is indicated by the "salesid" field in each record. For each salesid displayed on the screen, the user can select or ignore the record. The Sales identifier number (salesid) will be checked or unchecked and indicates to PHP that this record should be updated. The other fields in the list provide additional information.
Tips & Warnings
- Although a while statement was provided as an example of populating a check box in this instance, it can also be implemented by line by line commands in an HTML form without a while loop.
- Photo Credit Jupiterimages/Creatas/Getty Images
How to Set HTTP Headers
Hypertext Transfer Protocol, or HTTP, is the language Web browsers and servers use to communicate over the Internet. Every time a browser...
How to Insert a Decimal in PHP MySQL
PHP provides many standard functions for handling MySQL databases. This means that inserting data into a MySQL database table using a PHP...
How to Insert Data From a Form in PHP to a MySQL Database
Inserting data into MySQL from a form on a website using PHP is something that developers often do. The task is not...
How to Insert a Time Stamp in PHP MySQL
PHP Web developers use PHP and MySQL to design websites and applications with dynamic data. One of the most important pieces of...
How to Record a Web Conference on a Mac
Web conferencing has become more and more common. Whether to chat with a friend or to conduct business, web conferencing is incredibly...