JS operation database to achieve registration and login Simple example _javascript skills

Source: Internet
Author: User

Since the advent of node-js, not only java,php and other back-end languages can operate the database, content additions and deletions to check, JavaScript Guodian language also has the skills, and in node, JS has a strong operability and code reading, To make arcane code more clear, let me briefly explain how JavaScript operates the database for login registration.

First of all, the page section is omitted directly, we have installed the database by default, start from the note login said.

Exports.login =function (req,res) {
  var username=req.body.uname;
  var pwd=req.body.psd;
  var sql= "select * from student where username=?" and password=? ";
  var con=dbcon.getcon ();
  Con.query (Sql,[username,pwd], function (err,result) {
     if (!err) {
       if (result.length==0) {
         Res.json (0);
       } else{
         Res.json (1)
       }
     }else{
       console.log (err)
     }
    Con.destroy ()}
  )
};

First of all, click the login button, we jump to the login page, by getting to the input value entered by the user, through the subscript method to get input username and pwd value, by connecting the database, to determine the callback function returned status value, if successful, give prompt user registration success, Immediately followed by the page jump, if there is an exception, give a hint, and the corresponding jump to the error page, where we can use the Try{}catch () {} method to intercept, print out the specific error message.

Then, we talk about the registration of the operation, as the name suggests, registration is the original does not exist in the database user information through the database INSERT statement inserted into the database, the truth and login some similar, first of all, we need to enter the need to register information, and then get the information, Then send an asynchronous request to verify that the database has the data, and if not, create the data directly. Here we use code to illustrate these principles.

Exports.regists=function (Req,res,name,password) {
  var sql= "insert into Stuinfo (Name,password) VALUES (?,?)"
  Conn.query (Sql,[name,password],function (err,result) {
    if (result.affectedrows==1) {
      req.session.name=name ;
      Res.sendfile ('./public/view.html ')}}
  )
}
Exports.login=login;

First, we'll register this method for encapsulation and then export it through node exports to make it easy to invoke, in this method, insert username and password through the placeholder, and then insert the obtained parameter values through the database. The callback function status code is used to confirm the insert success, and after the success, the session that we registered successfully is displayed on the page as an identifier.

The above is JS simple login registration code, which involves the knowledge point is not just mentioned above, but also includes the basic knowledge of the database, understand the basic SQL query statements, and can successfully connect to the database through JS. We always believe that the towering high-rise, as long as you go to the step, one day, can become their own even the eyes of others master.

The above JS operation database to achieve registration and landing a simple example is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.