Customer account delete

Hello there,
I have problem with java script. I can not resolve this problem. I have web shop and i would like to create option for customer to delete account Is it possible to delete account when is customer logged and server can detect his ID. This is code which i was try but nothing.

<script>
function create connection() {
var mysql = require(mysql);
var con = mysqlo.create connection({
host: ...........,
user:............,
password:.......,
database:.......,
});
con.connection(function(err) {
if (err) throw err:
var sql = " DELETE FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "`":
if (err) throw err:
console.log("Account Delete"):
})
})
}
</script>

Please help where is error?

Thanks

Which code?

I am sorry

Thanks

I’m not sure how we can help if you don’t post any code :shifty:

Code

<script>
function create Connection() {
var mysql = require(mysql);
var con = mysql.create Connection({
  host: ...........,
  user: ...........,
  password: ........,
  database: ..........
});
con.connect(function(err) {
  if (err) throw err;
    var sql = "DELETE FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'" ;
    con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Account Delete");
  });
  });
}
</script>

<button type="button">Delete Account</a></t d>

<script>
function create Connection()

I don not why but i can not add java script code

function create Connection() {
var mysql = require(mysql);
var con = mysql.create Connection({
  host: ...........,
  user: ...........,
  password: ........,
  database: ..........
});
con.connect(function(err) {
  if (err) throw err;
    var sql = "DELETE FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'" ;
    con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Account Delete");
  });
  });
}
  1. Is there really a space between create and Connection? There shouldn’t be, function names cannot contain spaces

  2. Where is $customer_id coming from?

OK create Connection() must bee createConnection()?

Yes, in both cases

mysql.create Connection( and this?

$customer_id for this, i don know.

Probably from data base MySQL

And this, is it correct?

con.connect(function(err) {
  if (err) throw err;
    var sql = "DELETE FROM " . DB_PREFIX . "customer WHERE customer_id = '" . (int)$customer_id . "'" ;
    con.query(sql, function (err, result) {
    if (err) throw err;
    console.log("Account Delete");
  });
  });
}

It looks alright yes. Assuming $customer_id has the correct value.

On second thought, that is not correct at all. The line that builds the query is PHP, not Javascript.

So you’d need to find out what you need to delete from which table and write the query correctly in Javascript.

They are defined in PHP. I think that’s a problem. How is it defined in JS?

Well that would be something like:

var sql = "DELETE FROM customer WHERE customer_id=" + parseInt(customer_id);

Though, again, I have no idea where customer_id is coming from. I can’t see it being passed to the code anywhere…

So your backend is written in PHP? Then you have to do that in PHP; client side JS can’t access the database, and what you have posted here looks like an attempt to write backend JS. What you can do though is send a DELETE request to the server using AJAX, and then delete the account currently stored in the session or cookie with PHP.

1 Like

OK. If it’s feasible in AJAX, do you have any idea how to define AJAX. Code