1

我正在尝试更新数据库,这是我的代码

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();

我要做的是更新来自已提交表单的客户的余额。$s->execute();如果我尝试回显该值,我可以一直获取代码中的值,$newBalance也就是说,在执行该行后它不会显示,页面变为空白。执行语句中发生了一些事情。$s->execute()它不允许我的代码继续。任何的想法?我是否以错误的方式使用 PDO 类。它没有到达“catch”声明。任何帮助都会很棒。最终结果是页面返回到它开始更新余额的位置。

4

2 回答 2

1

您只是将值绑定到余额,而不是 id,您需要如下行:

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

在查询中使用它们之前,最好确保$_POST['balance']$_POST['oldbalance']已设置好:

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

如果您没有收到错误,则可能没有错误报告,您可以通过添加error_reporting(E_ALL);到页面顶部来启用它。

于 2012-10-31T03:47:27.070 回答
0

var_dump()两个变量$_POST['balance']& $_POST['oldbalance']。我相信他们会来的string。类型转换是解决方案之一。在这种情况下尝试类型转换以对 int/float 执行加法。

$newBalance = (int)$_POST['balance'] + (int)$_POST['oldbalance'];
于 2012-10-31T03:46:24.687 回答