I'm very new to MySQL and PHP. I have two tables. The first one is users
which has the information for the site users and the second table is called SendRequests
.
I want to store the data each user posts (it's like an ad posting site) in the second table where I want the first column to be for the email of the logged-in user so I can find the posts from that user later. This is how I am trying to do it:
Using two INSERT
s as follows:
$query1=" INSERT INTO SendRequests (email) SELECT email FROM users
WHERE id = '".$_SESSION['id']."' LIMIT 1";
mysqli_query($link, $query1);
$_SESSION['id']= mysqli_insert_id($link);
$query = "INSERT INTO `SendRequests` (`country`, `city`, `fee`) VALUES
('".mysqli_real_escape_string($link,$_POST['departurecountry'])."',
'".mysqli_real_escape_string($link, $_POST['departurecity'])."',
'".mysqli_real_escape_string($link,$_POST['fee'])."')."')
WHERE id = '".$_SESSION['id']."'";
mysqli_query($link, $query);
You can use this query to insert data while searching for your email :
INSERT INTO SendRequests(email, country, city, fee)
SELECT email, "FR", "Paris", 56.65
FROM SendRequests
WHERE id = 1;
This will works if a row with id 1
is already inserted. You can by this way merge your 2 insert in a single one.
So if the SendRequests with id 1 is equal to anon@host.com
, and you run the query above, you'll have at least 2 rows :
ID EMAIL COUNTRY CITY FEE
========================================
1 anon@host.com US Florida 150.25
2 anon@host.com FR Paris 56.65
There is an error in the second query - a spare ."')
towards the end ~ change it to:
$query = "INSERT INTO `SendRequests` (`country`, `city`, `fee`) VALUES
('".mysqli_real_escape_string($link,$_POST['departurecountry'])."',
'".mysqli_real_escape_string($link, $_POST['departurecity'])."',
'".mysqli_real_escape_string($link,$_POST['fee'])."')
WHERE id = '".$_SESSION['id']."'";