Call id of a table to a new table and insert values to this new table

I have recently started to write server scripts. Although the question may seem to be very basic but i am not able to get a solution.I have two tables one is PRODUCTS and the other is DEALS.


Id  product_name  product_desc   category    brand
1   product1      desc1          cat1        brand1                                                     
2   product2      desc2          cat2        brand2


Id     deal_name      productid    
1      todayoffer        2      

I wish to put the value of id (from PRODUCTS table) into productid and carry this value to DEALS table and then add deal_name along with it as shown above. The recent code that i am using is

require_once('config.php'); //connection


$inserts = mysql_query("insert into deals (dealname,productid) SELECT productid FROM products WHERE productid = '".$id."'");

    $posts[0]['message'] = 'Deal Registered';
    $idd = mysql_insert_id();
    $selectt = mysql_query("select * from deals where id = '".$idd."'");

    $posts[0]['detail'] = $selectt;
    header('Content-type: application/json');
    echo json_encode($posts);

Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.

Apart from SP’s valid point about mysql and to use PDO instead, what actually is your question? Is your code not working, and if not, in what way? Do you get error messages, and what are they?

One minor point is that I’d steer clear of using the same column name in two tables - I had some trouble with that in trying to retrieve both.