Jump to content

Archived

This topic is now archived and is closed to further replies.

cturner

Inserting data?

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks buildakicker but can you please show me in code what you mean?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thanks for your reply Skatecrazy1. Could you please post some code here? I am finding it hard to understand what you mean.

Share this post


Link to post
Share on other sites
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 table

make 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)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.