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.

<html>
 <head>
  <title>Add New Record in MySQL Database Using PHP</title>
 </head>
 <body>
  <?php
   if(isset($_POST['add']))
   {
    $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";
    mysql_close($conn);
   }
 ?>
<form method="post" action="<?php $_PHP_SELF ?>">
 <table width="400" border="0" cellspacing="1" cellpadding="2">
  <tr>
   <td width="100">Employee Name</td>
   <td><input name="emp_name" type="text" id="emp_name"></td>
  </tr>
  <tr>
   <td width="100">Employee Address</td>
   <td><input name="emp_address" type="text" id="emp_address"></td>
  </tr>
  <tr>
   <td width="100">Employee Salary</td>
   <td><input name="emp_salary" type="text" id="emp_salary"></td>
  </tr>
  <tr>
   <td width="100"> </td>
   <td> </td>
  </tr>
  <tr>
   <td width="100"> </td>
   <td>
   <input name="add" type="submit" id="add" value="Add Employee">
   </td>
  </tr>
 </table>
</form>
</body>
</html>

Leave a Reply

Related Posts: You may like

Extract Total Number of Image And Missing Alt Attribute using php

How to extract Heading tags using PHP from string

How to remove whitespace from string using php

Remove All Special Characters From String PHP

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

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.