PHP 5 MYSQL insert data into table
Insert data into table or database
After creating database and table we can start storing data in table. To insert data into table we will use INSERT INTO statements.
Syntax of insert data into table
INSERT INTO table_name (fieldname1 ,fieldname2, ...) VALUES
(value1,value2, ...)
All the data must be in string form that means data or even variable name must be in double of single quoted.
Note: There is no need to specify the auto increment field (like serial number or id) into the query. Mysql will add itself.
Complete example of insert data into table
<?php $host = 'localhost'; $user = 'root'; $password=""; $db_con = mysql_connect($host, $user, $password); if($db_con) { echo"Connected successfuly
"; } else { echo"fail"; } $b=mysql_select_db("my_db1", $db_con); $query=mysql_query("INSERT INTO student(name, branch) VALUES ('Ram1','mae')"); if($query) { $bg="Data sent successfully"; echo "$bg"; } else { echo"fail"; } mysql_close(); ?>
Output
Data sent successfully
Explanation
In previous section we made three field, but only two field specify because of auto increment field id.
Password field is blank because we do not set the password at the time of creation of database.
If there is no password then it must be quote (double or single) not leave blank.