Cheers,
So, I have a "parent table" where I need to insert some stuff. The "parent table" - FILES contains
id_file
id_cust - foreign key for idcust from table Customers
register_date
id_object - foreign key for idcust from table Objects
Table Customers
idcust
cust_name
address
phone
Table Objects
idobject
name_object
I have created an HTML form to insert a new FILE which contains 2 combo boxes and register date
<form method="post" action="">
<tr>
<td>Customer name: </td> <td>
<select name="cust_name">
<?php
$sql = mysqli_query($conn, "SELECT * FROM customers");
while ($row = $sql->fetch_assoc()){
echo "<option value=\"cust_name1\">" . $row['cust_name'] . "</option>";
}?>
</select></td></tr>
<tr>
<td>Register date</td>
<td> <input type="date" name="register_date"/></td>
</tr>
<tr>
<td>Object: </td> <td>
<select name="object">
<?php
$sql = mysqli_query($conn, "SELECT * FROM objects");
while ($row = $sql->fetch_assoc()){
echo "<option value=\"name_object1\">" . $row['name_object'] . "</option>";
}?>
</select></td></tr>
<tr><td colspan=2> <input name ="submit" type="submit" value="Add a new file"></td>
</tr></form>
What I have tried:
Since yesterday, I have tried a looooot of queries, this is the last one:
<?php
$conn = mysqli_connect("localhost", "root", "", "testdb");
if ($conn -> connect_error){
die("Connection failed:". $conn-> connect_error);
}
if(!empty($_POST['submit'])){
$cust_name = $_POST['cust_name'];
$register_date = date('Y-m-d',strtotime($_POST['register_date']));
$name_object =$_POST['name_object'];
$sql = "INSERT INTO files VALUES (
(SELECT * FROM customers c WHERE c.cust_name = $cust_name),
$register_date,
(SELECT * FROM objects o WHERE o.name_object = $name_object)
)";
$conn->query($sql);
if($conn->error){
echo $conn->error;
} else
{
$message= "We have added the file no. " .$conn->insert_id;
}
}
?>
I have tried with LEFT JOIN, without any success... Can you help me with this query? Thank you.