I'm trying to use NodeJS with PHP.
I want to use NodeJS to instantly insert information into a MYSQL database without refreshing the page. (With PHP you have to refresh).
Only thing is. I installed NodeJS and RequireJS. But it cant find the mysql.js file which is included with NodeJS. Do you have to set up the path towards it. Or instal it in the same directory in PHP. I dont know whats going on.
I also added a screenshot with the error code i recieve from the console.
The code im using to insert the information towards the database with Javascript.
document.getElementById('addDatabase').onclick = function() {
var dateNow = "<?php echo $date; ?>";
var orderlog = "<?php echo $idorderlogFinal; ?>";
var idorder = "<?php echo $row->idorder; ?>";
var username = "<?php echo $_SESSION["username"]; ?>";
var inputVal = document.getElementById("textDatabase").value;
var mysql = require(['mysql']);
<?php
include('includes/conJava.php')
?>
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = "INSERT INTO picqer_salesorder_notes (idorder_log, idorder, `user`, description, created_at) VALUES (orderlog, idorder, username, inputVal, dateNow)";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
});
}
</script>```
where do you initialise the connection? You define a mysql object, but then you call con.connection. con is a JS object, too, where does it come from? And what is includes/conJava.php for?
Furthermore, ['mysql'] is an array, is that really correct?
well of course, it could be quite difficult to access a db without a connector... But why do you define the connector con (= connection object) in an extra PHP script instead of directly in the JS code? Btw, mysql = ... gets the mysqlmodule, not the connector. Using that module, you can instantiate a connector.
I included the con with a PHP script because i dont want important connection information inside the index page.
Mysql = gets indeed the module. But also that module he can't find. Do you have to install the NodeJS package in the JS folder of your project where your working in?