0

xampp 安装中的 PHP 5.4.7,MySQL 5.5.27 - 我整天都在努力让各种 UPDATE 语句工作,但无济于事。已阅读并尝试了http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers中的示例,并放弃了一次更新(找到了替代解决方案),但我想知道下面的这个有什么问题。顺便说一句,我所有其他 PDO FETCHALL 查询都可以正常工作...

function updateThisMemberInterests($PDOdbObject, $memberId, $interests)
{
try
{
    $intId = 0;
    $upInt = $connectionObject->prepare("UPDATE `member_interest` SET (`interest_id`) VALUES (:intId)");
    $upInt->bindParam(':intId', $intId, PDO::PARAM_INT);
    foreach($interests as $intId)
    {
        $upInt->execute();
    }
    $affected_rows = $upInt->rowCount();
    return $affected_rows;
}
catch (PDOException $e)
{
    echo "There was a problem connecting to this database.";
    $e->getMessage();
}
}

我的数据库设置函数指定: PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION

但除了我在 Catch 中回显的信息外,我什么也没得到。

一定是错误地解释了教程的说明 - 有人可以告诉我它是什么吗?

4

1 回答 1

1

ok - thanks to the error-handling being switched on, i found several problems, specifically that i was mixing INSERT and UPDATE syntax. in the end i solved the problem with a transaction that deletes existing interests and inserts the new ones. this works well.

here is my code:

function updateThisMemberInterests($PDOdbObject, $memberId, $interests)
{
if ($interests != 0)
{
    try
    {
        //begin transaction
        $PDOdbObject->beginTransaction();

        $delInts = "DELETE FROM `member_interest` WHERE `member_id` = $memberId";
        $PDOdbObject->exec($delInts);

        $intId = 0;
        $upInt = $PDOdbObject->prepare(  "INSERT INTO member_interest (`member_id`,`interest_id`) VALUES ($memberId, :interest_id)" );
        $upInt->bindParam(':interest_id', $intId, PDO::PARAM_INT);
        foreach($interests as $intId)
        {
            $upInt->execute();
        }
        //commit
        $PDOdbObject->commit();
        $affected_rows = $upInt->rowCount();
        return $affected_rows;
    }
    catch (PDOException $e)
    {
        echo "There was a problem - rolling back this transaction.";
        //rollback transaction
        $PDOdbObject->rollBack();
        echo $e->getMessage();
    }
}//more than zero interests changed
}
于 2013-05-18T11:05:13.373 回答