Computer Programming web Web programming Tips



PHP Form with array of fields

By Sergey Skudaev


In this tip, you will learn how to create an array of input type fields on PHP form when you do not know in advance how many records exist in the table. Records from a table or query will be displayed on the editable form. User may edit all fields data at once and save changes in the database.

Create a table in MySQL using Data Model and Database free tutorial.


create table credit_cards(

cardid INT NOT NULL AUTO_INCREMENT,

name varchar(20),

type varchar(20),

expired date,

card_num INT,

credit float,

phone varchar(12),

address varchar(100),

city varchar(20),

state varchar(2),

zip varchar(10),

PRIMARY KEY(cardid)

);

 

Read MySQL Manual to learn how to use command prompt to start MySQL server and client, and create database and table.

Connect to MySQL and insert few records in the table using SQL query.


"insert into credit_cards(cardid, name, type, expired, card_num, credit,
phone, address, city, state, zip) values (0, 'CHASE', 'Visa', '2008-01-01',
123456789, 10000, '123-345-6789', '12 West 19 Street', 'New York', 'NY', '10021')";

"insert into credit_cards(cardid, name, type, expired, card_num, credit,
phone, address, city, state, zip) values (0, 'CITI', 'Mastercard', '2010-01-01',
192837465, 5000, '123-345-1234', '18 West 69 Street', 'New York', 'NY', '10028')";

"insert into credit_cards(cardid, name, type, expired, card_num, credit,
phone, address, city, state, zip) values (0, 'American Express', 'Mastercard', '2010-01-01',
987654321, 8000, '123-345-4321', '8 West 9 Street', 'New York', 'NY', '10028')";


PHP form with array of input type fields is almost the same as an regular PHP form. the difference is that input type name must have square brackets. For example:

<input type="text" name="myfield[]"/ >

You have to count the number of records and pass it to the next page in hidden input type like that:

print('<input type="hidden" name="rec_count" value="'. $rec_count.'">);

In the template that inserts or updates records you have to execute a query in the loop like that:


for($i=0; $i<$rec_count; $i++)

$sql="update mytable set myfield='".$myfield[$i]."'";

    if(!mysql_query($sql))
	    {
	    echo mysql_errno() . ": ";
	    echo mysql_error() . "<br>";
	    }

Download the fieldarray.zip archive with the PHP files.

Edit MySQL access variables on both PHP files. Type your hostname if it is not localhost, type your user name ( it is not recommended to use root user), type your password, and your database name.

$hostname="localhost";
$dbuser="root";
$dbpassword="aspirin";
$dbname="credit";

Copy these files in the arrayfields folder in the htdocs directory on your PC and to start example type in the browser URL:

"http://localhost/arrayfields/fieldsarray.php"

Form with data from credit_cards table displays. Edit fields value and press the Update button. All records will be updated and displayed on the form. Play with the code.

My eBooks on Amazon.com

US    UK    BR    CA
US    UK    BR    CA
US   UK   BR   CA