| | |
insrting from one table to another
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
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:
The insert you want can be done with the result of the first query, or you can do the select as a subquery:
Of course {condition} should be replaced with your WHERE condition.
Subqueries should work on MySQL 4.1 and later I believe...
eg:
PHP Syntax (Toggle Plain Text)
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:
PHP Syntax (Toggle Plain Text)
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Jun 2008
Posts: 46
Reputation:
Solved Threads: 0
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--
(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 ---
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...
In the GUI, i tried--
sql Syntax (Toggle Plain Text)
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 ---
php Syntax (Toggle Plain Text)
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 7:17 am. Reason: Keep It Organized - please use [code] tags
•
•
•
•
mysql_query(" update table2 set col2 = ( Select col1 from table1 where col3 = '$_POST["Tests"]' ) where ID = '$_POST["ID"]' ") or mysql_error()
php Syntax (Toggle Plain Text)
mysql_query("update table2 set col2 = ( Select col1 from table1 where col3 = '".$_POST['Tests']."') where ID = '".$_POST['ID']."'") or mysql_error();
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Jun 2008
Posts: 46
Reputation:
Solved Threads: 0
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...
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:
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.
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.
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:
PHP Syntax (Toggle Plain Text)
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.
PHP Syntax (Toggle Plain Text)
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
Other Threads in the PHP Forum
- Previous Thread: Back Button problem
- Next Thread: Storing button clicks
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code codingproblem cron curl database date directory display download dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select send server sessions sms snippet soap source space speed sql static structure syntax system table tutorial up-to-date update upload url validation validator variable video web wordpress xml youtube






