how to insert data into mysql database using php

how to insert data into mysql database using php

Data can be entered into MySQL tables by executing SQL INSERT statement using PHP function mysql_query. Here We Explain simle example to insert a record into employee table.

  <title>Add New Record in MySQL Database Using PHP</title>
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = '';
    $dbname = 'Database Name';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
    mysql_select_db($dbname ) or die(mysql_error());

    $emp_name = $_POST['emp_name'];
    $emp_address = $_POST['emp_address'];
    $emp_salary = $_POST['emp_salary'];

    $sql = "INSERT INTO employee (emp_name, emp_address, emp_salary, join_date) VALUES('$emp_name', '$emp_address', $emp_salary, NOW())";

    $retval = mysql_query( $sql) or die(mysql_error());
    echo "data successfully Inserted \n";
<form method="post" action="<?php $_PHP_SELF ?>">
 <table width="400" border="0" cellspacing="1" cellpadding="2">
   <td width="100">Employee Name</td>
   <td><input name="emp_name" type="text" id="emp_name"></td>
   <td width="100">Employee Address</td>
   <td><input name="emp_address" type="text" id="emp_address"></td>
   <td width="100">Employee Salary</td>
   <td><input name="emp_salary" type="text" id="emp_salary"></td>
   <td width="100"> </td>
   <td> </td>
   <td width="100"> </td>
   <input name="add" type="submit" id="add" value="Add Employee">
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.