syh syh - 7 months ago 16
PHP Question

insert an id gotten from another table

I have a message table

**message**
---------------------------------------
msg_id (AI) | user_id | content


and user table

**user**
---------------------------------------
user_id (AI) | email | password | name


I would like to store the user who posted the message into the database. The codes I have written allows me to store the message submitted into the database but not the
user_id


if (isset($_POST['submit'])) { // Form has been submitted.
$msg = new msg();
$msg->user_id = ???
$msg->content = trim($_POST['content']);
if ($msg->createMsg()) {
//action
} else {
//action
}


the
createMsg()
is a sql statement that inserts my query.

how do I get the
user_id
from the
user
table to store into the
message
table?

Answer

store user data during login into session like $_SESSION['user'];

$msg->user_id = $_SESSION['user']['user_id'];
Comments