cturner Posted October 20, 2006 Share Posted October 20, 2006 I am not sure whether this is a PHP or MySQL question. Anyway I am wanting to insert data from one table to another. My question is how can I insert a userid from the username that is logged in into another table after I have selected the userid and the username?Here is the code that I am working with:[code] $query = "SELECT * FROM `users` WHERE `username` = '$username'" or die ("Could not select because: ".mysql_error()); $sqlquery = mysql_query ($query) or die("Could not query because: ".mysql_error()); if (mysql_num_rows($sqlquery)) { //$row = mysql_fetch_assoc($sqlquery); $insert = "INSERT INTO `commentpost` (`articleid`, `userid`, `comment`, `date_entered`) VALUES ('$articleid', `$userid', '$comment', '$date')" or die ('Could not insert data into the table because: '.mysql_error()); if (mysql_query ($insert)) { // once the data has been added go to the comment_added page header ('Location: comment_added.php'); } else { print "<p>Could not add the entry because: <b>" . mysql_error() . "</b>. The query was $query.</p>"; } }[/code] Quote Link to comment Share on other sites More sharing options...
buildakicker Posted October 20, 2006 Share Posted October 20, 2006 I am not sure if I read this right, but you want to get the Loggedin persons ID and put it in another table? If so, check if user is logged in, select user ID, append info to new table.hope that helps. Quote Link to comment Share on other sites More sharing options...
cturner Posted October 20, 2006 Author Share Posted October 20, 2006 Thanks buildakicker but can you please show me in code what you mean? Quote Link to comment Share on other sites More sharing options...
Skatecrazy1 Posted October 20, 2006 Share Posted October 20, 2006 usually the simplest way to transfer data table to table would be to extract it from the source table then insert it into the target table, I imagine. if you need some code tell me, but i'm figuring you know what you're doing. Quote Link to comment Share on other sites More sharing options...
cturner Posted October 20, 2006 Author Share Posted October 20, 2006 Thanks for your reply Skatecrazy1. Could you please post some code here? I am finding it hard to understand what you mean. Quote Link to comment Share on other sites More sharing options...
Skatecrazy1 Posted October 20, 2006 Share Posted October 20, 2006 okay, so first you need to get the data from your `users` (i'm presuming) table[code]<?php//connect to mysql$conn = @mysql_connect("host", "username", "password") or die(mysql_error());//select your database$rs = @mysql_select_db("database_name", $conn) or die(mysql_error());//setup sql to retrieve info$sql = "SELECT * FROM `users` WHERE `username`=$username";//execute query$rs = @mysql_query($sql, $conn) or die(mysql_error());//get data in array$row = mysql_fetch_array($rs);//set data into variables//this is just an example, change variables/array values as needed$userid = $row['id'];$comment_text = $row['comment'];$date_entered = $row['date_entered'];/*now make some more SQL that inserts the newly retrived data into your comment table*/$insert = "INSERT INTO `commentpost` (`articleid`, `userid`, `comment`, `date_entered`) VALUES ('', '$userid', '$comment_text', '$date_entered')"//now execute the query to insert the data into the target table@mysql_query($insert, $conn) or die(mysql_error());?>[/code]after that you can do whatever you want as far as sending the user to another page, that's just the general code to take the data out of the user table and insert it into the commentpost tablemake sure you edit some of the values (i arbitrarily decided on some of the names, so if you're using something different, go ahead and change it so it works) Quote Link to comment Share on other sites More sharing options...
cturner Posted October 20, 2006 Author Share Posted October 20, 2006 Thanks Skatecrazy1 you are a gem! Quote Link to comment Share on other sites More sharing options...
Skatecrazy1 Posted October 20, 2006 Share Posted October 20, 2006 no problem tell me if it works out Quote Link to comment Share on other sites More sharing options...
cturner Posted October 20, 2006 Author Share Posted October 20, 2006 Yes it worked for you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.