MySQL INSERT Statement with PHP

MySQL INSERT statement inserts record in MySQL database table. We can execute INSERT statement using PHP to insert new records in the database table. For this let's  create a demo table in MySQL database, Copy following MySQL code in your PhpMyAdmin. It will create a table name demotable with 4 columns: id (Auto increment), name, email and phone number, and inserts some records for the demo. [cc lang="mysql"] CREATE TABLE IF NOT EXISTS `demotable` ( `id` int(11) NOT NULL AUTO_INCREMENT, `iName` varchar(60) NOT NULL, `iEmail` varchar(60) NOT NULL, `iPhoneNumber` varchar(60) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; [/cc]

Insert Record in MySQL Record

PHP code below will insert a new record in the database. Save this file in your testing server and execute it by typing its address on browser. [cc lang="php"] '. mysql_error()); }//If everything looks good, Select our Database mysql_select_db($MySQLDatabase, $MySQLConnection);//MySQL statement to insert a record in database table $query = mysql_query("INSERT INTO demotable (iName, iEmail, iPhoneNumber) VALUES ( 'John Cena', '[email protected]', '8549302392' )");if($query) // Check if insert statement was successful { //Show success message echo 'successfully inserted data in record'; }else{ //Show error message echo 'Mysql Error:
'. mysql_error(); } //Close MySQL connection mysql_close($MySQLConnection); ?> [/cc]
    New question is currently disabled!