1

I am trying to update a database, here is my code

if (isset($_GET['placeorder']))
{
    include 'includes/db.php';

    try
    {
    $newBalance = $_POST['balance'] + $_POST['oldbalance'];
    $sql = 'UPDATE customer SET
            balance = :balance
            WHERE id = :id';
    $s = $pdo->prepare($sql);
    $s->bindValue(':balance', $newBalance);
    $s->execute();
    }
    catch (PDOException $e)
    {
        $error = 'Could not add the new balance for the customer' . $e->getMessage();
        include  'result.php';
        exit(); 

    }
    header('Location: .');
    exit();

What I am trying to do is update the balance for a customer that is coming from a form that was submitted. I am able to get the value in the code all the way up to where I get to $s->execute(); if I try to echo the value, which is $newBalance, it will not show after that line is executed, the page goes blank. Something is happening in the execute statement. $s->execute() that it does not allow my code to proceed. Any idea? Am I using the PDO class the wrong way. It is not getting to the "catch" statement. Any help would be great. The end result is that the page returns to where it started with the updated balance.

1
  • Are you sure you've got error reporting on? Put error_reporting(E_ALL); at the top of your code Commented Oct 31, 2012 at 3:48

2 Answers 2

1

You are only binding a value to the balance, not the id, you need a line like:

$s->bindValue(':id', $id);

It would be a good idea to make sure that $_POST['balance'] and $_POST['oldbalance'] are set as well before using them in your query:

$balance = isset($_POST['balance'])? $_POST['balance'] : 0;
$oldbalance = isset($_POST['oldbalance'])? $_POST['oldbalance'] : 0;
$newbalance = $balance + $oldbalance;

If you aren't getting an error, you likely don't have error reporting on, you can enable it by adding error_reporting(E_ALL); to the top of your page.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks doublesharp, that was exactly my issue. I've been looking at this thing far too long. Thans for the help. I do have error reporting turned on, but not binding "id" was causing the issue. Thanks again.
0

var_dump() both variables $_POST['balance'] & $_POST['oldbalance']. I am sure they are coming as string. Type casting is one of the solution. Try typecasting in this case to perform addition on int/float.

$newBalance = (int)$_POST['balance'] + (int)$_POST['oldbalance'];

1 Comment

Thanks, swampnesh, the issue was that I was not binding the id value. Thanks for taking the time.

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.