Jump to content

Inserting data?


cturner

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]
Link to comment
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)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.