Log in

View Full Version : How to insert data into multiple table in mySQL



davelf
04-26-2010, 09:28 AM
How to insert data into multiple table in mySQL?

i just know this one:



$sql="insert into daily_quest(content,date,answer) values('$content','$date','$answer')";


and is there any configuration for the primary or foreign key in mysql database design?

thx for your attention....

djr33
04-26-2010, 05:02 PM
Can you give more information? I'm not sure if I know the answer, but I also don't completely understand the question.
It sounds like you could do this using two different queries, but you want to combine these queries into a single query-- is that right?
If so, can you write out both queries so that someone can try to show you how to combine them?

davelf
04-27-2010, 10:15 AM
yup like what u mention, i want to combine 2 insert query into 1 query, this is the example:



$date = date("d-m-y");
$id_quest = $_POST['txtidquest'];
$id_answer = $_POST['txtidanswer'];
$content = $_POST['txtcontent'];
$answer = $_POST['txtanswer'];
$answer1 = $_POST['txtanswer1'];
$answer2 = $_POST['txtanswer2'];
$answer3 = $_POST['txtanswer3'];
$answer4 = $_POST['txtanswer4'];
$answer5 = $_POST['txtanswer5'];
$answer6 = $_POST['txtanswer6'];

$sql="insert into daily_quest(id_daily_quest,content,date,answer) values('$id_quest','$content','$date','$answer')";
$sql2="insert into daily_quest_ans (id_daily_quest_ans,answer1,answer2,answer3,answer4,answer5,answer6) values('$id_answer','$answer1','$answer2','$answer3','$answer4','$answer5','$answer6')";


it's ok to use 2 insert, i just want to know how to combine it, i already search and see example but i still don't figure it out, how it work, i hope u can give an example... thx:)

boogyman
04-27-2010, 03:37 PM
If you separate the statements with a semi-colon, you can execute them as "one query".



INSERT INTO tbl(field, field) VALUES(val, val); INSERT INTO tbl2(field2, field2) VALUES(val2, val2);

davelf
04-28-2010, 01:32 AM
ok it done know and it work perfectly in the database.

here's the new problem.
How to call or select from multiple table?

here is my code to call or select data from single table:


<?php
include "connect.php";
$sql ="select * from user";
$rs = mysql_query($sql);
while(
$fetch= mysql_fetch_array($rs)){
?>

<tr class="fontContent">
<td> <?=$fetch[1] ?> </td>
<td> <?=$fetch[2] ?></td>
<td> <?=$fetch[4] ?></td>
<td class="news fontNewsBlue"><a href="edituser.php?kode=<?=$fetch[0]?>">Edit</a><strong> |</strong> <a href="deleteuser.php?kode=<?=$fetch[0]?>">Delete</a> </td>
</tr>

<?php
}
include "disconnect.php";
?>


How to select data from multiple table?

thx...

djr33
04-28-2010, 05:18 AM
You will need to look into merging the two tables into a temporary fake table. You can't query two tables at once, but you can create a very complex query that would merge two tables then be able to query it.
In my opinion it's a lot easier to just deal with the data after in PHP or whatever other language you're using.