I need a way to select the values from another table and then insert them In another table. And using the id which is posted from .
in this file i need to select data from 2 differnt table and then insert it into the table MainStrInv . Please check this code or give me a simple example how I can Select values from 2 different tables and insert into 3rd table. I need this in insert.php page which is the action of my form. Thanks in advance.


  $con = mysql_connect("localhost","root","root");
if (!$con)
  die('Could not connect: ' . mysql_error());
  mysql_select_db("mobilink", $con);
  $a= $_POST[ItemCode];
 // echo $a;
  $result = mysql_query("SELECT ItemName FROM itemcodename  WHERE ItemCode='$a'");
while($row = mysql_fetch_array($result))

$c= $row['ItemName'];
  $sql="INSERT INTO MainStrInv (EntrySlipNumber, BillNo,ItemName, ItemCode, BillDate, Date, Qty,  EntryType, RequesterName, SupplierName)

  $sql2="INSERT INTO MainStrInvmain (EntrySlipNumber, BillNo,ItemName, ItemCode, BillDate, Date, Qty,  EntryType, RequesterName, SupplierName)


header('Location: MAIN_STORE_entry.php');


8 Years
Discussion Span
Last Post by ayesha789

JUst out of curiosity, why are you adding the exact same data to two tables? It doesn't make sense to me. Show us the table structure and purpose of these tables. I'm pretty sure there's an easier way to achieve your goals. Data duplication is almost never the best option.

Also you're using raw form data by the looks of it. You'd want to clean this before using it in your SQL.


My problem is solved. actually I have on table which includes item codes and item names. and in entry i use thes item and code for entry data. so i have to populate . its working now.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.