Re: Seperating data from form into two tables
| From: | Richard Lynch | Date: | Wed, 27 Dec 2000 04:14:21 +0000 |
| Subject: | Re: Seperating data from form into two tables | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31914@lists.php.net to get a copy of this message | ||
> 1. Do I need to open two database connections (one for inserting clients
> and one for inserting data into interests)
No.
> 2. Is it recommended that I do this insert with two functions or does it
> matter?
That's mostly up to you. I personally would just use code like this:
$connection = mysql_connect('localhost', 'user', 'password') or
die('Could
not connect to database.');
mysql_select_db('databasename', $connection) or die('Could not select
database');
$query = "insert into clients(name, address) values('$name',
'$address')";
$insert = mysql_query($query, $connection) or die(mysql_error() . $query);
$client_id = mysql_insert_id($connection);
$query = "insert into interests(client_id, whatever) values($client_id,
'$whatever')";
$insert = mysql_query($query, $connection) or die(mysql_error() . $query);
Actually, I would recommend redesigning your database to have a one-to-many
table of interests. IE:
Clients:
ID Name Address ...
1 Billy Bob Whatever
2 John Boy Whatever
.
.
.
Interests
Client_id Interest
1 Skating
1 Skiing
2 Bicycling
2 Skiing
.
.
.
This way, there is no pre-determined limit to a client's interests. Also,
you can easily ask for all clients interested in "skiing" for example.
You then need to iterate through the Interest array and execute an INSERT
for each one. Or, you can figure out that multiple-insert syntax if you are
super concerned about performance.
> I'm hoping someone can help me out. I've posted my PHP script at
> www.ucalgary.ca/~myuen/phpscript.html.
>
> Thanks in advance,
> Mike
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>