1

So just converting old sql statements into prepared statements

So how do I add to a value already stored in a database?

in old terms I did this:

$sql = "UPDATE `table` SET `PT` = PT+$PaymentGross ... ";

but how do you accomplish same in prepared statements?

like so:

$sql = "UPDATE `table` SET `PT` = PT+? ...  ";

or like so:

$stmt->bind_param("i",PT+$PaymentGross ... );

couldn't find any info, or perhaps couldn't punch right keywords into google

3
  • 1
    So what's the problem with writing the code and test it? Commented Jul 29, 2017 at 10:54
  • 1
    So what really is your problem? Please provide more detail so that we can help. Commented Jul 29, 2017 at 11:03
  • Sorry I tried the statement before it wasn't working. I have just notice that the mistake was elsewhere on the statement line. I assumed I had typed UPDATE table SET PT = PT+? in wrong. My bad, silly mistake Commented Jul 29, 2017 at 11:06

1 Answer 1

2

You can try this way

$sql = "UPDATE `table` SET `PT`=SUM(`PT`+?) WHERE id=?";

    $stmt = $db->prepare($sql);

    // This assumes the PT is int `d` and id is int `d`


    $stmt->bind_param('dd', $PaymentGross, $id);
    $stmt->execute();

    if ($stmt->errno) {
      echo "FAILURE!!! " . $stmt->error;
    }
    else echo "Updated {$stmt->affected_rows} rows";

    $stmt->close();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.