php Tutorials
Creating An RSS Feed With PHP And MySQL
Retrieving XML With Curl and SimpleXML
Using curl to Query Remote Servers
MySQL Basics
PHP mySQL Voting Booth
File Download Security
PHP Fundamentals

MySQL Basics

Inserting data into MySQL

INSERT Queries

Insert queries use the MySQL INSERT statement. The INSERT statement follows the syntax of INSERT into <table Name> (<fields to populate>) values ('<values for fields>');. Fields and values are separated with a comma.


INSERT into table1 (field1,field2,field3) values ('value1','value2','value3');

Basic insert statement populates field1, field2, and field3 with value1, value2 and value3 in table1.


INSERT into table1 (field1,field2,field3,field4) values (25,field1+1,'value3',field2*3);

This insert statement demonstrates MySQLs ability to do math on values in an insert statement. Here we set field1 to 25 field2 is 25 field3 is value3 and field4 set to 78. The math on insert statements may only be done on fields set prior to the current field.

UPDATE Queries

Update queries are used to change values for rows already stored in a table. Syntax is UPDATE <table> SET <field>=<value> WHERE <field>=<value>;. The SET portion of the statement is where you put the columns to update and the values to update them, and the WHERE portion determines which rows to update.


UPDATE table1 SET field1='value1', field2+1, field3*2 WHERE field4=10;

This statement updates table1 setting field1 to "value1", field2 is added by one, and field3 is multiplied by two on any row that field4 equals 10.


UPDATE table1,table2 SET table1.field1=table2.field3*2 WHERE table1.field2=table2.field4

Here we threw in a curveball you'll notice we've selected two tables. Also we've noted all fields in the format of table.column. This allows us to reference values in one table to set values in another table.

Background information <<  1 2 3  >> Removing data from MySQL
New Content