I've got two different tables 'user' and 'candidates' and I'm trying to add values from one table to another by user selection.
This is the code displaying 'name' and 'surname' rows from 'user' table:
echo "<select class='form-control custom-select' name='candid'>";
while ($row = $result->fetch_assoc())
{
$candid = $row['name'] .' '. $row['surname'];
echo "<option value='.$candid.'>".$candid."</option>";
}
echo "</select>";
I concatenated two columns and assigned their values to $candid variable. Now I need to add these values to the 'name' and 'surname' columns in 'candidates' table. I also tried other version of this code:
while ($row = $result->fetch_assoc())
{
echo "<option value='candid'>".$row['name'] .' '. $row['surname']."</option>";
}
With this SQL query:
$sql = ("INSERT INTO candidates (name, surname) VALUES ('$row[name]', '$row[surname]')");
if ($conn->query($sql) === TRUE)
{
echo "New record created successfully";
}
else
{
// $conn->error only for testing purposes
echo "Error: " . $sql . "<br>" . $conn->error;
}
But it doesn't work, so there is my question. Is it possible to do this using only PHP and SQL?