mysqli更新参数绑定无法正常工作

时间:2014-10-07 14:54:13

标签: php mysql mysqli

我正在尝试使用预准备语句构建一个可重用的函数来更新表中的行。在通过php.net文档挖掘后,我想出了这个。我没有收到任何错误消息,但它没有更新表。 (我写了一个类似的函数来插入记录,但工作得很好。)希望我只是缺少一些简单的东西,有人可以很容易地发现。 :)

function updateRecord($table,$data,$fields,$id) {
    $mysqli = dbConnect();
    $fieldNames = array(); 
    $typeString = '';
    $placeholders = '';
    $params = array();

    foreach($fields as $fieldName => $fieldType) {
        $fieldNames[] = $fieldName; 
        $typeString .= $fieldType;
         $params[] = &$data[$fieldName];
    }
    $params[] = &$id;
    array_unshift($params,$typeString.'i');

    $fieldString = implode('=?,',$fieldNames).'=?';

    $query = 'update '.$table.' set '.$fieldString.' where id = ?';

    if (!$stmt = $mysqli->prepare($query)) {
        echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }
    call_user_func_array(array($stmt, "bind_param"), $params);

    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
    }

    $stmt->close();
    dbDisconnect($mysqli);
}

$_POST传递给$data,像('first_name' => 's', 'last_name' => 's', etc.)这样的数组传递给$fields

我做了一系列的var_dump确实看到了发生了什么,这些都是结果。

$params = array(17) { 
[0]=> string(16) "ssidssdsdssssssi" 
    [1]=> &string(4) "asdf" 
    [2]=> &string(0) "" 
    [3]=> &string(1) "0" 
    [4]=> &string(4) "0.00" 
    [5]=> &string(0) "" 
    [6]=> &string(10) "0000-00-00" 
    [7]=> &string(4) "0.00" 
    [8]=> &string(10) "0000-00-00" 
    [9]=> &string(4) "0.00" 
    [10]=> &string(0) "" 
    [11]=> &string(0) "" 
    [12]=> &string(26) "bc__1412690490_384x523.jpg" 
    [13]=> &string(0) "" 
    [14]=> &string(0) "" 
    [15]=> &string(0) "" 
    [16]=> &int(2) 
}
$fields = array(15) { 
    ["edition"]=> string(1) "s" 
    ["ISBN"]=> string(1) "s" 
    ["pages"]=> string(1) "i" 
    ["price"]=> string(1) "d" 
    ["pub_year"]=> string(1) "s" 
    ["release_date"]=> string(1) "s" 
    ["pre_order_price"]=> string(1) "d" 
    ["sale_start_date"]=> string(1) "s" 
    ["sale_price"]=> string(1) "d" 
    ["kindle"]=> string(1) "s" 
    ["image_1"]=> string(1) "s" 
    ["image_2"]=> string(1) "s" 
    ["paypal_script"]=> string(1) "s" 
    ["pre_order_paypal_script"]=> string(1) "s" 
    ["sale_paypal_script"]=> string(1) "s" 
}   
$fieldnames = array(15) { 
    [0]=> string(7) "edition" 
    [1]=> string(4) "ISBN" 
    [2]=> string(5) "pages" 
    [3]=> string(5) "price" 
    [4]=> string(8) "pub_year" 
    [5]=> string(12) "release_date" 
    [6]=> string(15) "pre_order_price"
    [7]=> string(15) "sale_start_date" 
    [8]=> string(10) "sale_price" 
    [9]=> string(6) "kindle" 
    [10]=> string(7) "image_1" 
    [11]=> string(7) "image_2" 
    [12]=> string(13) "paypal_script" 
    [13]=> string(23) "pre_order_paypal_script" 
    [14]=> string(18) "sale_paypal_script" 
}
$query=string(232) "update editions set edition=?,ISBN=?,pages=?,price=?,pub_year=?,release_date=?,pre_order_price=?,sale_start_date=?,sale_price=?,kindle=?,image_1=?,image_2=?,paypal_script=?,pre_order_paypal_script=?,sale_paypal_script=? where id = ?"

$stmt = object(mysqli_stmt)#2 (10) { //(var_dump placed after $stmt->execute)
    ["affected_rows"]=> int(0),
    ["insert_id"]=> int(0), 
    ["num_rows"]=> int(0),
    ["param_count"]=> int(16),
    ["field_count"]=> int(0),
    ["errno"]=> int(0),
    ["error"]=> string(0) "",
    ["error_list"]=> array(0) { },
    ["sqlstate"]=> string(5) "00000",
    ["id"]=> int(1)
}

0 个答案:

没有答案