baby83 Posted July 22, 2008 Share Posted July 22, 2008 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... Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/ Share on other sites More sharing options...
mmarif4u Posted July 22, 2008 Share Posted July 22, 2008 Do you have any code. Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596276 Share on other sites More sharing options...
baby83 Posted July 22, 2008 Author Share Posted July 22, 2008 nope... but can u teach me how to generate the code? a simple code... so that i can try... please... Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596293 Share on other sites More sharing options...
mmarif4u Posted July 22, 2008 Share Posted July 22, 2008 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. Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596301 Share on other sites More sharing options...
baby83 Posted July 22, 2008 Author Share Posted July 22, 2008 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()); Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596304 Share on other sites More sharing options...
baby83 Posted July 22, 2008 Author Share Posted July 22, 2008 the problem is... when the first time insert is okay. but to insert the 2nd value for hobby using the same id is not okay. it cant insert into the 2nd row of the table ['hobbies'].... Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596306 Share on other sites More sharing options...
mga_ka_php Posted July 22, 2008 Share Posted July 22, 2008 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()); Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596308 Share on other sites More sharing options...
mmarif4u Posted July 22, 2008 Share Posted July 22, 2008 Did the id represent the person name in the personal table. from where $id is coming. Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596310 Share on other sites More sharing options...
baby83 Posted July 22, 2008 Author Share Posted July 22, 2008 yes... the $id is represent the person name.... Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596317 Share on other sites More sharing options...
mmarif4u Posted July 22, 2008 Share Posted July 22, 2008 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. Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596320 Share on other sites More sharing options...
baby83 Posted July 22, 2008 Author Share Posted July 22, 2008 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? Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596328 Share on other sites More sharing options...
mmarif4u Posted July 22, 2008 Share Posted July 22, 2008 ok. Did you try and modify the code above. try to modify it for your need. it will work. Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596336 Share on other sites More sharing options...
baby83 Posted July 22, 2008 Author Share Posted July 22, 2008 ive tried so many times... but i havent found any correct result... Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596339 Share on other sites More sharing options...
mmarif4u Posted July 22, 2008 Share Posted July 22, 2008 post your updated code. Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596344 Share on other sites More sharing options...
baby83 Posted July 22, 2008 Author Share Posted July 22, 2008 $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); Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596346 Share on other sites More sharing options...
mmarif4u Posted July 22, 2008 Share Posted July 22, 2008 So your are Malaysian.rite. Now tel me from where $id is coming,is it from input form. Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596350 Share on other sites More sharing options...
baby83 Posted July 22, 2008 Author Share Posted July 22, 2008 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. Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596358 Share on other sites More sharing options...
mmarif4u Posted July 22, 2008 Share Posted July 22, 2008 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. Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596363 Share on other sites More sharing options...
baby83 Posted July 22, 2008 Author Share Posted July 22, 2008 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) Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596369 Share on other sites More sharing options...
LemonInflux Posted July 22, 2008 Share Posted July 22, 2008 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 Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596371 Share on other sites More sharing options...
mmarif4u Posted July 22, 2008 Share Posted July 22, 2008 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. Link to comment https://forums.phpfreaks.com/topic/115976-inserting-multiple-data-into-multiple-tables-using-one-id/#findComment-596373 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.