I’m trying to insert data about a user on my website but I think the MySQL query I’m using is invalid. I want to insert the value ‘$profile’ into the ‘pph_user_bio’ column inside the ‘pph_user_profile’ table however inside that table is a foreign key linking back to a table named ‘users’ which stores the user’s ‘_id’ so I only want the database to insert the data ($profile) into the corresponding ‘_id’ which is set as the current session id.
$query = "INSERT INTO pph_user_profile (pph_user_profile) VALUES ($profile)" .
"SELECT _id " .
"FROM users" .
"WHERE _id = ". $_SESSION['_id_of_user'];
$result = mysql_query($query);
I’m pretty new to MySQL so is there anyone who can tell me where I’ve made the error?
EDIT:
Here is an updated query, I’m still trying to do the same thing and no rows exist in ‘pph_user_profile’ so It’s definitely insert.
$query = "INSERT INTO pph_user_profile (_id) VALUES " . $_SESSION['_id_of_user'];
mysql_query($query);
$query = "INSERT INTO pph_user_profile (pph_user_bio) VALUES ($bio) WHERE _id = " . $_SESSION['_id_of_user'];
mysql_query($query);
I think you may be looking for an
UPDATEstatement instead of anINSERT:You use
UPDATEto set the value on an existing record, and you can use aWHEREclause to specify which record to update.To insert a new row, setting both columns, you can do this:
To insert a new row, but update it if the key already exists, use
INSERT INTO ... ON DUPLICATE KEY UPDATE: