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 in MySQL a table from 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 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 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>";
	    }

Dowload fieldarray.zip archive with 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 recomended to use root user), type your password, and your database name.

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

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

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

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

My eBooks on Amazon.com

Subscribe to our mail list to receive free code examples.

Comments

No comments yet.

Add Comment

* Required information
(never displayed)
 
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
 
1000
Enter the third letter of the word castle.
 
Enter answer:
Captcha
Refresh
 
Enter code:
 
Notify me of new comments via email.
 
Remember my form inputs on this computer.
 
I have read and understand the privacy policy. *
 
I have read and agree to the terms and conditions. *
 
 
Powered by Commentics