Hướng dẫn how do you insert data to database using javascript with html? - làm thế nào để bạn chèn dữ liệu vào cơ sở dữ liệu bằng cách sử dụng javascript với html?

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()" />
  
  •       
  • & 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 do you insert data to database using javascript with html? - làm thế nào để bạn chèn dữ liệu vào cơ sở dữ liệu bằng cách sử dụng javascript với html?

    Hướng dẫn how do you insert data to database using javascript with html? - làm thế nào để bạn chèn dữ liệu vào cơ sở dữ liệu bằng cách sử dụng javascript với html?

    Đầ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 do you insert data to database using javascript with html? - làm thế nào để bạn chèn dữ liệu vào cơ sở dữ liệu bằng cách sử dụng javascript với html?

    Đầ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 do you insert data to database using javascript with html? - làm thế nào để bạn chèn dữ liệu vào cơ sở dữ liệu bằng cách sử dụng javascript với html?

    Đầ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 do you insert data to database using javascript with html? - làm thế nào để bạn chèn dữ liệu vào cơ sở dữ liệu bằng cách sử dụng javascript với html?

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

    Làm thế nào có thể chèn dữ liệu vào cơ sở dữ liệu trong HTML?

    Đối với điều này, bạn cần làm theo các bước sau:..
    Bước 1: Lọc các yêu cầu biểu mẫu HTML của bạn cho trang web Liên hệ với chúng tôi. ....
    Bước 2: Tạo cơ sở dữ liệu và bảng trong MySQL. ....
    Bước 3: Tạo biểu mẫu HTML để kết nối với cơ sở dữ liệu. ....
    Bước 4: Tạo trang PHP để lưu dữ liệu từ biểu mẫu HTML vào cơ sở dữ liệu MySQL của bạn. ....
    Bước 5: Tất cả đã hoàn thành !.

    Làm thế nào có thể thêm dữ liệu trong cơ sở dữ liệu bằng JavaScript?

    Tạo một tệp JS có tên "Chèn" trong thư mục DBEXample và đặt dữ liệu sau vào đó:..
    var mysql = yêu cầu ('mysql') ;.
    var con = mysql.createdConnection ({.
    Chủ nhà: "Localhost",.
    Người dùng: "Root",.
    Mật khẩu: "12345",.
    Cơ sở dữ liệu: "Javatpoint".
    con.Kết nối (hàm (err) {.

    Chúng ta có thể lưu trữ dữ liệu trong cơ sở dữ liệu bằng JavaScript không?

    JavaScript cho phép chúng tôi lưu trữ dữ liệu trong trình duyệt bằng API lưu trữ cục bộ.Tại đây, bạn có thể sử dụng LocalStorage và SessionStorage.Các đối tượng cho phép chúng tôi lưu trữ dữ liệu (trong các cặp khóa/giá trị) và cập nhật nó từ lưu trữ của trình duyệt.Để xem dữ liệu, hãy mở trình duyệt của bạn.. 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.

    JavaScript có thể hoạt động với cơ sở dữ liệu không?

    JavaScript là ngôn ngữ phía máy khách chạy trong trình duyệt và MySQL là công nghệ phía máy chủ chạy trên máy chủ.Bạn cần sử dụng Node.js ngôn ngữ phía máy chủ để kết nối với cơ sở dữ liệu MySQL.You need to use the server-side language Node. js to connect to the MySQL Database.