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.
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
If you like this page click +1 button.
Please rate the tutorial
How to Build Your Own Web Site from Scratch [Kindle Edition] $2.99
Earn Money on Internet as an Affiliate [Kindle Edition] $0.99
| Comments | |
|---|---|
- Home
- arrayoffields
- Batch Files
- Java Properties
- Form Validation
- Display Image PHP
- Upload File PHP
- phpMyAdmin
- Environment Variables
- Delete Trojan horse
- PHP Code Examples
- Learn PHP
- Modeless Popup
- Read Screen Resolution
- PHP Class Example
- Page View Counter
- Display Any Table
- Read csv file
- PHP Array Functions
- Array of Fields
- GET mess
- Array of Check boxes
- PHP and MS Access
- User_Auth. Demo
- PHP App Example
- PHP + Oracle 1
- PHP + Oracle 2
- Image Collection
- Create Gallery
- Block Spam Post
- $_REQUEST,$_SESSION
- Headers Allredy Sent
- Virtual Server
- Apache:OS 10048
- Set Shopping Cart
- My Best CSS Layout
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn SQL
- Learn C++
- Learn Java
- Learn Visual Basic
- Web Site Tips
- Site map
- Registration
Web programming Tips