PHP Programing language

PHP MySQL Insert Data
Previous Home Next

In MySQL table is created after that inserting data in the table , When the data is put into a MySQL table it is referred to as inserting data. The inserting data it is important to the exact names and types of the table's columns. If you try to place a 500 word essay into a column that only accepts integers of size three, you will end up with a nasty error!

How can Insert Data into your Table

Now that you have created your table, let's put some data into that puppy, Here is the PHP/MySQL code for inserting data into the "tablefirst" table. This table was created in MySQL database.

PHP & MySQL Code:

// Make a MySQL Connection

mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());

// Insert a row of information into the table "example"

mysql_query ("INSERT INTO example (name, age) VALUES ('Aniket', '23' ) ") 
or die (mysql_error());  

mysql_query("INSERT INTO example (name, age) VALUES ('Abhas', '21' ) ") 
or die(mysql_error());  

mysql_query("INSERT INTO example (name, age) VALUES ('Ashoka', '15' ) ") 
or die(mysql_error());  

echo "Data Inserted!";


Data Inserted!

In this MySQL database the code is much simpler to understand than the create table code, and it will be most of the MySQL queries you will learn in the rest of this tutorial. Once again, we will cover the code line by line.

mysql_query("INSERT INTO example

Again we are using the mysql_query function. "INSERT INTO" means that data is going to be put into a table. The name of the table we specified to insert data into was "tablefirst".

(name, age) VALUES('Aniket', '23' ) ")'

"(name, age)" are the two columns we want to add data into. "VALUES" means that what follows is the data to be put into the columns that we just specified. Here we enter the name Aniket for "name", and 23 for "age".

Be sure to note the location and number of apostrophes and parentheses in the PHP code, as this is where a lot of beginner PHP/MySQL programmers run into problems.

Previous Home Next