-1

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.

tibory
  • 3
  • 1
  • So you're trying to create an `INSERT` statement. Have you started with some tutorials on MySQL? (And PHP, since you tagged that as well?) Where are you stuck? Inserting a record into a table, and including dynamic values in that record, is covered by just about any tutorial. – David Aug 20 '17 at 12:00
  • Hello David, I have the SQL $sql = "INSERT INTO land (land_id, land_name) VALUES ('land_ID????', '$land_Name')"; but I want the land_ID field to populated with the unique id from the users table – tibory Aug 20 '17 at 12:05
  • Ok, and how will you know which ID value to use? What is the logic to determine that? – David Aug 20 '17 at 12:05
  • the user is logged in and I am collecting the ID to hold the session – tibory Aug 20 '17 at 12:07
  • 2
    So use the ID that's in the session value? Your example already shows you using a PHP variable in your query, so what's stopping you from using another PHP variable in your query? (Note: This is wide open to SQL injection. You might want to take a look here: https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – David Aug 20 '17 at 12:08

1 Answers1

0
$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'];
James V
  • 60
  • 1
  • 7