I have a form that has fields that auto populate with data from mysql table. These input fields get their values from mysql table called person
. I am able to successfully display these values. But I am not able to update these values correctly. The values share a foreign key called academy_id
. When I update the fields it changes the values to all the same value. How can I successfully update each value? EXAMPLE
Table values before form submit:
+----+------------+------------+-----------+
| ID | academy_id | first_name | last_name |
+----+------------+------------+-----------+
| 1 | 15 | Person1 | Last1 |
| 2 | 15 | Person2 | Last2 |
+----+------------+------------+-----------+
After trying to change the value
+----+------------+------------+-----------+
| ID | academy_id | first_name | last_name |
+----+------------+------------+-----------+
| 1 | 15 | Person2 | Last2 |
| 2 | 15 | Person2 | Last2 |
+----+------------+------------+-----------+
//DATBASE SELECT QUERY
$id = 15;
$db_select3 = $db_con->prepare("
SELECT a.name,
a.academy_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;
$result3 = '';
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'] ."'/></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>";
//UPDATE VALUES IN DATABASE
if(isset($_POST['submit'])) {
$f = 1;
while(isset($_POST['person_fname_' . $f]))
{
$person_fname = $_POST['person_fname_' . $f];
$person_lname = $_POST['person_lname_' . $f];
$query_init3 = "UPDATE person SET academy_id=:id, first_name=:person_fname, last_name=:person_lname WHERE academy_id=:id;";
$query_prep3 = $db_con->prepare($query_init3);
$query_prep3->execute(array(
"id" => $id,
"person_fname" => $person_fname,
"person_lname" => $person_lname
));
$f++;
}
}