Jump to content

inserting multiple data into multiple tables using one ID


baby83

Recommended Posts

i have a problem while posting the data to the database. i have 3 tables. and each table have their own primary (which is 'id'). i want to insert the data using one form for about 10 data.

 

example:

i want to register new data (name: mark, age: 24, hobbies: reading, music)

 

and i want to insert the data (name, age in one table 'personal')

and others will be insert into table 'hobbies'.

 

i want the table be like this:

 

'personal'

 

id      name      age

1      mark      24

 

 

'hobbies'

 

id    hobbies        personal_id

1      reading            1

2      music              1

 

can anyone help me? i'm new for php and mysql. please... :(

 

 

 

 

 

1stly you have to create a form:

 

<form action="" method="POST">
<input type="text" name="name">
<input type="text" name="age">
<input type="text" name="hobbies">
</form>

Then in PHP:

Connect to mysql and db...
//insert into table
$sql="insert into personal(name,age)values('$_POST['name']','$_POST['age']')";
$query=mysql_query($sql) or die(mysql_error());

//write the rest for other tables.....

 

This is just a basic idea, try to build it for your need.

 

this is what i've tried...

 

<form name="form1" method="post" action="test.php">

  <table width="202" border="0" align="center">

    <tr>

      <td width="82">Name</td>

      <td width="110"><input type="text" name="name"></td>

    </tr>

    <tr>

      <td>Age</td>

      <td><input type="text" name="age"></td>

    </tr>

    <tr>

      <td>Hobby</td>

      <td><input type="text" name="hobby"></td>

    </tr>

  </table>

  <p align="center">

    <input type="submit" name="Submit" value="Submit">

  </p>

</form>

 

 

 

 

$name = $_POST['name'];

$age = $_POST['age'];

$hobby=$_POST['hobby'];

 

 

 

    $query_update = "INSERT INTO personal(name,age,id)

VALUES

('$name','$age','$id')

";

    $result_update = mysql_query($query_update) or die(mysql_error());

 

$query_update2 = "INSERT INTO hobbies (id,hobby)

VALUES

('$id','$hobby)

";

    $result_update2 = mysql_query($query_update2) or die(mysql_error());

1stly you have to create a form:

 

<form action="" method="POST">
<input type="text" name="name">
<input type="text" name="age">
<input type="text" name="hobbies">
</form>

Then in PHP:

Connect to mysql and db...
//insert into table
$sql="insert into personal(name,age)values('$_POST['name']','$_POST['age']')";
$query=mysql_query($sql) or die(mysql_error());

//write the rest for other tables.....

 

This is just a basic idea, try to build it for your need.

 

 

additional to this, if your going to use the id of first sql query,

$sql="insert into personal(name,age)values('$_POST['name']','$_POST['age']')";
$query=mysql_query($sql) or die(mysql_error());
$last_id=mysql_insert_id();

$sql="insert into hobbies(hobbies,personal_id)values('$_POST['hobbies']','$last_id')";
$query=mysql_query($sql) or die(mysql_error());

 

Now one thing more.Are you using sessions after login.Are these pages  without login.

If without login,then better to store them in one table like:

Personal

id name age hobbies

 

Not a good idea to store just one column in other table.

i know... but the actual problem is... 1 have 3 tables...

 

1) Parent

2) Child

3) Personal

 

the data will be like this:

 

table 'parent'

 

parent_id      parent1    parent2    parent3        ic

1                  mark      josh          alicia          1011

2                  anita      susan        william          1234

 

table 'child'

 

child_id        child1      child2        child3          parent        ic        parent_id

1                a              b              c              mark          1011          1

2                d              e              f              josh          1011          1

3                g              h              i              alicia          1011          1

4                j                k              l              anita          1234          2

5                m              n              o              susan          1234          2

6                p                q              r              william          1234        2

 

i want the data will insert like that.... can help me?

 

 

$id = $_POST['id'];

$tatooibu = $_POST['tatooibu'];

 

$ic = $_POST['ic'];

$baka = $_POST['baka'];

$tarikhdaftar= $_POST['tarikhdaftar'];

$hargapawah=$_POST['hargapawah'];

$pesanan=$_POST['pesanan'];

$perjanjian=$_POST['perjanjian'];

 

 

$spesis = $_POST['spesis'];

$jantina = $_POST['jantina'];

 

$status = $_POST['status'];

 

$jurusan = $_POST['jurusan'];

 

 

 

$_rs ="UPDATE ternakan SET ternakan.ic ='$ic',

  ternakan.tatooibu ='$tatooibu',

 

ternakan.hargapawah='$hargapawah',

ternakan.pesanan='$pesanan',

ternakan.perjanjian='$perjanjian',

  ternakan.status ='$status',

  ternakan.jurusan ='$jurusan',

  ternakan.jantina='$jantina',

  ternakan.baka ='$baka',ternakan.tarikhdaftar ='$tarikhdaftar', 

  ternakan.spesis ='$spesis'

  WHERE ternakan.id = '$id';";

 

$query=mysql_query($_rs);

 

$_rs2 ="UPDATE ibu SET ibu.ic ='$ic',

  ibu.tatooibu ='$tatooibu'

  WHERE ibu.parent_id = '".$_GET['id']."';";

 

$query2=mysql_query($_rs2);

 

$_rs3 ="UPDATE anak SET anak.ic ='$ic',

  anak.tatooibu ='$tatooibu'

 

  WHERE anak.child_id = '".$_GET['parent_id']."';";

 

$query3=mysql_query($_rs3);

yes im malaysian. actually i have emailed u.. asking about this prob. but its ok... hmm im a bit blank rite now...

 

the $id is from input form.

 

firstly, the user needs to register his personal. then the successful page will appear. and it asks the user either he wants to continue or not. if wants... he needs to insert his $ic and the system will find either the $ic is correct or not. if correct, the system will put a link and the user can click on it. then, he will fill the 'ternakan' form. the main prob occurs from here. the user needs to insert the parent name one by one until 10 times. when the user insert the first name (ex: mark) into the 'parent' table into 'parent1' row and it will also insert the name into the 'child' table into the 'parent' row. then the system will insert it into the database and the user needs to keep entering it til the parent's name finish.

tq for the mail.

I am using rarely that email address.anyway just check your mail.its in Malay.

But i am not Malay. a foreigner in Malaysia.HAHA.so plz ENGLISH.

About your problem.

you say the ID is coming from the form.is it primary in the db.i have a solution for your problem. As i already work on this issue b4.

But need your clear attention to this problem.

Any way i will PM you my email address,you can send me to that email.

ok... sorry. i thought u r malaysian too. sorry. yes the $id is primary.

 

'parent' table:

column (parent_id(primary key),parent1,parent2,parent3,ic)

 

'child' table:

column (child_id(primary key), child1, child2,child3,parent_name,ic,parent_id)

 

 

the hobbies personal_id should not be auto_increment. Do a SELECT query on the personal table to get the personal ID, then insert that into the hobbies table.

 

----------------

Now playing: Enter Shikari - Closing

via FoxyTunes

the 1st thing is that your parent_id is primary so no need to have $id.Just auto incremnt it in table,dats all

It will generate a key every time you insert a record there.

Now store that id in the session and pass it to the other page.Aslo can use GET method.

but session is more secure than GET.Hidden POST field also can.

Session is better here.

NOW.YOU have two tables parent and child.rite

1st parent will create a account and it will pass to the next page for child registration.

One thing more,what is parent1,parent2..... and child1,child2.....

I will OFF now,you can mail me if you want more help.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.