insert query in mysql with function using php and connect mysql using php, how to create a function in php also
STEP 1. create table in mysql
--
-- Table structure for table `employee`
--
CREATE TABLE IF NOT EXISTS `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`email` text NOT NULL,
`address` varchar(128) NOT NULL,
`slug` varchar(128) NOT NULL,
PRIMARY KEY (`id`),
KEY `slug` (`address`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
THIS QUERY in mysql :
INSERT INTO `erp`.`employee` (`id`, `name`, `email`, `address`, `slug`) VALUES (NULL, 'jack', 'jack@gmail.com', 'london', 'london');
STEP 1. create table in mysql
--
-- Table structure for table `employee`
--
CREATE TABLE IF NOT EXISTS `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`email` text NOT NULL,
`address` varchar(128) NOT NULL,
`slug` varchar(128) NOT NULL,
PRIMARY KEY (`id`),
KEY `slug` (`address`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
THIS QUERY in mysql :
INSERT INTO `erp`.`employee` (`id`, `name`, `email`, `address`, `slug`) VALUES (NULL, 'jack', 'jack@gmail.com', 'london', 'london');
STEP 2. Create a index.php
<html>
<head></head>
<body>
<form action="" method="POST">
Name: <input type="text" name="name"><br> <br>
Email: <input type="text" name="email"> <br><br>
Address:<input type="text" name="address"> <br><br>
Slug: <input type="text" name="slug">
<input type="submit" name="ADD" value="submit">
</form>
</body>
</html>
<?php
include_once('data.php');
$conn=DB_Mysql();
$tableName="employee";
$db="erp";
if($_POST) {
$name=$_POST['name'];
$email=$_POST['email'];
$address=$_POST['address'];
$slug=$_POST['slug'];
InsertData($conn,$tableName,$db,$name,$email,$address,$slug);
}
$retval=getData($conn,$tableName,$db);
if(! $retval ) {
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
echo "<br> No :{$row['id']} ";
echo "Name :{$row['name']} ";
echo "Email :{$row['email']} ";
echo "Address :{$row['address']} ";
echo "Slug :{$row['slug']} <br> ";
}
?>
STEP 3. Create a data.php
<?php
function DB_Mysql() {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
return $conn;
}
function getData($conn,$tableName,$db) {
$sql = "SELECT * FROM $tableName";
mysql_select_db(''.$db.'');
$retval = mysql_query( $sql, $conn );
return $retval;
}
function InsertData($conn,$tableName,$db,$name,$email,$address,$slug) {
$sql = "INSERT INTO `erp`.`".$tableName."` (`id`, `name`, `email`, `address`, `slug`) VALUES (NULL, '".$name."', '".$email."', '".$address."', '".$slug."');";
mysql_select_db(''.$db.'');
$retval = mysql_query( $sql, $conn );
return $retval;
}
?>
No comments:
Post a Comment