Inserting MySQL Table records using Php

From DeveloperWiki

Jump to: navigation, search

This example demonstrates inserting table records to a table using Php.

Requirements

  • Configured Php-MySQL-Apache/IIS environment
  • Basic Php,MySQL Knowledge

Step 1: Create MySQL Table "customers"

create table customers(
c_id int not null primary key,
c_first_name varchar(128) not null,
c_last_name varchar(128) not null,
c_email varchar(128) null,
c_telephone varchar(32) null
)

Step 2: Create Php Script to Connect to mySQL Server

<?Php
$user_name = "root";
$password = "s3cr3t";
$database = "customerdb";
$server = "localhost";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {
$query = "INSERT INTO customers (c_first_name, c_last_name, c_email,c_telephone) 
VALUES ('Foo', 'Bar', 'user@domain.com','000-00000000')";
$result = mysql_query($query);
mysql_close($db_handle);
print "Records added to the database";
}
else {
print "Database NOT Found";
mysql_close($db_handle);
}
?>

[edit] Contributors

124.43.54.10, Randika, 124.43.32.199, 124.43.48.167