简体   繁体   English

从HTML表单编辑/更新MySQL数据库值

[英]Edit/Update MySQL DB Values from HTML form

I have a form that currently submits values to a mysql database. 我有一个当前将值提交到mysql数据库的表单。 After each submit is performed the data that was just inserted in to the mysql db is echoed below the form. 每次提交完成后,将在表格下方回显刚刚插入到mysql db中的数据。 The form has dynamic input fields. 表单具有动态输入字段。 I am know wanting to add the possibility to edit the values previously inserted. 我知道要增加编辑以前插入的值的可能性。 I place an edit button that redirects the user to a personel_edit.php page in hopes of populating all the input fields accordingly to the academy_id . 我放置了一个编辑按钮,该按钮将用户重定向到personel_edit.php页面,以希望将所有输​​入字段相应地academy_idacademy_id In the code you will see how I do the initial insert and then after do a select to display the information inserted. 在代码中,您将看到我如何进行初始插入,然后执行选择以显示插入的信息。 But I am not sure how to populate this dynamic fields or pass the data to the edit page according to the academy_id ? 但是我不确定如何根据academy_id填充此动态字段或将数据传递到编辑页面? Example

After submit - Insert/Display 提交后-插入/显示

 if(isset($_POST['submit'])){

$name = $_POST['name'];
$acad_id = $_POST['acad_id'];
$courses_offered=$_POST['courses_offered'];

$query_init  = "INSERT INTO academy (name, academy_id)  VALUES (:name, :acad_id);"; 
$query_prep = $db_con->prepare($query_init);
$insert_result = $query_prep->execute(array(
    "name" => $name,
    "acad_id" => $acad_id
));

$s = 1;
while(isset($_POST['person_fname_' . $s]))
{
    $contact_role = isset($_POST['person_contact_' . $s]) ? 1 : 0;
    $instructor_role = isset($_POST['person_instructor_' . $s]) ? 1 : 0;
    $person_fname = $_POST['person_fname_' . $s];
    $person_lname = $_POST['person_lname_' . $s];
    $person_email = $_POST['person_email_' . $s];
    $person_phone = $_POST['person_phone_' . $s];
    $person_fax = $_POST['person_fax_' . $s];

    $query_init2 = "INSERT INTO person (academy_id, contact_role, instructor_role, first_name, last_name, person_email, person_phone, person_fax) VALUES (:acad_id,:contact_role,:instructor_role,:person_fname,:person_lname,:person_email,:person_phone,:person_fax);";
    $query_prep2 = $db_con->prepare($query_init2);
    $insert_result2 = $query_prep2->execute(array(
        "acad_id" => $acad_id,
        "contact_role" => $contact_role,
        "instructor_role" => $instructor_role,
        "person_fname" => $person_fname,
        "person_lname" => $person_lname,
        "person_email" => $person_email,
        "person_phone" => $person_phone,
        "person_fax" => $person_fax
    ));
    $s++;


}

$db_select  = $db_con->prepare("
SELECT a.name, 
       a.academy_id,
       p.contact_role,
       p.instructor_role,
       p.first_name,
       p.last_name,
       p.person_email,
       p.person_phone,
       p.person_fax
FROM academy a
INNER JOIN person p ON a.academy_id = p.academy_id
WHERE a.academy_id = :acad_id
");
if (!$db_select) return false;
    if (!$db_select->execute(array(':acad_id' => $acad_id))) return false;
    $results = $db_select->fetchAll(\PDO::FETCH_ASSOC);
    if (empty($results)) return false;
    $final_result = '';
    $first = true;
    foreach ($results as $value){
        if($first){
          $first = false;
          $final_result .= "<b>Academy Name: </b>".$value['name']."<b>  ID: </b>".$value['academy_id']."</br>";
        }
          $final_result .= "---------------------PERSONEL-----------------------</br>";
          $final_result .= "<b>First Name: </b>".$value['first_name']."</br><b>Last Name: </b>".$value['last_name']."</br><b>Email: </b>".$value['person_email']."</br>";
          $final_result .= "<b>This person has the role of an instructor: </b>".$value['instructor_role']."</br><b>This person has the role of a contact: </b>".$value['contact_role']."</br>";
          $final_result .= "<b>Phone: </b>".$value['person_phone']."</br><b>Fax: </b>".$value['person_fax']."</br>";        
    }

    $final_result .= '<button name="change" id="change" onClick="check(' . $value['academy_id'].');">Edit</button>';

}

    ?>

HTML Form HTML表格

<form action="courses.php" method="POST">
Name: <input type="text" name="name"></br>
Academy<input id="academy_id" name="acad_id" placeholder="Academy ID" type="text" /></br>
            How many courses offered?
            <select name="courses_offered">
                <option value="default">---Select---</option>
                <option value="1">1</option>
                <option value="2">2</option>
            </select>
        <div id="course_catalog"></div>
        Personel Information:
        <ul id="pq_entry_1" class="clonedSection">
            <li>
                <input id="person_fname_1" name="person_fname_1" placeholder="Person #1 - First Name" type="text" />
            </li>
            <li>
                <input id="person_lname_1" name="person_lname_1" placeholder="Last Name" type="text" />
            </li>
            <li>
                <input id="person_email_1" name="person_email_1" placeholder="Email" type="text" />
            </li>
            <li>
                <input id="person_phone_1" name="person_phone_1" placeholder="Phone" type="text" />
            </li>
            <li>
                <input id="person_fax_1" name="person_fax_1" placeholder="Fax" type="text" />
            </li>
            <li>
                <input id="person_contact_1"  name="person_contact_1" type="checkbox" />Concact
            </li>
             <li>
                <input id="person_instructor_1"  name="person_instructor_1" type="checkbox" />Instructor
            </li>
        </ul>
        <input type='button' id='btnAdd' value='add another Person' />
        <input type='button' id='btnDel' value='delete Delete' />   
        </br>
        </br>
    <input value="SAVE" name="submit" type="submit">
</form> 
</body>
See Results:
<?php
echo $final_result;
echo $final_result2;

After insertion of the data u select the Max(academy_id) and store in variable. 插入数据后,请选择Max(academy_id)并存储在变量中。

 $var_academy_id=Max(academy_id)

then pass this variable. 然后传递此变量。

Through your query if you want to edit your form use hidden field 通过查询是否要编辑表单使用隐藏字段

<input type="hidden" name="status" value="update">

then check inside post variable value of this hidden field if update then use update query or if submit then use insert query. 然后检查此隐藏字段的内部post变量值,如果更新则使用更新查询,或者如果提交然后使用插入查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM