PHP PDO动态更新具有多个记录的db表到特定用户ID

时间:2013-02-14 18:21:49

标签: pdo

/ *新手需要一些帮助;我正在创建一个类,以便在指示时自动更新我的应用程序数据库记录,但我一直在下面收到此消息,并且对于它,我只是没有看到我做错了什么。有人可以请我查看我的代码吗?谢谢。

警告:PDOStatement :: bindParam()需要至少2个参数,1 ......在第331行中给出;其中“else if(is_string($ val)){”的位置。

* /

// vars given
     // DBDriver: MySQL
$myTable = 'seeYou';
$loginDate = NULL;
$ip = $_SERVER['REMOTE_ADDR'];  
$date = @date('m/d/Y \a\\t h:i a');
$_id =1;

// data array 
$idata = array("last_logged_in"=>$loginDate,
             "login_date"=>$date,
             "ip_addr"=>$ip
             );


class name
{

    ///------------ other methods here---------///

/**
 *--------------------------------------------
 *  Method - PDO: SET FIELD VALUE PLACEHOLDER   
 *--------------------------------------------
 * @return fields with prefix as placeholder 
 */
protected function set_fieldValPlaceHolders(array $data) 
{
    $set = '';
    foreach($data as $field => $value)
    {
        $set .= $field .'= :'.$field . ','; 
    }

    // remove the last comma
    $set = substr($set, 0, -1);
    return $set;
}


public function save($data=NULL, $_id = NULL, $rows= NULL, $dbTable= NULL)
{
    //----------------- some other codes goes here ----------------//
    $id = (int)$_id;

        // update row with a specific id
        if (isset($id) !== NULL && $rows === NULL)
        {
            $set = $this->set_fieldValPlaceHolders($data);

            $sql = "UPDATE {$dbTable} SET {$set} WHERE user_id = :uid"; 

            try
            {
                // Build the database statement
                $_stmt = $this->_dbConn->prepare($sql);
                $_stmt->bindValue(':uid',$id, PDO::PARAM_INT);

                foreach ($data as $field => $val) 
                {
                    if(is_int($val)){  
                        $_stmt->bindValue(':'.$field.'\', '.$val.', PDO::PARAM_INT');
                    }
                    else if(is_string($val)){
                        $_stmt->bindValue(':'.$field.'\', '.$val.', PDO::PARAM_STR');
                    }
                    else if(is_bool($val)){
                        $_stmt->bindValue(':'.$field.'\', '.$val.', PDO::PARAM_BOOL');
                    }
                    else if(is_null($val)){
                        $_stmt->bindValue(':'.$field.'\', '.$val="null".', PDO::PARAM_NULL');
                    }
                    else {
                        $_stmt->bindValue(':'.$field.'\', '.$val.', NULL');
                    }

                    $result = $_stmt->execute();
                    $num = $_stmt->rowCount();
                }

            }
            catch(PDOException $e)
            {
                die('Error!  The process failed while updating your record. <br /> Line #'.__LINE__ .' '.$e);
            }

            if ($result === true)
            {
                return true; 
            }               
        }

1 个答案:

答案 0 :(得分:0)

检查你的bindValue调用:你给1个参数(一个长字符串)。它至少需要两个。检查所有'

例如,它应该是:

$_stmt->bindValue(':'.$field, $val, PDO::PARAM_INT);
相关问题