dongying6896 2013-12-02 08:40
浏览 37
已采纳

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

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++;
        }
}
  • 写回答

1条回答 默认 最新

  • dongsong4418 2013-12-02 08:59
    关注

    Get rid of that $s variable and directly associate the person.ID with the inputs, like so:

    $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>';
    }
    

    Then you will process $_POST['person'] on the update page something like:

    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;
        */
    }
    

    I minimized the example because your code was all over the place, but hopefully this gives you a rough template to work from - see the php:HTML FAQ for how this $_POST relationship works.

    I've also completely glossed over the need for HTML entity protection on the values coming out of the database to prevent from attacks such as XSS. You should look into these sooner, rather than later.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)