Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

UPDATE multiple tables using php

I wanted to UPDATE these details from few tables, can i know how can i do it? it seems not appearing.

<body>
    <form class="" action="view_update_emp.php" method="post">
        <label>Employee Number</label><br>
        <b><?php echo $row['emp_num']; ?><br></b>
        <label>First Name</label><br>
        <input type="text" name="first_name" class="form-control" placeholder="Enter first name" value="<?php echo $row['first_name']; ?>"><br>
        <label>Last Name</label><br>
        <input type="text" name="last_name" class="form-control" placeholder="Enter last name" value="<?php echo $row['last_name']; ?>"><br>
        <label>Date of Birth</label><br>
        <b> <?php echo $row['birth_date']; ?></b><br><br>
        <label>Date assigned for the job position</label><br>
        <b><?php echo $row['date_assign']; ?></b><br>
        <label>Salary</label><br>
        <input type="number" name="emp_salary" class="form-control" placeholder="Enter Salary" value="<?php echo $row['emp_salary']; ?>"><br>
        <button type="submit" class="btn btn-primary" name="update" value="Update Data">Update</button><br>
        <button type="submit" class="btn btn-primary" name="cancel" value="Cancel Data">Cancel</button><br>
    </form>

</body>
<?php
$connection = mysqli_connect("localhost", "root", "");
$db = mysqli_select_db($connection, 'amaz');

if (isset($_POST['update'])) {
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $query = "UPDATE `employee`  SET first_name='$_POST[first_name]',last_name='$_POST[last_name]' WHERE employee.first_name='$_POST[id]'";
    $query_run = mysqli_query($connection, $query);
    if ($query_run) {
        echo "sucess";
    }
} else if (isset($_POST['cancel'])) {
    echo "fail";
}
?>

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could replace this

<label>Employee Number</label><br>
<b><?php echo $row['emp_num']; ?><br></b>

With a hidden fields to hold the key you need later in a way that you can access it when you need to

    <input type="hidden" value="<?php echo $row['emp_num']; ?>" name="id">

The user will not see this, but it will allow $_POST[id] to hold the value of the emp_num when you get back to the PHP code after submission

You would also need to change the update query

WHERE employee.first_name='$_POST[id]'

To

WHERE employee.emp_num='$_POST[id]'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading