Editar/actualizar valores de base de datos MySQL desde formulario HTML

I have a form that currently submits values to a mysql database. After each submit is performed the data that was just inserted in to the mysql db is echoed below the form. 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. 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? Ejemplo

After submit - Insert/Display


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

$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


$db_select  = $db_con->prepare("
SELECT a.name, 
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){
          $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>';



Formulario 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>
        <div id="course_catalog"></div>
        Personel Information:
        <ul id="pq_entry_1" class="clonedSection">
                <input id="person_fname_1" name="person_fname_1" placeholder="Person #1 - First Name" type="text" />
                <input id="person_lname_1" name="person_lname_1" placeholder="Last Name" type="text" />
                <input id="person_email_1" name="person_email_1" placeholder="Email" type="text" />
                <input id="person_phone_1" name="person_phone_1" placeholder="Phone" type="text" />
                <input id="person_fax_1" name="person_fax_1" placeholder="Fax" type="text" />
                <input id="person_contact_1"  name="person_contact_1" type="checkbox" />Concact
                <input id="person_instructor_1"  name="person_instructor_1" type="checkbox" />Instructor
        <input type='button' id='btnAdd' value='add another Person' />
        <input type='button' id='btnDel' value='delete Delete' />   
    <input value="SAVE" name="submit" type="submit">
See Results:
echo $final_result;
echo $final_result2;

preguntado el 27 de noviembre de 13 a las 07:11

You have to do an ajax call for inserting as well as updating so that you can send back all the fields as your ajax response and you can populate the values using jquery. -

@issueNo329 can you give me an example? -

2 Respuestas

After insertion of the data u select the Max(academy_id) and store in variable.


then pass this variable.

respondido 27 nov., 13:13

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.

respondido 27 nov., 13:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.