insert value of a previous table to a new table and update the new table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • learner001
    New Member
    • Jun 2014
    • 4

    insert value of a previous table to a new table and update the 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.
    Code:
    PRODUCTS TABLE:
    
    Id  product_name  product_desc   category    brand
    1   product1      desc1          cat1        brand1                                                     
    2   product2      desc2          cat2        brand2
    
    DEALS TABLE
    
    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

    Code:
    <?
        require_once('config.php'); //connection
        
        $dealname=$_REQUEST['dealname'];
        $productid=$_REQUEST['productid'];
        $productid=$_REQUEST['productid'];
        $id=$_REQUEST['id'];
    $inserts = mysql_query("insert into deals (dealname,productid) values ('".$dealname."','".$productid."') SELECT productid FROM products WHERE productid = '".$id."'");
    	
    $posts[0]['message'] = 'deal Added';
    $idd = mysql_insert_id();
    $selectt = mysql_query("select * from deals where id = '".$idd."'");
    $results =  mysql_fetch_assoc($selectt);
    $posts[0]['detail'] = $results;
    header('Content-type: application/json');
    echo json_encode($posts);
    ?>
    When running the code it is returning false value. would really appreciate some help
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    First you should read the warning here
    "This extension is deprecated as of PHP 5.5.0, and will be removed in the future. "

    Also you should read about SQL Injection, and how to prevent it. Read:
    http://stackoverflow.com/questions/6...jection-in-php and Google it....

    Secondly, your lines 5 and 6 are the same......?

    Now to your question.
    You cannot have two SQL statement in one mysql_query

    Change line 8 to something like:
    Code:
    $getproductid = mysqlquery("SELECT productid FROM products WHERE productid = '".$id."'");
    $productid = mysql_result($getproductid, 0);
    $inserts = mysql_query("insert into deals (dealname,productid) values ('".$dealname."','".$productid."');

    Comment

    Working...