I am have data from mysql table person
populate several input fields through a foreach loop. All values share a foreign key called academy_id
. I am able to run a single query to store values or run a query to update the values. I am now trying to accomplish the ability to do Delete the values stored in the table and then re insert the values. When trying to do so I get a syntax error near the Insert query statement. How can I delete the value first and then re insert it in the table? Here is an live EXAMPLE
//SELECT Query to display values
<?php
$id = 15;
$db_select3 = $db_con->prepare("
SELECT a.name,
a.academy_id,
p.person_id,
p.first_name,
p.last_name
FROM academy a
LEFT JOIN person p ON a.academy_id = p.academy_id
WHERE a.academy_id = :id
");
if (!$db_select3) return false;
if (!$db_select3->execute(array(':id' => $id))) return false;
$results3 = $db_select3->fetchAll(\PDO::FETCH_ASSOC);
if (empty($results3)) return false;
echo "<strong>Personel Information:</strong>";
$s = 1;
foreach ($results3 as $value3){
echo "<ul id=\"pq_entry_".$s."\" class=\"clonedSection\">";
echo "<li><input id=\"person_fname_".$s."\" name=\"person_fname_".$s."\" placeholder=\"Person #1 - First Name\" type=\"text\" value='" . $value3['first_name'] ."'/><input type=\"hidden\" name=\"person_id_".$s."\" value='". $person_id = $value2['person_id']."'/></li>";
echo "<li><input id=\"person_lname_".$s."\" name=\"person_lname_".$s."\" placeholder=\"Last Name\" type=\"text\" value='" . $value3['last_name'] ."'/></li>";
echo "</ul>";
$s++;
}
echo "<input type='button' id='btnAdd' value='add another Person' />
<input type='button' id='btnDel' value='Delete' /></br>";
Delete then Insert values - Problem here
if(isset($_POST['submit'])) {
//Insert or Update Values
$f = 1;
while(isset($_POST['person_fname_' . $f]))
{
$person_fname = $_POST['person_fname_' . $f];
$person_lname = $_POST['person_lname_' . $f];
$query_init3 = "DELETE FROM person WHERE academy_id=:id INSERT INTO person (academy_id, first_name, last_name) VALUES (:id,:person_fname,:person_lname);";
$query_prep3 = $db_con->prepare($query_init3);
$query_prep3->execute(array(
"id" => $id,
"person_fname" => $person_fname,
"person_lname" => $person_lname
));
$f++;
}
}