Roubie
Roubie

Reputation: 299

update mysql database with jdbc

I have an error updating my database because of variables. This is my code:

UPDATE `payment` SET `paid`=1 AND `amoun`=$amountpaid WHERE `paid`=0 AND `userid`=$uid

$amountpaid is the amount of the bill that the user paid and $uid is user id. It seems like using $ in front of variable names is forbidden. How can I use variables in SQL?

Upvotes: 0

Views: 3583

Answers (2)

Roubie
Roubie

Reputation: 299

I got the solution by using String.

I converted the ArrayList to a String and then sent the data as string. The data got updated but I don't know what will happen next if I want to view the data in the client tier...

Upvotes: 0

WhiteFang34
WhiteFang34

Reputation: 72039

Where are your variables coming from? You probably want something like this if you're using JDBC:

int setPaid = 1;
int amountPaid = x; // put $amountpaid here
int wherePaid = 0;
int userId = y; // put $uid here

String updateQuery = "UPDATE payment SET paid = ?, amoun = ?"
        + " WHERE paid = ? AND userid = ?";
PreparedStatement ps = con.prepareStatement(updateQuery);
ps.setInt(1, setPaid);
ps.setInt(2, amountPaid);
ps.setInt(3, wherePaid);
ps.setInt(4, userId);
ps.executeUpdate();

Upvotes: 2

Related Questions