473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.
Expand|Select|Wrap|Line Numbers
  1. PRODUCTS TABLE:
  2.  
  3. Id  product_name  product_desc   category    brand
  4. 1   product1      desc1          cat1        brand1                                                     
  5. 2   product2      desc2          cat2        brand2
  6.  
  7. DEALS TABLE
  8.  
  9. Id     deal_name      productid    
  10. 1      todayoffer        2           
  11.  
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
  3.  
  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."'");
  9.  
  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. ?>
  18.  
When running the code it is returning false value. would really appreciate some help
Jul 19 '14 #1
1 1079
Luuk
1,047 Expert 1GB
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:
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."');
  4.  
Jul 19 '14 #2

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

Similar topics

3
by: takilroy | last post by:
Hi, Does anyone know of a simple way to do this? I want to create an insert trigger for a table and if the record already exists based on some criteria, I want to update the table with the...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
1
by: Bryan | last post by:
Hi, I have two tables. I want to update two columns in my first table, and with two values and held in my #temp table but based on a RUN_DATE from my first table. Can anyone point me in...
6
by: Christopher Lusardi | last post by:
How can I fix this? When I do the below I get the error message: "Cannot insert explict value for identity column in table 'Employees' when IDENTITY_INSERT is set to OFF." To get this message,...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
2
by: feeman | last post by:
I read a previous posting and have tried to use it in my database to update a table called transaction table, but am having problems with it. I have pasted the following into the after event...
3
by: Manikandan | last post by:
Hi, I have table with three columns as below table name:exp No(int) name(char) refno(int) I have data as below No name refno 1 a 2 b 3 c
1
by: islandtalker | last post by:
Hi, In a stored procedure (MS SQL) I want to update a table to Add columns to it, and then update those new columns with data from a variable. The problem is that, after updating the table I...
1
by: mrobinsc | last post by:
** This SQL statement returns 4 rows SELECT COUNT(*) G.ACTIVITY_ID G.RESOURCE_TYPE G.RESOURCE_CATEGORY G.RESOURCE_SUB_CAT G.ANALYSIS_TYPE G.PROJECT_ID
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.