Jump to content


Photo

Inserting data?


  • Please log in to reply
8 replies to this topic

#1 cturner

cturner
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • LocationMy computer

Posted 20 October 2006 - 12:02 AM

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:
	$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>";
		}
    }


#2 buildakicker

buildakicker
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 20 October 2006 - 12:08 AM

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.

#3 cturner

cturner
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • LocationMy computer

Posted 20 October 2006 - 12:11 AM

Thanks buildakicker but can you please show me in code what you mean?

#4 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 20 October 2006 - 01:54 AM

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.
fuck it or fight it

#5 cturner

cturner
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • LocationMy computer

Posted 20 October 2006 - 02:28 AM

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

#6 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 20 October 2006 - 03:29 AM

okay, so first you need to get the data from your `users` (i'm presuming) table

<?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());
?>

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)
fuck it or fight it

#7 cturner

cturner
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • LocationMy computer

Posted 20 October 2006 - 03:32 AM

Thanks Skatecrazy1 you are a gem!

#8 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 20 October 2006 - 03:46 AM

no problem tell me if it works out
fuck it or fight it

#9 cturner

cturner
  • Members
  • PipPipPip
  • Advanced Member
  • 148 posts
  • LocationMy computer

Posted 20 October 2006 - 08:32 AM

Yes it worked for you.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users