Results 1 to 6 of 6

Thread: How to insert data into multiple table in mySQL

  1. #1
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    472
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default How to insert data into multiple table in mySQL

    How to insert data into multiple table in mySQL?

    i just know this one:

    Code:
    $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....
    Last edited by davelf; 04-26-2010 at 09:40 AM.
    _____________________

    David Demetrius // davejob
    _____________________

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    472
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default

    yup like what u mention, i want to combine 2 insert query into 1 query, this is the example:

    Code:
    	$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
    _____________________

    David Demetrius // davejob
    _____________________

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    If you separate the statements with a semi-colon, you can execute them as "one query".

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

  5. #5
    Join Date
    Jul 2009
    Location
    Binus University
    Posts
    472
    Thanks
    78
    Thanked 21 Times in 21 Posts

    Default how to select from multiple table?

    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:
    Code:
    <?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...
    _____________________

    David Demetrius // davejob
    _____________________

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •