Home > Software design >  Why data doesn't get to the server?
Why data doesn't get to the server?

Time:10-05

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

<body>
    <form  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"  placeholder="Enter first name" value="<?php echo $row['first_name']; ?>"><br>
        <label>Last Name</label><br>
        <input type="text" name="last_name"  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"  placeholder="Enter Salary" value="<?php echo $row['emp_salary']; ?>"><br>
        <button type="submit"  name="update" value="Update Data">Update</button><br>
        <button type="submit"  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

CodePudding user response:

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]'
  • Related