-3

I am trying to make a registration system. When I ran the code on Google chrome, I got an error:

require() is not defined

How can I solve this problem?

//TODO:Register

var mysql=require('mysql');

var con=mysql.createConnection({
    host:"localhost",
    user:"root",
    password:"",
    database:"progmeet_database"

});

function Register(){
    //Value Takers
    var name_input_value=document.getElementById("Progmeet_register_page_name_and_surname_input").value;
    var username_input_value=document.getElementById("Progmeet_register_page_username_input").value;
    var Phonenumber_input_value=document.getElementById("Progmeet_register_page_phone_number_input").value;
    var DOB_input_value=document.getElementById("Progmeet_register_page_DOB_input").value;
    var password_input_value=document.getElementById("Progmeet_register_page_password_input").value;
    var email_input_value=document.getElementById("Progmeet_register_page_email_input").value;

    //Database appender
    var Insert_into_value="INSERT INTO progmeet_user_info_table (name,username,Phonenumber,DateOfBirth,Password,Email),VALUES('namex','usernamex','Phonenumberx','DOBx','passwordx','emailx')";
    Insert_into_value.replace("namex",name_input_value);
    Insert_into_value.replace("usernamex",username_input_value);
    Insert_into_value.replace("Phonenumberx",Phonenumber_input_value);
    Insert_into_value.replace("DOBx",DOB_input_value);
    Insert_into_value.replace("passwordx",password_input_value);
    Insert_into_value.replace("emailx",email_input_value);
    con.query(Insert_into_value,function(err,result){
        if(err) throw err;
        console.log("suscess!");
    });
    alert("cihantoker")
};

con.connect(console.log("connected!"));
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I shared the code as an answer pls check it – cihan toker Jun 21 '22 at 19:07
  • 2
    [edit] your question with the code (don't post it as an answer) – Icepickle Jun 21 '22 at 19:07
  • I did it with edit. – cihan toker Jun 21 '22 at 19:09
  • 1
    Chrome is not going to run node js code. You are running code meant for a server, not clientside JavaScript. – epascarello Jun 21 '22 at 19:11
  • 1
    You can't connect to a mysql database from the browser. And even if you could it would be a really bad idea to expose the username and password to your database to the public. – t.niese Jun 21 '22 at 19:11
  • This answer might solve your question https://stackoverflow.com/questions/57851947/cant-call-mysql-code-in-chrome-browser-js-file – Dhruv Jun 21 '22 at 19:15
  • Is that mean I should create a node server to run MySQL on the web? – cihan toker Jun 21 '22 at 19:24
  • There are many things you could run as your server, `node` is one of them. PHP is another. In your `function Register` after collecting the variables you will want to send them to the server to be processed (a form `POST`, a call using `fetch(...)` etc). Variables should be declared with `let` or `const`, not `var` anymore. You probably want the password entered twice (to detect mistyping). Don't just _store_ the password, you need to [hash and salt](https://crackstation.net/hashing-security.htm) it first. – Stephen P Jun 21 '22 at 19:31
  • @cihantoker You don't "run MySQL on the web". You run a web server (ie Node.js, PHP, Ruby on Rails, .Net, etc), which serves up content. That web server may contact a MySQL database server to dynamically build its responses; this may be running on the same machine (but different port) or a different machine. But in any case, the MySQL server is never exposed directly to the Internet, rather the browser talks to the server, and the server talks to the database. The web server is also where your database credentials are used to connect to the database. – user229044 Jun 22 '22 at 00:13
  • 5
    Please do not vandalize your question. This is not fair to the site members, especially @user229044 who took the time and energy to answer the question. It is also against stop rules, will be reversed any time that you do it, and only creates more busy work for those of us who actively moderate the site. – Hovercraft Full Of Eels Aug 18 '23 at 14:52

1 Answers1

2

When I ran the code on Google chrome

This code will not run in a web browser. You need to run it on a server via Node. Your browser cannot process require statements, these are part of Node.js.

You can use a pre-processor like Webpack to make require statements work in code that will ultimately run in the browser, but the code you've provided attempts to connect to a MySQL database on localhost. This cannot work in a browser, the code must necessarily run on a server, regardless of whether the require statement can be made to work.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • Yes, anything to do with a database must be served from the server. However, that server may be your very own computer ;) As it is with PHP and MYSQL using XAMPP. – Lexus de Vinco Jun 21 '22 at 19:37
  • 1
    @AlexDev I'm not sure what your point is. Yes, your computer is a server in this context. It is serving the document. The *browser* cannot connect to a database on `localhost`, even if you're running the browser on the same machine as the database. – user229044 Jun 22 '22 at 00:11
  • Exactly my point! You need a connection to the database through PHP or one of the other back end languages/frameworks. Some beginners haven't quite grasped back end VS front end technicalities. Just pointing it out for those people. And agreeing with your post! ;-) – Lexus de Vinco Jun 26 '22 at 22:45