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.

Contents:
1. How to install Pocket PC emulator on your Windows PC
2. Three columns layout CSS styles for regular PC browsers
3. One column layout CSS styles for mobile device

css styles for mobile device

Pay via PayPal. You should not be concern about my web site security, because when you click the link, you will be redirected to PayPal site and it can be trusted. You will have your financial transaction with PayPal, not with me. So, please do not afraid to buy from my site. Credit cards are accepted. After payment is done, on the PayPal thank you page

click the Return to Merchant link.
You will be redirected to the download page! Also, You will receive email with link to download the tutorial.In case of any problem please contact us:administration@configure-all.com

Three columns layout template created in such a way that the middle column content coming first in the HTML code. This way, Google spider, while visiting your page, will see your main content first. Then the right and the left columns are placed.

In mobile device, web page has only one column. The same web page is used for regular browsers and a mobile device. It is very important because if you create two pages with the same content, Google will punish you for duplicate content.

Download My tutorial in PDF

My tutorials are different from all other tutorials and books because I do not skip initial steps that a reader needed to start learning. Some times, you buy a book and start to read it. You understand everything from the point the author starts, but you do not know how to get to that starting point. I never miss the beginning!

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.

If you get error message like this:

mysql_connect()[function.mysql-connect] Client does not support authentication protocol requested by server; consider upgrading MySQL client in C:\Apache2\htdocs\examples\fieldsarray.php on line 10 Unable to connect

Visit MySQL web site to find out how to fix it

Did you find information useful? Send to your friend a link to this page

Please rate the tutorial

1 2 3 4 5 6 7 8 9 10



Comments
Register to add comments ( 5000 chars ) for arrayoffields.php.
Are you human? Please select two the same numbers