tibory tibory - 1 month ago 11
MySQL Question

Inserting data into a SQL table but getting a specific column data from another table with different column names

I am looking for some help,

I have two tables, one being users and another being land. users can have many pieces of land.

my users table holds - ID | USERNAME | PASS
The Land table holds - ID | Land_ID |Land_Name

I am trying to create an SQL statement that will allow me to create a new 'land' record but it will populate the 'Land_ID' with the 'ID' from the users table. Therefore:

The user with ID 1 has land name green which has the Land_ID 1 meaning they own that land?

I hope that makes sense.

Thank you in advance for any help.

Answer Source
$var = $session_id; //use whatever variable you use when collecting the session id, from what I gather you are using the unique id from the users table for this.

$sql = "INSERT INTO land (land_id, land_name) VALUES ('$var', '$land_name')"

Extra info:

If this is coming from an HTML form you can input the users session id as a hidden input:

<input type = "hidden" name="id" value="<?php echo['id']; ?>">

In this case you could then define the variable as:

$var = $_POST['id'];