Jump to content

Inserts & Selects


yddib

Recommended Posts

$comment=$_POST['comment'];
$linda='1';


mysql_query("SELECT f_name, l_name FROM gradinfo WHERE u_id = '$id'");
mysql_query("INSERT INTO comments (from_u_id,comment,u_id,f_name,l_name) VALUES ('$id', '$comment','$linda','f_name','l_name')");

 

We know this inserts f_name & l_name instead of the name linked with $id.

How do I drag the 2 selected fields to be inserted in another table?

Link to comment
https://forums.phpfreaks.com/topic/123923-inserts-selects/
Share on other sites

There are a couple ways, the first is to select the data into PHP values (which you are doing already), then use them in the insert. The other way is to use a INSERT...SELECT statement.

 

But...before we go down those roads, I'm going to propose that you don't store the f_name,l_name in the comments table. You have the ID of the gradinfo table, so when you need to call the data, you can just do a JOIN:

SELECT a.comment,b.f_name,b.l_name FROM comments a LEFT JOIN gradinfo b ON a.from_u_id = b.u_id

Link to comment
https://forums.phpfreaks.com/topic/123923-inserts-selects/#findComment-639736
Share on other sites

Archived

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

×
×
  • 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.