Data को एक MySQL Table में Insert करने के लिए आपको SQL INSERT INTO Command का उपयोग करना होगा. आप mysql> Prompt या PHP जैसे किसी भी Script का उपयोग करके Data को MySQL Table में INSERT कर सकते है.
MySQL Table मे Data को INSERT करने के लिए यहां पर आप एक सामान्य SQL Syntax देख सकते है.
INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );
String Data Types को Insert करने के लिए सभी Values को Single या Double Quotes में रखना आवश्यक होता है.
Command Prompt से Data को Insert करने के लिए हम SQL INSERT मे Command का उपयोग MySQL Table tutorials_tbl मे Data को Insert के लिए करेंगे.
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> INSERT INTO tutorials_tbl
->(tutorial_title, tutorial_author, submission_date)
->VALUES
->("Learn PHP", "Ali Ammu", NOW());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO tutorials_tbl
->(tutorial_title, tutorial_author, submission_date)
->VALUES
->("Learn MySQL", "Ali S", NOW());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO tutorials_tbl
->(tutorial_title, tutorial_author, submission_date)
->VALUES
->("JAVA Tutorial", "Ammu", '2007-05-06');
Query OK, 1 row affected (0.01 sec)
mysql>
आप MySQL Table मे Data को Insert करने के लिए PHP function mysql_query () मे SQL INSERT का उपयोग कर सकते है.
<!DOCTYPE html>
<html>
<head>
<title>Add New Record in MySQL Database Example</title>
</head>
<body>
<?php
if(isset($_POST['add'])) {
$dbhost = 'localhost:3036';
$dbuser = 'Troot';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc() ) {
$tutorial_title = addslashes ($_POST['tutorial_title']);
$tutorial_author = addslashes ($_POST['tutorial_author']);
} else {
$tutorial_title = $_POST['tutorial_title'];
$tutorial_author = $_POST['tutorial_author'];
}
$submission_date = $_POST['submission_date'];
$sql = "INSERT INTO tutorials_tbl ".
"(tutorial_title,tutorial_author, submission_date) "."VALUES ".
"('$tutorial_title','$tutorial_author','$submission_date')";
mysql_select_db('TUTORIALS');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
} else {
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<table width = "600" border = "0" cellspacing = "1" cellpadding = "2">
<tr>
<td width = "250">Tutorial Title</td>
<td>
<input name = "tutorial_title" type = "text" id = "tutorial_title">
</td>
</tr>
<tr>
<td width = "250">Tutorial Author</td>
<td>
<input name = "tutorial_author" type = "text" id = "tutorial_author">
</td>
</tr>
<tr>
<td width = "250">Submission Date [ yyyy-mm-dd ]</td>
<td>
<input name = "submission_date" type = "text" id = "submission_date">
</td>
</tr>
<tr>
<td width = "250"> </td>
<td> </td>
</tr>
<tr>
<td width = "250"> </td>
<td>
<input name = "add" type = "submit" id = "add" value = "Add Tutorial">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
Data को Insert करते समय यह जांचने के लिए कि function get_magic_quotes_gpc () का उपयोग करना सबसे अच्छा है यह देखने के लिए कि क्या Magic Quote के लिए मौजूदा Configuration Set है या नहीं.
यदि यह Function Returns Value False देता है, तो Quotes से पहले Slashes को Add करने के लिए Function Addslashes () का उपयोग करे.