简体   繁体   中英

How to automatically update the column in phpmyadmin

My table name is reservations:

| reservation_id(pk) | number of people | capacity |

I want that when I enter number of people than the no_of_people column subtract from the capacity column and the capacity column is updated for the new no_of_people. Please tell me how I can update the capacity table?

Model view:

 public function insert_into_db($people)
{
    $sql = "INSERT INTO `reservation` (`no_of_people`) VALUES (?) ";
    "SELECT *, `capacity`-`no_of_people` AS `capacity` FROM `reservation`";
    $this->db->query( $sql, array($people) );
}

Controller view

public function insert_people()
{

    $people =$this->input->post('no_of_people');

    $this->Books_model->insert_into_db($people);
    redirect('Booking_Controller/view');
}

view file:

<form action="<?php echo base_url();?>index.php/Booking_controller/insert_people" method="POST">

<table>
    <tr>
        <td> <label for="no of people" class="col-sm-2 control-label"><td>Number Of People*</td></label>
        </td>
        <td> <input type = 'text' name='no_of_people'></td>
    </tr>

    <tr>

        <td><input type='submit' class="btn btn-success"  value="Book a Table"></td>

    </tr>

</table>

Please help me find where I made the mistake?

As @nathanmac alludes to, this is a bad database design paradigm. The capacity and number of seats still available are completely unrelated to a specific reservation and shouldn't be stored there. Among other serious computational problems, it means you're always referencing the most recent reservation to get the seat count and if a reservation ten people prior revises their number of people in the reservation, you're going to have to recompute for each of the remaining reservations as well.

A better way -- and note that there are probably half a dozen good ways to implement this -- is to dynamically count the number of people as nathanmac said. Any time you need to determine how many seats are available, you fetch the count of total capacity and subtract the dynamically computed number of reservations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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