php-mysqli重复插入


php mysqli insert on duplicate

在outerweb上问候;-)

为了让我的生活更轻松一点,我决定做一个用数据更新表的功能。

我想这样做。例如:

$table = "ticket_stati";
$fields = array(`ticket_stati_id`, `locked_record`, `ticket_stati_name`, `ticket_stati_description`, `ticket_stati_color`);
$data = array(1, 1, 'Open', 'The ticket is marked as open and not assigned or acked.', '#130a5a');
// Call function
func_update_table($table, $fields, $data);
$data = array(2, 1, 'Assigned', 'The ticket has been assigned to a user or group', '#11a916');
func_update_table($table, $fields, $data);

但我很难弄清楚应该如何创建INSERT INTO,以确保在重复时只更新具有可更新数据的字段。

我假设ON DUPLICATE使用PRIMARY密钥来决定是否存在重复。因此,除了主键之外的任何东西都应该更新(在上面的例子中,删除数据中的ticket_static_id和相应的id,并更新其余的)或者只将$data中的数据与表中的数据匹配,以及具有不同数据的字段更新这些数据。

似乎我需要做一些工作。(精…)

所以我做了一个小函数(基于以上关于主键上的on DUBLICATE的假设。

函数alter_data_in_table($table、$fields、$data){全球$db;

/* 
expect Index to be primary or unique 
so if primary key exists - update else insert
*/
$temp_line = array();
$temp_data = array();
/* Secure sanity in data */
foreach ($data as $line) {
    $temp_line = array();
    foreach ($line as $value) {
        $value = str_ireplace("'", "", $value);
        $temp_line[] = "'".$value."'";
    }
    $temp_data[] = $temp_line;
}
$data = $temp_data;
foreach ($data AS $line) {
    $sql = "
    INSERT INTO ".$table." (".implode(", ", $fields).") VALUES (".implode(", ", $line).")
    ON DUPLICATE KEY UPDATE ";
    // Start at 1. Skip ID
    for($x=1; $x < sizeof($fields); $x++) {
        $line[$x] = str_ireplace("'", "", $line[$x]);
        $sql .= $fields[$x]."='".$line[$x]."'";
        if ($x+1 == sizeof($fields))
            $sql .= ";";
        else
            $sql .= ", ";
    }
    mysqli_query($db, $sql) or cc("ERROR: SQL Update Table Data", $sql, mysqli_error($db), 0, $this_document);
}

}//结束函数alter_data_in_table

用途为:

$table = "ticket_stati";
$fields = array('ticket_stati_id', 'locked_record', 'ticket_stati_name', 'ticket_stati_description', 'ticket_stati_color');
$data = array(
array(1, 1, 'Open', 'The ticket is marked as open and not assigned or acked.', '#130a5a'),
array(2, 1, 'Assigned', 'The ticket has been assigned to a user or group', '#11a916'),
array(3, 1, 'Closed', 'The ticket has been closed and are no longer active.', '#1121e7'),
array(4, 1, 'Marked as spam', 'The ticket has been marked as spam and is set for purging', '#ab1d1d'),
array(5, 1, 'Time Started', 'The time has been started and the ticket is active', '#f28282'),
array(6, 1, 'Time Stopped', 'The time has been stopped and the ticket is inactive', '#ddf76b'),
array(7, 1, 'Updated', 'The ticket has been updated', '#42f62d'),
array(8, 1, 'Manual Escalation', 'The ticket has been escalated by a user and nearest leader has been informed.', '#028d00'),
array(9, 1, 'Automatic Escalation', 'The ticket has been automatic escalated and nearest leader has been informed.', '#9a1212'),
array(10, 1, 'Pending Customer', 'The ticket awaits responce from customer', '#44a981'),
array(11, 1, 'Pending Supplier', 'The ticket awaits responce from supplier', '#315e4c'),
array(12, 1, 'Pending User', 'The ticket awaits responce from another user.', '#7ba75c'),
array(13, 1, 'Pending External Consultant', 'The ticket awaits responce from external Consultant', '#6a1792'),
array(14, 1, 'ToDo', 'Ticket marked to be dealt with later', '#804000')
);
alter_data_in_table($table, $fields, $data);

去试验台;-)