By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,649 Members | 696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,649 IT Pros & Developers. It's quick & easy.

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

P: 4
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.
Expand|Select|Wrap|Line Numbers
  3. Id  product_name  product_desc   category    brand
  4. 1   product1      desc1          cat1        brand1                                                     
  5. 2   product2      desc2          cat2        brand2
  9. Id     deal_name      productid    
  10. 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

Expand|Select|Wrap|Line Numbers
  1. <?
  2.     require_once('config.php'); //connection
  4.     $dealname=$_REQUEST['dealname'];
  5.     $productid=$_REQUEST['productid'];
  6.     $productid=$_REQUEST['productid'];
  7.     $id=$_REQUEST['id'];
  8. $inserts = mysql_query("insert into deals (dealname,productid) values ('".$dealname."','".$productid."') SELECT productid FROM products WHERE productid = '".$id."'");
  10. $posts[0]['message'] = 'deal Added';
  11. $idd = mysql_insert_id();
  12. $selectt = mysql_query("select * from deals where id = '".$idd."'");
  13. $results =  mysql_fetch_assoc($selectt);
  14. $posts[0]['detail'] = $results;
  15. header('Content-type: application/json');
  16. echo json_encode($posts);
  17. ?>
When running the code it is returning false value. would really appreciate some help
Jul 19 '14 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 1,035
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: 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:
Expand|Select|Wrap|Line Numbers
  1. $getproductid = mysqlquery("SELECT productid FROM products WHERE productid = '".$id."'");
  2. $productid = mysql_result($getproductid, 0);
  3. $inserts = mysql_query("insert into deals (dealname,productid) values ('".$dealname."','".$productid."');
Jul 19 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.