In this tutorial you will learn about the MySQL Insert Query and its application with practical example.
To insert data into MySQL table you would need to use INSERT INTO SQL
Syntax:
1 2 3 |
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN ); |
To insert string data types it is required to keep all the values into double or single quote, for example:- “value”.
Inserting Data Using PHP:
Inserting a row in “emp_info” table:
1 2 3 4 5 6 7 8 9 |
mysql_query("INSERT INTO `emp_info` ( `empID` , `empName` , `empCode` , `empSalary` , `empDept` ) VALUES ( NULL , 'christine', '123', '25000', 'EDP'"); |
Inserting Form Data Using PHP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php //After having connection esatablished and selecting the DB if(isset($_POST["submit"]) && $_POST["submit"]!=""){ $name=$_POST["name"]; $code=$_POST["code"]; $salary=$_POST["salary"]; $dept=$_POST["dept"]; //INSERT QUERY if(mysql_query("INSERT INTO `emp_info` ( `empName` , `empCode` , `empSalary` , `empDept` ) VALUES ( $name, $code, $salary,$dept")){ echo "Data inserted"; } } ?> <form action="" method="post"> <table width="365" border="0" cellpadding="3" cellspacing="1" bgcolor="#E4E4E4" style="display:inline;"> <tr bgcolor="#F8F8F8"> <td width="77" align="right">Employee Name: </td> <td width="273"><input type="text" name="name" id="name" ></td> </tr> <tr bgcolor="#F8F8F8"> <td align="right">Employee Code: </td> <td><input type="text" name="code" id="code" ></td> </tr> <tr bgcolor="#F8F8F8"> <td align="right">Emmployee Salary:</td> <td><input type="text" name="salary" id="salary" ></td> </tr> <tr bgcolor="#F8F8F8"> <td align="right">Emmployee Department:</td> <td><input type="text" name="dept" id="dept" ></td> </tr> <tr bgcolor="#DBDBDB"> <td colspan="2" align="center"><input name="submit" type="submit" id="submit" value="Submit" ></td> </tr> </table> </form> |