简体   繁体   中英

Update DB fields as based in inputs received from user

My DB has table names which consists of various columns like Fname , Lname , Age_chkd , gndr_chkd , profession_gdk and many more

I have table to display these values

<table>
    <tr>
        <td>F Name</td>
        <td>L Name</td>
        <td>Age</td>
        <td>Profession</td>
    </tr>
    <tr>
        <td contenteditable="true" id="as_1">Value from DB</td>
        <td contenteditable="true" id="as_2">Value from DB</td>
        <td contenteditable="true" id="as_3">Value from DB</td>
        <td contenteditable="true" id="as_4">Value from DB</td>
    </tr>
</table>

I tried two approaches:

  1. Make the name of td same as of DB column names but am not in favor of this approach as DB will be exposed.
  2. Make the name of td as someprefix::DB_column and separate value after :: at PHP end but again not well versed approach m seeing in this.

Can anybody please let me know better and awesome approach for this?

Note: the table is getting updated by ajax as you are seeing contenteditable=true and below is the jquery that calls ajax

$("td[contenteditable=true]").blur(function(){
    var field_userid = $(this).attr("id") ;
    var value = $(this).text() ;
    $.post('updatemenu/update_orderstatus' , field_userid + "=" +value,function(data){
        if(data != '') {
            message_status.text(data);
        setTimeout(function(){message_status.hide()},3000);
        }
    });
});

You need models.

What you're describing here sounds to me like the ideal situation to use a Model. You've defined the Database Table. A model in PHP would be the layer between your application and the database.

This way, you can make changes using your application directly to the model, which enforces that the data is correct (or at least not harmful) before trying to update/mutate your database records.

A model could look like this:

class Names {

    private $id;
    public $fName, $lName, $Age, $Gender, $Profession;

    public static function GetById($id) { 
        // Get data by ID and fill the instance 
    }

    public function Save() { 
        // Check model, save to the database by using $this->Age, $this->Profession variables. 
    } 
}

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