在mysql表中使用相同的FK更新多个值

时间:2013-12-02 08:40:46

标签: php mysql

我有一个表单,其中包含自动填充mysql表中数据的字段。这些输入字段从名为person的mysql表中获取其值。我能够成功显示这些值。但我无法正确更新这些值。这些值共享一个名为academy_id的外键。当我更新字段时,它会将值更改为所有相同的值。如何成功更新每个值? EXAMPLE

表单提交前的表值:

+----+------------+------------+-----------+
| ID | academy_id | first_name | last_name |
+----+------------+------------+-----------+
|  1 |         15 | Person1    | Last1     |
|  2 |         15 | Person2    | Last2     |
+----+------------+------------+-----------+

尝试更改值

+----+------------+------------+-----------+
| 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++;
        }
}

1 个答案:

答案 0 :(得分:1)

摆脱那个$ s变量并直接将person.ID与输入相关联,如下所示:

$db_select3  = $db_con->prepare("
    SELECT  a.name,
            a.academy_id,
            p.id as 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 ");

/* ... */ 

foreach ($results3 as $value3){
    $id = $results3['person_id'];
    $first_name = $results3['first_name'];
    $last_name = $results3['last_name'];

    echo '<ul id="pq_entry_'.$s.'" class="clonedSection">';
    echo '<li><input id="person_fname_'.$s.'" name="person['.$id.'][fname]"
         placeholder="Person #1 - First Name" type="text" value="'
         $fname.'/></li>';
}

然后你将在更新页面上处理$ _POST ['person'],如:

foreach($_POST['person'] as $person_id => $person) {
    /* 
       UPDATE person SET academy_id=:id, 
       first_name=:person_fname, last_name=:person_lname
       WHERE id = $person_id;
    */
}

我最小化了这个例子,因为你的代码到处都是,但希望这能为你提供一个粗略的模板 - 请参阅php:HTML FAQ了解这个$ _POST关系是如何工作的。

我还完全掩盖了对数据库中出现的值进行HTML实体保护的需求,以防止XSS等攻击。你应该尽早而不是晚些时候研究这些。