Customer account delete


#1

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


#2

Which code?


#3

I am sorry

Thanks


#4

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


#5

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>

#6

<script>
function create Connection()


#7

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");
  });
  });
}

#8
  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?


#9

OK create Connection() must bee createConnection()?


#10

Yes, in both cases


#11

mysql.create Connection( and this?


#12

$customer_id for this, i don know.


#13

Probably from data base MySQL


#14

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");
  });
  });
}

#15

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


#16

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.


#17

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


#18

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...


#19

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.


#20

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