FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


A variable number of inputs for PHP to insert MYSQL





bukaida
First let me start out by saying I am a beginner so I apologize in
advance if what I ask is too fundamental.

I am accessing a MYSQL database through a web browser and I am
entering data into data tables using PHP.

The problem I am facing is that the number of input rows on the web
page is not fixed and will vary each time. Someone suggested I use
Javascript and that is why I am here now.


Here is an example of the rows that I would like to be able to fill :
Say for
record 1(Persons living in canada)
---------------------------------
id name address
---------------------------------
1 john canada
2 Jim canada
3 smith canada
-------------------------------

record 2(Persons living in LA)
----------------------------------
id name address
---------------------------------
1 joe LA
2 mathew LA
-------------------------------

I have to design an insert form with textbox which will allow me to insert variable number of records in the table(probably giving an option at the begining,to specify how many records). I am using php 4.4 and mysql 5.1. Please help.
rvec
There are 2 ways to solve this (probably more).
1. Just dispay a textarea and in php explode the input to get an array (explode(PHP_EOL, $_POST['textfield']) and explode(' ', $row)).
2. At first select how many rows you want and then display normal textfields for each row. This could be done with javascript, but could also be done by submitting the form and build a second form with php.

After you get the results in php you should try to put it all in an array. That way you can easily loop through the results when putting them in the database.
bukaida
Thanx RVEC.I liked the second approach. What I did is, created a second table and put the variable number of records there and used the id of the main table as foreign key.So when the data from the main table is inserted successfuly the insert.php, it contains a hyperlink to the form for the second data(may be a header could be used for auto redirect, but I am not very comfortable with headers). A hyperlink at the bottom of the second insert.php is put which is labelled as continue insert and another link finished. The finished link closes the current window and the continue insert link takes the user again to the insert form for the second table. This somehow solves the insertion problem.
But this approach cannot be applied in case of updation. So any idea of tackling that problem? The first approach is a bit too technical for me. So it will be extremely helpful if a sample code can be provided(entire code not needed, only the portion dealing with explode and other stuffs).
Thanx in advance.
jmraker
In javascript you can create a function that either uses something like. (There's probably some syntax error.)

Code:

function addRow(){
    rows = document.getElementById('rows");
    div = document.createElement("div");
    rows.insertBefore(div, false);
    div.innerHTML += "<div id='" + lastrow + "'><the HTML input fields></div>";
}
Related topics
PHP Book (Php and Mysql for Dynamic Web Sites)
HTTP AUTH with PHP and mySQL
Make search engine With PHP and mySQL, for your site
Crash course in PHP and MYSQL
php and mysql connection in flash based website
Installing Apache Php and MySQL [newbie tutorial]
Which book is best for beginner of PHP and MySQL?
[man]Azlo tu mismo ShoutBox en PHP y mysql
Developing a Login System with PHP and MySQL
PHP and MySQL FAQ - Read Before posting!
Building a Product Quoter with PHP and MySQL
"Drop Down List Box" Using PHP and MySql
which host do u like put php&mysql?
php and mysql form
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.