hi, there.. i have few problems here...

i have 2 table in my database.. and i want to take few data from first table and insert it into second table... how can i do that?? i don't want the user write it twice in different form...

is there any way that i can use???

Recommended Answers

All 5 Replies

INSERT INTO table2 SELECT * FROM table1;

Member Avatar for diafol

Show your table structure.
We cannot begin to guess what you're trying to do.
Also state which fields you need data inserted into in both tables.

i just start using php, so i'm sorry if i'm using wrong term for this...


This is the table structure..

<title>Untitled Document</title>

<?php

$idUser = $_REQUEST['idUser'];

require("connect.inc");

$query = mysqli_query($conn,"SELECT * FROM pelatih WHERE idUser='$idUser'");
$result = mysqli_fetch_array($query);

mysqli_close($conn);
?>

<div align="center">
  <p align="left"><a href="home.php">Back</a></p>
  <p><strong><u>PELATIH LATIHAN INDUSTRI</u></strong></p>
  <p>&nbsp;</p>
  
  <form action="detailspelatihproses.php" method="post">
  <table width="679" border="0">
  
    <tr>
      <td width="219">Nama Pelatih</td>
      <td width="8">:</td>
      <td width="430"><label><span class="style1">
          <label><span class="style16"><?php print $result['nama'];?></span><span class="style16">
          <input type="hidden" name="nama" value="<?php print $result['nama'];?>"/>
          </span></label>
        </label></td>
    </tr>
    <tr>
      <td>Nama Institut Pengajian</td>
      <td>:</td>
      <td><label><span class="style1">
          <label><span class="style16"><?php print $result['namaInstitut'];?></span><span class="style16">
          <input name="institut" type="hidden" id="institut" value="<?php print $result['namaInstitut'];?>"/>
          </span></label>
        </label></td>
    </tr>
    <tr>
      <td>No. Kad Pengenalan</td>
      <td>:</td>
      <td><label><span class="style1">
          <label><span class="style16"><?php print $result['noIC'];?></span><span class="style16">
          <input name="noic" type="hidden" id="noic" value="<?php print $result['noIC'];?>"/>
          </span></label>
        </label></td>
    </tr>
    <tr>
      <td>Jantina</td>
      <td>:</td>
      <td><label><label><span class="style1">
          <label><span class="style16"><?php print $result['jantina'];?></span><span class="style16">
          <input name="jantina" type="hidden" id="jantina" value="<?php print $result['jantina'];?>"/>
          </span></label>
        </label></label></td>
    </tr>
    <tr>
      <td>Tarikh Mula Latihan</td>
      <td>:</td>
      <td><label><span class="style1">
          <label><span class="style16"><?php print $result['TarikhMula'];?></span><span class="style16">
          <input name="tarikhmula" type="hidden" id="tarikhmula" value="<?php print $result['TarikhMula'];?>"/>
          </span></label>
        </label></td>
    </tr>
    <tr>
      <td>Tarikh Akhir Latihan</td>
      <td>:</td>
      <td><label><span class="style1">
          <label><span class="style16"><?php print $result['TarikhAkhir'];?></span><span class="style16">
          <input name="tarikhakhir" type="hidden" id="tarikhakhir" value="<?php print $result['TarikhAkhir'];?>"/>
          </span></label>
        </label></td>
    </tr>
    <tr>
      <td colspan="3"><div align="center">
        <label></label>
      </div></td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <table width="390" height="205" border="0">
    <tr>
      <td width="63" height="46">Kategori</td>
      <td width="8">:</td>
      <td width="142"><label></label>
          <p>
            <label>
            <input type="radio" name="kategori" id="radio" value="Lewat">
              Lewat <br>
              <input type="radio" name="kategori" id="radio2" value="Tidak Hadir">
              Tidak Hadir </label>
        </p></td>
    </tr>
    <tr>
      <td>Tarikh</td>
      <td>:</td>
      <td><label>
        <input type="text" name="tarikh" id="tarikh">
      </label></td>
    </tr>
    <tr>
      <td height="85">Catatan</td>
      <td>:</td>
      <td><textarea name="catatan" id="catatan" cols="45" rows="5"></textarea></td>
    </tr>
    <tr>
      <td height="40" colspan="3"><label></label>
          <p align="center">
            <label>
            <input type="submit" name="simpan" id="simpan" value="Submit">
            </label>
        </p></td>
    </tr>
	
  </table>
  </form>
  <p>&nbsp;</p>
</div>

this one is the process

<?php

$nama = $_REQUEST['nama'];
$institut = $_REQUEST['institut'];
$ic = $_REQUEST['noic'];
$kategori = $_REQUEST['kategori'];
$tarikh = $_REQUEST['tarikh'];
$catatan = $_REQUEST['catatan'];
$idUser= $_REQUEST['idUser'];

 if (isset($_POST['simpan'])) 
 { 

 if (!$kategori | !$tarikh  | !$catatan )
 {

		?> 
<script type="text/JavaScript">
	alert("Sorry, please fill ALL field below!");
	setTimeout("location.href='detailspelatih.php';",00000);
</script>
<?php
}

else

{
 
			$query ="INSERT INTO kehadiran (nama, namaInstitut, noIC, kategori, tarikh, catatan) VALUES  ('$nama', '$institut', '$ic', '$kategori', '$tarikh', '$catatan')";



	mysqli_query($conn,$query);
	
	mysqli_close($conn);	
	
?>
<script type="text/JavaScript">
	alert("Data Succesfully Saved!");
	setTimeout("location.href='home.php';",00000);
</script>
<?php
} 
}
?>

and now i want to insert the field 'nama', 'institusi' and 'ic' into different table in database...

those 3 have been insert earlier in different table.. but, i want to use it again, and i don't want the user to write their info again....

how can i do that???
can you get it what i mean???
(i'm sorry, my english quite bad)

It's like u need to read more about database. there is what is call redundancy, you dont have to re-insert does data into another table when you have them already.

instead of inserting those values into separate table why not add another field (column) on to the table call id, that will auto-increment on any record insertion, and then use this id to select a particular column's from the table.

An advice, you are new to this language and for you to have knowledge of it you have to learn of which entails reading, get ebooks online or search for php/mysql tutorial online.

$query ="INSERT INTO kehadiran (id, nama, namaInstitut, noIC, kategori, tarikh, catatan) VALUES  (null '$nama', '$institut', '$ic', '$kategori', '$tarikh', '$catatan')";

oh... i never know about redundancy.. thanks for the info.. so, i just need to use the same table and add 'id' to make my work easier... right??

thanks for the help and your advice... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.