|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
entering values in two tables
I have a form where several fields are filled out. I want 3 of those fields to be inserted into one table and all the fields to be entered in a second table but I'm not sure how to code it.
This is what I have so far, but I'd like first_name, last_name, and email to go into a seperate table named 'mail'. I want to do this so people can delete themselves from the database without deleting their entire profile. Code:
INSERT INTO clients SET first_name ='$f_name', last_name='$l_name', address='$address', city = '$city', state = '$state', zip = '$zip', email= '$email', h_phone = '$h_phone', w_phone = '$w_phone', c_phone = '$c_phone', type = '$client'"; Thanks, Rob |
|
#2
|
||||
|
||||
|
Code:
INSERT INTO clients (first_name, last_name, address, city, state, zip, email, h_phone, w_phone, c_phone, type) VALUES ('$f_name', '$l_name'...);
And then run the same query with the table and the field lists redefined. I'm a little curious about why you want people to delete themselves, but not really, and why you're going about it in this manner. For the sake of having your data normalized, and since you're keeping the data anyway, you might consider simply adding a flag field to your clients database (type enum) that defaults to "Y" and having users set it to "N" when they wish to delete themselves. This'll allow you to keep data on these people but to prevent its public display. |
|
#3
|
|||
|
|||
|
In addition to the client being added to the list upon filling out the form, I also want to be able to use a different form to add emails manually. If I used the same form that the client does, I will be entering in a bunch of blank fields that aren't necessary. The form I would use would just be firstname, lastname, email. What I meant about people deleting themselves was from the mailing list.
I like your idea of flagging each one better, but didn't like the idea of all the empty rows. I'm not experienced enough in database design to know if that is even a problem? Any thoughts? |
|
#4
|
||||
|
||||
|
Ah, the solution to that is to have separate tables and to establish a relationship between them. So your clients table would contain an auto_increment field as a primary key and would contain the contact info, active flag, etc. The mail table would have an integer field the same size as the auto_increment field in the clients table, not unique in this case. In other words, the integer field in the mail table could contain the same integer across multiple rows, and this integer would correspond to the key in the client's table. And the mail table of course contains only the mail info and the id field. When you're selecting from the database, you'll join on that integer field to get all mails from the mail table whose id correspond to the unique id in the clients table. Using this structure prevents you from having any data duplication and should execute quickly as well.
|
|
#5
|
|||
|
|||
|
Excellent! After reading your post a couple of times to digest it, it sank in and is a great idea. Not only fixed my problem but taught me some things.
Thank you. ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Databases > General SQL Development > entering values in two tables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|