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.

<?php

  $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)
VALUES
('$_POST[EntrySlipNumber]','$_POST[BillNo]','$c','$_POST[ItemCode]','$_POST[BillDate]','$_POST[dt]','$_POST[Qty]','$_POST[EntryType]','$_POST[RequesterName]','$_POST[SupplierName]')";

  $sql2="INSERT INTO MainStrInvmain (EntrySlipNumber, BillNo,ItemName, ItemCode, BillDate, Date, Qty,  EntryType, RequesterName, SupplierName)
VALUES
('$_POST[EntrySlipNumber]','$_POST[BillNo]','$c','$_POST[ItemCode]','$_POST[BillDate]','$_POST[dt]','$_POST[Qty]','$_POST[EntryType]','$_POST[RequesterName]','$_POST[SupplierName]')";


mysql_query($sql,$con);
mysql_query($sql2,$con);
  
      }

header('Location: MAIN_STORE_entry.php');


mysql_close($con)



?>
Member Avatar for diafol

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.

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.