User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 423,637 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,184 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 561 | Replies: 19
Reply
Join Date: Jun 2008
Posts: 44
Reputation: akshit is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
akshit akshit is offline Offline
Light Poster

Re: insrting from one table to another

  #11  
Jul 7th, 2008
PS. I tried running the SELECT query from the SQL GUI (naviCAT), and i gave me the perfect result....
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: insrting from one table to another

  #12  
Jul 7th, 2008
If you run the same select query from navicat, it works fine, but, if you run it from your script, it doesn't return any records ? I can say this for sure. Something is wrong with your script. You are doing something wrong while building dynamic query.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: insrting from one table to another

  #13  
Jul 8th, 2008
If you want to select just one column, then make sure you ask for just one column. If you only need one row, then limit the results to one row...

eg:
SELECT column1 from table1 where {condition} LIMIT 1

The insert you want can be done with the result of the first query, or you can do the select as a subquery:
INSERT INTO table2 SET column1 = (SELECT column1 FROM table1 WHERE {condition} LIMIT1)

Of course {condition} should be replaced with your WHERE condition.

Subqueries should work on MySQL 4.1 and later I believe...
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: May 2008
Posts: 84
Reputation: antwan1986 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
antwan1986's Avatar
antwan1986 antwan1986 is offline Offline
Junior Poster in Training

Re: insrting from one table to another

  #14  
Jul 8th, 2008
Silly question I'm sure but have you got both your connection and select database strings? i.e. mysql_conn and mysql_select_db ?

Anthony
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
Reply With Quote  
Join Date: Jun 2008
Posts: 44
Reputation: akshit is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
akshit akshit is offline Offline
Light Poster

Re: insrting from one table to another

  #15  
Jul 8th, 2008
i tried this in the GUI (navicat), and it worked just perfect, but when i run it from my script, it doesnt work.

In the GUI, i tried--

  1. UPDATE table2 SET col2 = ( SELECT col1 FROM table1 WHERE col3 = 'value' ) WHERE ID = 5

(i chose a static vaue of 'id' and 'value' here, but in actual, have to use dynamic value for both, depending on user input coming from previous page, which is a form)

in the script(PHP), i wrote ---

  1. mysql_query(" update table2 set col2 = ( Select col1 from table1 where col3 = '$_POST["Tests"]' ) where ID = '$_POST["ID"]' ") or mysql_error()


PS-- "Tests" and "ID" are 2 fields entered by the user in the form on the previous page.

When i run this command from my PHP script, it dos not return any error, bu no updating occurs....

wat may be wrong?

pls suggest...

THX...
Last edited by peter_budo : Jul 9th, 2008 at 6:17 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: insrting from one table to another

  #16  
Jul 8th, 2008
mysql_query(" update table2 set col2 = ( Select col1 from table1 where col3 = '$_POST["Tests"]' ) where ID = '$_POST["ID"]' ") or mysql_error()
Are you sure this didn't give any error ? I am 100% sure this will generate an error (because of the mixup of ' and "). Try this.
  1. mysql_query("update table2 set col2 = ( Select col1 from table1 where col3 = '".$_POST['Tests']."') where ID = '".$_POST['ID']."'") or mysql_error();
P.S. Also check if $_POST['ID'] and $_POST['Tests'] is empty.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Jun 2008
Posts: 44
Reputation: akshit is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
akshit akshit is offline Offline
Light Poster

Re: insrting from one table to another

  #17  
Jul 8th, 2008
Originally Posted by nav33n View Post

P.S. Also check if $_POST['ID'] and $_POST['Tests'] is empty.




Thx a lot buddy.... u just got it right... i checked up, and as u suggested, the "$_POST['Tests']" variable was blank.... the reason being (OK, now pls dont beat me up)....

i the previous page(the form where all data is collected, i used 'T'ests), whereas in the script, i was using $_POST["tests"].....

thx 4 pointing that out.... i wud never hav managed without u pointing it out...

thx a lot....

thx...
Reply With Quote  
Join Date: Jun 2008
Posts: 44
Reputation: akshit is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
akshit akshit is offline Offline
Light Poster

Re: insrting from one table to another

  #18  
Jul 8th, 2008
P.S. There was no error due he the ('),(`) or the (") .....
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: insrting from one table to another

  #19  
Jul 8th, 2008
Cool
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: insrting from one table to another

  #20  
Jul 9th, 2008
Just a note on this:

When you place user input into an SQL query, always make sure you escape it for SQL injections (used hack into your database). For Strings you can use mysql_real_escape_string() or mysql_escape_string() depending on support for it. If its an integer, then just use intval().

eg:

mysql_query("update table2 set col2 = ( Select col1 from table1 where col3 = '".mysql_real_escape_string($_POST['Tests'])."') where ID = '".intval($_POST['ID'])."'") or mysql_error();

In mysql, the Integer fields can be treated as strings, but for portability its good to not have qoutes around field values that are integers as it would most likely not work on other SQL databases.

Also, its good to optimize the queries. A simple optimization is to use the LIMIT keyword.

mysql_query("update table2 set col2 = ( Select col1 from table1 where col3 = '".mysql_real_escape_string($_POST['Tests'])."' LIMIT 1) where ID = ".intval($_POST['ID'])." LIMIT 1") or mysql_error();

On large datasets LIMIT can really speed up the query. What happens is the database does not have to go through its whole index, but instead stop on the first result that matches the WHERE portion of the query.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 10:10 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC