943,851 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1652
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 3rd, 2008
0

insrting from one table to another

Expand Post »
hi guys...

i wanna select a single cell (not full row) from one MySQL table and insert the value into another table. The datatype of the (value being extracted) and the (column of insertion) are the same.

somebody pls help...
Reputation Points: 10
Solved Threads: 0
Light Poster
akshit is offline Offline
46 posts
since Jun 2008
Jul 3rd, 2008
0

Re: insrting from one table to another

well i guess the problem is that when i use the command

$res = select * from table where column = 'value';

then AN ENTIRE RECORD is returned into $res.

Then, when i try to assign the value of $res into the other table, i am trying to assign a RECORD into a COLUMN, which is probably causing he error....

in case this is correct, how do i get over this??
Reputation Points: 10
Solved Threads: 0
Light Poster
akshit is offline Offline
46 posts
since Jun 2008
Jul 3rd, 2008
0

Re: insrting from one table to another

This is how you fetch the record and this is how you insert it.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jul 3rd, 2008
0

Re: insrting from one table to another

I have referredd to the links ou indicated, and this is the code i have come up with. But this doesnt seem to work either. I guess the error is based on the usage of ("") , ('') and (``) at different points in the query.

Somebody please clarify... with the code...

MY CODE
php Syntax (Toggle Plain Text)
  1. $res = select * from table1 where column1 = 'value';
  2. mysql_query("Update table2 set col2 = '$res[`1`]' ");

thx...
Last edited by peter_budo; Jul 10th, 2008 at 9:58 am. Reason: Keep It Organized - please use [code] tags
Reputation Points: 10
Solved Threads: 0
Light Poster
akshit is offline Offline
46 posts
since Jun 2008
Jul 3rd, 2008
0

Re: insrting from one table to another

Okay. This is how you do it.
php Syntax (Toggle Plain Text)
  1. $res = mysql_query("select * from table1 where column1='value'");
  2. while($row = mysql_fetch_array($res)) {
  3. $value2 = $row['column2']; //assign column2 value to $value2
  4. $query = mysql_query("update table2 set col2='$value2'"); //update col2 in table2
  5. }
  6. ?>
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jul 3rd, 2008
0

Re: insrting from one table to another

i tried your solution... it does not produce an error, but no UPDATE occurs...
Reputation Points: 10
Solved Threads: 0
Light Poster
akshit is offline Offline
46 posts
since Jun 2008
Jul 3rd, 2008
0

Re: insrting from one table to another

Post your code. Lets see where you have gone wrong.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jul 4th, 2008
0

Re: insrting from one table to another

The CODE

php Syntax (Toggle Plain Text)
  1. // retreiving data from table 1
  2.  
  3. $r1 = mysql_query("Select * from table1 where Column1 = '$test' ") or die(mysql_error());
  4.  
  5.  
  6. // updating the other table
  7.  
  8. while($row = mysql_fetch_array($r1))
  9. {
  10. $value2 = $row['Rate'];
  11.  
  12. $query = mysql_query("update table2 set amt = '$value2' ");
  13. }

plz help...

thx...
Last edited by peter_budo; Jul 9th, 2008 at 7:17 am. Reason: Keep It Organized - please use [code] tags
Reputation Points: 10
Solved Threads: 0
Light Poster
akshit is offline Offline
46 posts
since Jun 2008
Jul 4th, 2008
0

Re: insrting from one table to another

Before while have this.
print mysql_num_rows($r1); I believe, your query doesn't return any records.
The above print statement, will print the number of records found for the select query.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Jul 7th, 2008
0

Re: insrting from one table to another

Click to Expand / Collapse  Quote originally posted by nav33n ...
Before while have this.
print mysql_num_rows($r1); I believe, your query doesn't return any records.
The above print statement, will print the number of records found for the select query.

Hey nav33n.

You were right. IO tried that command an it showed 0, indicating that no rows were returned by SQL.

Now pls suggest what i shud do...

i shall be highly grateful fr ur co-operation.

thx a lot...
Reputation Points: 10
Solved Threads: 0
Light Poster
akshit is offline Offline
46 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Back Button problem
Next Thread in PHP Forum Timeline: Storing button clicks





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC