Link to home
Start Free TrialLog in
Avatar of Žan Anđić
Žan AnđićFlag for Bosnia and Herzegovina

asked on

PHP insert multiple tables with more records in one table MySQL

I have two tables, one is users, and second is family_list,

Tables:

users > id, name,surname,dateofbirth,address,contact  (id is autoincrement and primary key)
family_list > id, name, dateofbirth, user_id

I made relation, id primary key with user_id foreign key.

Now, I need php form to store data to multiple tables, where I can add user to table users and for family_list table I need to can add more than one record for one user. So, one user can have more childern in family _list table. I am using bootstrap. How to do that, PHP and Jquery?

If there is some tutorial for beginner.

Thank you
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

You have multiple parts to your question and it will be best to start with just one.

First, bootstrap and jquery are for the front end view and do not pertain to inserting data on the back end.  Do you need help with the front end as far as how to lay out a form and make it look good?

Part of your puzzle is coming up with the right database structure.  Are you 100% comfortable with what you have or do you need assistance with designing database tables?  

The back end functions can be broken down to how to accept data from a form and how to insert data to your database.

Which of these options do you need help with? or multiple/all?
Hi @ Žan Anđić
 
You can insert Many data in multiple table

If you want to insert some data in two table you can do it like this

  You have to fire 2 query and check at your end which should be insert in which table

if you have dynamic field than inset like this
//suppose this is your table and you have more input field
<input type='name[]' name='name'>


<?php
if(isset($_POST['submit'])){

            $name = $_POST['name'];
            $mob = $_POST['mobile'];            
            $count = count($name);
            for($i=0; $i<$count; $i++) {
            if(trim($_POST['name'][$i] && $_POST['mobile'][$i] !='')) {
            $Query = "INSERT INTO Taable (name,mobile) VALUES(".$_POST['name'][$i]."','".$_POST['mobile'][$i]."')";
            $Result6 = mysql_query($conn, $Query);    
                   }    
            }   
}
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Žan Anđić

ASKER

Thank you for answers.

Do you need help with the front end as far as how to lay out a form and make it look good?

I want to make it look good, I want in frontend to be available to add more fields for childrens with + button.

Part of your puzzle is coming up with the right database structure.  Are you 100% comfortable with what you have or do you need assistance with designing database tables?  
And I will have two more table similar family_list, so there will be 4 tables final.

USERS |FAMILY_LIST | ASSETS | UNIT

ASSETS > id, type, value, users_id
UNIT > id, unit_name,period, users_id

User generated image
So, I need to make one form, maybe is good to make step1 step2 step3 step4... where I will be able to add new user, and add records to parent tables of user. When I adding records to parent tables (family_list, assets, unit) I need to be able to click on button + (add row), if I need to add more than one record.
If you have no development experience then this is not really the forum to be asking this question. Either you need to open a Gig and pay someone to do it for you or you need to enroll in some development courses to get you up to speed with the technologies that you need to use.

I would build your front end as an SPA (single page application) that captures all the data to common model - when you are done you submit the model to a Service via AJAX - the service validates the data and adds it to the database (using a process similar to the one shown above) and then returns a status.

For this you would need the following skills

JavaScript
jQuery / Angular / React
HTML
CSS
PHP
MySQL

Other options are to use a PHP Framework like Laravel to build the application and have the screens run from the server - this makes (in my view) the model a bit more difficult as you have to store the progressive parts in a session.
Thank you Julian,

I want to learn...and this is exercise for me, before I was made with PHP, HTML and CSS no relational database.

I was made form in HTML, CSS and PHP, one file named adduser.php and after submit is done redirect to step2.php where I can add record in second table, using mysqli_insert_id... is there some tutorial for beginners for inserting data into relational tables?

Thank you again
The step approach has problems especially in situations where a parent / child exists. For instance, if user completes step 1 and User record is created - but then abandon's the process - do you want to keep the half completed record or discard it - how do you know when you should do that.
Žan,

My suggestion to help you is to pick one thing to work on for this thread and once answered, move on to the next.

Get the code to work that Julian provided here to run in your own environment. You may run into errors just doing this and need help to make it work.  Stick with that for now and he made it easy as he provided test data that is hard coded. Make sure you understand it and then close out this thread.

The next thing you want to work on is creating a basic form without any fancy formatting that can post to the code provided and add data that way.  You will want to make a separate question just for that.

Lastly, create a question on making the form/page work using bootstrap front end css.  Before attempting that, I suggest spending a day going through an easy course over at https://www.codecademy.com/catalog/language/html-css.   Start with the basic html, then css then responsive.   You can wrap up by viewing the docs at https://getbootstrap.com/docs/4.0/getting-started/introduction/ and looking at examples such as https://getbootstrap.com/docs/4.0/examples/checkout/ and try on your own to make it what you need. Then come back here to get help.
I would add to Scott's post - try to get your thinking in terms of client and service. Your client (the browser) collects information and sends it to a service which processes it. By keeping a distinct separation between the code that does the view and the code that interacts with the database it is much easier to test and complete the various steps.

For instance - if you are going to go the AJAX route you would create a script that accepts the data from the forms and then runs in a similar fashion to the script I posted earlier. The script takes data in and returns a response indicating the result of the operation - that is all - no HTML or other logic is mixed up in that process.

This allows you to test that service on its own without your interface - you can setup a simple form with the relevant fields on that posts to the service. Once the service is working you can tick that box and move to the next phase knowing that as long as you send the right data to the service it will do what it is supposed to do.

You will see many PHP coding examples that are a mashup of PHP code and HTML - this is very difficult to
a) Debug
b) Extend
c) Read

Where possible compartmentalise your functionality into services and / or classes that perform specific tasks - these can be independently tested and then used with confidence when they are working.
Thank you for your answers. I start with learning, I created basic form which post data into tables,
Oh dear, I object to this proposal very strongly. The code given is problematic on many levels

1. It uses unsanitized data straight from $_POST
2. It suggests the mysql deprecated library

And lastly it does not answer the question.

The question was how to do an insert into a parent / child relationship - this requires getting the insert_id from the parent insert and using that in the child - not even touched on in the recommended answer.

If this question has to be closed I would leave the recommended answer out of the points as it simply does not address the question asked.