noncent / pdo_class_wrapper

A Class for PDO Wrapper
25 stars 21 forks source link

Insert issue #4

Closed tmuks closed 10 years ago

tmuks commented 10 years ago

Issue while inserting row with insert() I have counted it has enough matching parameter though it shows mismatch error

Executed Query -> INSERT INTO table (user_id,title,description,address,latitude,longitude,property_id,outdoor_size,late_bookings,cancellation_policy,created_date) VALUES (2,"poodles r us","poodles r us makes u dog happy! NO kennels, just friendly people TO look AFTER your dog WHILE you re away! think OF us friends","slough, slough, slough sl3 8ay, uk",51.5006615,-0.5303367,6,"medium",1,1,"2014-08-19 22:36:23"); ERROR:"SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens: 480"

lesslauer commented 10 years ago

That's not an issue- please make sure that all numeric values are integer, than it will be running... --> I recently had exactly the same problem, solved it by setting the variables in php to integer ($foo = (int)$foo).

noncent commented 10 years ago

Hi tmuks,

Thanks for using Class. You are getting error because of you are using lat, long values without quotes and the values are double/decimal type. PDO has not an PDO::PARAM for decimals/doubles. So, You can change function _bindPdoNameSpace by adding

// is double found then pdo param as string
case 'double':                    
$this->_oSTH->bindParam( ":s" . "_" . "$f", $array[$f], PDO::PARAM_STR );
break;

in else condition around line number 988.

or you can wrap your values in quotes as given example..

// your insert array data
$dataArray = array(
    "user_id" => 2,
    "title" => "poodles r us",
    "description" => "poodles r us makes u dog happy! NO kennels, just friendly people TO look AFTER your dog WHILE you re away! think OF us friends",
    "address" => "slough, slough, slough sl3 8ay, uk",
    "latitude" => "51.5006615",
    "longitude" => "-0.5303367",
    "property_id" => 6,
    "outdoor_size" => "medium",
    "late_bookings" => 1,
    "cancellation_policy" => 1,
    "created_date" => "2014-08-19 22:36:23"
);
// execute the insert 
$lid = $db->insert('table',$dataArray)->showQuery()->getLastInsertId();
tmuks commented 10 years ago

Thnaks all, It was the issue with double value as neeraj suggested I added following to the function _bindPdoNameSpace

//case 'double':
default:
$this->_oSTH->bindParam( ":s" . "_" . "$f", $array[$f], PDO::PARAM_STR );

And really a good class you created, just few minor enhancement required for optimum results