jquery - What is the best practice to update / edit form fields in php and mysql? -


this may sound typical question, wasn't able find decent answers on stackoverflow. i've built adding , deleting items php form. let's have simple structure this:

database

|car_id(pk)|year  |   make   |   model   |  vin  | -------------------------------------------------- |   1      | 2001 |  toyota  |  corolla  | 12345 | |   2      | 1999 |  ford    |  fiesta   | 67890 | |   3      | 2013 |  gmc     |  yukon    | abcde | 

car class

class car {    public car_id;    public year;    public make;    public model;    public vin; //unique, can 1 in db     function addcar(){...}    function editcar(){...}    function deletecar(){...} } 

php: cars.php?do=edit

this want run functions class , display templates using smarty

viewer edit_cars.html

when page loads, pulls values db , creates forms values in them, user can overwrite values or delete them.

question 1: should check if vin(vehicle identification number, can 1 unique number in database) matches car exists in system? car class or cars.php?

question 2: best way update records?

  1. delete row , insert new one?
  2. update values, if didn't change
  3. check fields different using javascript? (on change check values, if changed set kind of var in php)
  4. check fields different using php? (select old values , match them against form values after submit) use kind of php function update fields changed

question 3:(this more of concern me) after user submits form, should input:

  1. when user leaves values , nothing has changed
  2. when user deletes value completely(such "make")
  3. when user changes letter lower case upper

i know these lot of questions, i'm pretty sure lot of people benefit in future when searching these type of stuff.

thank answers

where should check if vin matches car exists in system? car class or cars.php?

the car class self should know it's own properties , not if there other cars same vin.

the restriction should @ database level unique key constraint. avoid exceptions, need check, outside of car class, if existing car registered within vin, prior inserting new car database.

what best way update records?

deleting original row , inserting new 1 cause have different primary key id car. cause problem foreign keys still reference old id prior update. updated, recommend avoiding method.

object relational mappers (orm's) track changes of objects once have been loaded, means can calculate , optimize sql query when executed.

for needs however, updating values (bar id) defiantly easiest option there negligible benefits of calculating changes (and unnecessary) when performing few queries or without orm.

after user submits form, should input:

all front end validation (that javascript) need replicated in end, server side. avoid sql injection , perform required business logic. i.e number of wheels cannot "bacon"

most of can cover on client side, allowing user know (without new http request) there wrong data have entered. inputs validate , how represent these errors user , depend on context of data being entered.

what very important validate data independently of javascript in php avoid disabling javascript in browser , sql injecting database.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -