Hướng dẫn how can add data in database using javascript? - làm thế nào có thể thêm dữ liệu trong cơ sở dữ liệu bằng cách sử dụng javascript?

Giới thiệu

Trong bài viết này, tôi sẽ giải thích cách chèn các bản ghi vào cơ sở dữ liệu bằng cách sử dụng TextBox trong JavaScript.

Lưu ý: Chương trình này sẽ chỉ hoạt động với Internet Explorer.This program will work only with Internet Explorer.

Đầu tiên tôi đã tạo một cơ sở dữ liệu empdetail. & Nbsp; sau đó tôi đã tạo một bảng trong cơ sở dữ liệu này. & Nbsp;

Mã truy vấn

  1. CREATE TABLE [dbo].[Emp_Info](  
  2.       [Emp_Id] [int] NULL,  int] NULL,  
  3.       [Name] [varchar](50) NULL,  
  4.       [Salary] [int] NULL,  int] NULL,  
  5.       [City] [varchar](50) NULL  
  6. ) & nbsp; on & nbsp; [Chính] & nbsp;

Bây giờ chèn một số dữ liệu vào bảng EMP_INFO.Sau đó sử dụng quy trình sau.

Hoàn thành chương trình

Insert_Value.htm

  1.   "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.   "http://www.w3.org/1999/xhtml">  
  3.   
  4.       
  5.       "text/javascript" >  
  6.         function InsertRecord()  function InsertRecord()  
  7.         {  
  8.             var txtid = document.getElementById('txtid').value;  var txtid = document.getElementById('txtid').value;  
  9.             var txtname = document.getElementById('txtname').value;  var txtname = document.getElementById('txtname').value;  
  10.             var txtsalary = document.getElementById('txtsalary').value;  var txtsalary = document.getElementById('txtsalary').value;  
  11.             var txtcity = document.getElementById('txtcity').value;  var txtcity = document.getElementById('txtcity').value;  
  12. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;| & nbsp; txtsalary.length & nbsp;! = 0 || & nbsp; txtcity.length & nbsp;! = 0)if (txtid.length != 0 || txtname.length !=0 || txtsalary.length !=0|| txtcity.length !=0)  
  13.             {  
  14.                 var connection = new ActiveXObject("ADODB.Connection");  var connection = new ActiveXObject("ADODB.Connection");  
  15.                 var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****;Provider=SQLOLEDB";  var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****;Provider=SQLOLEDB";  
  16.                 connection.Open(connectionstring);  
  17.                 var rs = new ActiveXObject("ADODB.Recordset");  var rs = new ActiveXObject("ADODB.Recordset");  
  18.                 rs.Open("insert into Emp_Info values('" + txtid + "','" + txtname + "','" + txtsalary + "','" + txtcity + "')", connection);  "insert into Emp_Info values('" + txtid + "','" + txtname + "','" + txtsalary + "','" + txtcity + "')", connection);  
  19.                 alert("Insert Record Successfuly");  "Insert Record Successfuly");  
  20.                 txtid.value = " ";  " ";  
  21.                 connection.close();  
  22.             }  
  23.             else  else  
  24.             {              
  25. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;; \ n & nbsp; Mức lương & nbsp;"Please Enter Employee \n Id \n Name \n Salary \n City ");  
  26.             }  
  27.         }  
  28.         function ShowAll()  function ShowAll()  
  29.         {  
  30.                 var connection = new ActiveXObject("ADODB.Connection");  var connection = new ActiveXObject("ADODB.Connection");  
  31.                 var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****;Provider=SQLOLEDB";  var connectionstring = "Data Source=.;Initial Catalog=EmpDetail;Persist Security Info=True;User ID=sa;Password=****;Provider=SQLOLEDB";  
  32.                 connection.Open(connectionstring);  
  33.                 var rs = new ActiveXObject("ADODB.Recordset");  var rs = new ActiveXObject("ADODB.Recordset");  
  34.                 rs.Open("select * from Emp_Info ", connection);  "select * from Emp_Info ", connection);  
  35.                 rs.MoveFirst();  
  36.                 var span = document.createElement("span");  var span = document.createElement("span");  
  37.                 span.style.color = "Blue";  "Blue";  
  38.                 span.innerText = "  ID " + "  Name " + "  Salary" + " City ";  "  ID " + "  Name " + "  Salary" + " City ";  
  39.                 document.body.appendChild(span);  
  40.                 while (!rs.eof)  while (!rs.eof)  
  41.                 {  
  42.                     var span = document.createElement("span");  var span = document.createElement("span");  
  43.                     span.style.color = "green";  "green";  
  44. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;; "& nbsp;+& nbsp; rs.fields (0) & nbsp;+& nbsp;" & nbsp;; "& nbsp;+& nbsp; rs.fields (2) & nbsp;+& nbsp;" & nbsp;"\n " + rs.fields(0) + " |  " + rs.fields(1) + " |  " + rs.fields(2) + " |  " + rs.fields(3);  
  45.                     document.body.appendChild(span);  
  46.                     rs.MoveNext();  
  47.                 }  
  48.                 rs.close();  
  49.                 connection.close();  
  50.             }   
  51.       
  52.       "text/css">  
  53.         #main    
  54.         {  
  55.             height: 264px;  
  56.         }  
  57.         #ShowRecord    
  58.         {  
  59.             width: 67px;  
  60.             z-index: 1;  
  61.             left: 20px;  
  62.             top: 257px;  
  63.             position: absolute;  
  64.         }  
  65.         #showall    
  66.         {  
  67.             z-index: 1;  
  68.             left: 114px;  
  69.             top: 257px;  
  70.             position: absolute;  
  71.         }  
  72.       
  73.   
  74.   "height: 431px">  
  75.     "show"  
  76.         style="font-size: x-large; font-weight: bold; height: 298px; color: #009999;">  "font-size: x-large; font-weight: bold; height: 298px; color: #009999;">  
  77.        Insert Employee Record  "font-size: medium; color: #000000;">  
  78.      Employee Id    
  79.       "txtid" type="text" />

      
  80.           "font-size: medium; color: #000000;">  
  81.             Name               
  82.               "txtname" type="text" />

      
  83.           "font-size: medium; color: #000000;">  
  84.             Salary              
  85.               "txtsalary" type="text" />

      
  86.           "font-size: medium; color: #000000;">  
  87.             City                  
  88.               "txtcity" type="text" />

      
  89.        "ShowRecord" type="button" value="Insert" onclick="InsertRecord()" />   
  90.       "showall" type="button" value="Show All Record" onclick="ShowAll()" />  
  91.       
  92. & nbsp;

Đầu ra 1

Nhấp vào nút "Hiển thị tất cả các bản ghi".

 

Hướng dẫn how can add data in database using javascript? - làm thế nào có thể thêm dữ liệu trong cơ sở dữ liệu bằng cách sử dụng javascript?

Hướng dẫn how can add data in database using javascript? - làm thế nào có thể thêm dữ liệu trong cơ sở dữ liệu bằng cách sử dụng javascript?

Đầu ra 2

Chèn bản ghi vào hộp văn bản sau đó nhấp vào nút "Chèn".

Hướng dẫn how can add data in database using javascript? - làm thế nào có thể thêm dữ liệu trong cơ sở dữ liệu bằng cách sử dụng javascript?

Đầu ra 3

Sau khi chèn một bản ghi, nhấp vào nút "Hiển thị tất cả các bản ghi". Bạn sẽ thấy hồ sơ được chèn thành công.

& nbsp;

Hướng dẫn how can add data in database using javascript? - làm thế nào có thể thêm dữ liệu trong cơ sở dữ liệu bằng cách sử dụng javascript?

Đầu ra 4

Nếu bạn nhấp vào nút Chèn mà không cần nhập bất kỳ giá trị nào vào hộp văn bản thì nó sẽ hiển thị lỗi.

& nbsp;

Hướng dẫn how can add data in database using javascript? - làm thế nào có thể thêm dữ liệu trong cơ sở dữ liệu bằng cách sử dụng javascript?

Để biết thêm thông tin, tải xuống ứng dụng mẫu đính kèm.

Xác định truy vấn SQL. ....

Xác định truy vấn SQL. .....
Tạo lược đồ XML. ....
Liên kết đến cơ sở dữ liệu ..
Tạo tệp JS có tên "Chèn" trong thư mục DBEXample và đặt dữ liệu sau vào đó: var mysql = abel ('mysql'); var con = mysql. createdConnection ({.
var mysql = yêu cầu ('mysql') ;.
var con = mysql. ....
Chủ nhà: "Localhost",.

Người dùng: "Root",.

Xác định truy vấn SQL.....Use INSERT Statement to add multiple rows in the table. INSERT INTO SELECT clause to insert the output generated by the SELECT query. INSERT IGNORE clause to ignore the error generated during the execution of the query.

Tạo lược đồ XML.....

Liên kết đến cơ sở dữ liệu ... Here, you can use LocalStorage and SessionStorage . The objects let us store data (in key/value pairs) and update it from the browser's storage. To view the data, open your browser.

Xác định truy vấn SQL.....

Tạo lược đồ XML......
Xác định truy vấn.Chúng tôi sẽ xác định hai truy vấn.....
Tạo lược đồ XML.....
Tạo biểu mẫu.....
Liên kết đến cơ sở dữ liệu.....
Xác định truy vấn SQL.....
Tạo lược đồ XML.....
Tạo biểu mẫu.....
Liên kết đến cơ sở dữ liệu.....