PHP/MySQL – correct use of INSERT query

For all PHP/MySQL developers, there are two ways of using INSERT query:

1. INSERT INTO [table] VALUES (‘value-1′,’value-2’);

2. INSERT INTO [table] (‘column-1′,’column-2’) VALUES (‘value-1′,’value-2’);

Always use INSERT variant 2, because in case you need to add more columns to the database, you won’t need to change the PHP code for the query to work.

The unspecified columns will take default value, defined when appending the new column.


Posted

in

by

Comments

7 responses to “PHP/MySQL – correct use of INSERT query”

  1. anonim Avatar
    anonim

    Thanks for this, I had always use the first one.

  2. anonim Avatar
    anonim

    Really good for beginners that make huge mistakes in their php work.

  3. Andreea Avatar

    Mersi ca ai partajat asta.

    Nu sunt atat de tehnica, insa faptul ca exista un blog care sa lamureasca si astfel de probleme e imbucurator.

    Mersi.

  4. curs valutar Avatar
    curs valutar

    Mai e si a 3-a varianta, mai simpla care are sintaxa similara cu update-ul.

    3. INSERT INTO tabel SET camp1=’valoare1′, camp2=’valoare2′;

  5. Cristi Avatar
    Cristi

    In php inca se mai lucreaza cu sql query? Nu exista un framework care sa separe query-ul din cod? Hibernate, proceduri stocate ceva?

  6. butcher Avatar

    normal ca exista..tone de frameworkuri, cakephp, codeigniter, symphony, etc care separa sql-ul de cod…mysql 5 suporta si proceduri stocate, triggeri, trazanctii tot ce vrei 🙂

  7. web design constanta Avatar
    web design constanta

    Trebuie sa incerc sa invat si eu php/mysql