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

How to remove whitespace from string using php

Remove All Special Characters From String PHP

comma separated values in mysql query

SQL statement to retrieve names beginning with A

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.