php - insert on duplicate key update - in mysqli loop -
i have database primary key 'id' , unique constraint made of price_date , fund_id , currency_id, , class_id
$query = "insert `price_data` (`price_date`, `fund_id`, `currency_id`, `class_id`, `nav`, `nav_change`) values"; $format = " ('%s', '%s', '%s', '%s', %f, %f),"; // go on each array item , append sql query foreach($prices $price) { $query .= sprintf( $format, $mysqli->escape_string($price['pricedate']), $mysqli->escape_string($price['fund']), $mysqli->escape_string($price['currency']), $mysqli->escape_string($price['class']), $mysqli->escape_string($price['nav']), $mysqli->escape_string($price['navchange']) ); } // last values tuple has trailing comma cause // problems, let remove $query = rtrim($query, ','); // mysqli::query returns boolean insert $result = $mysqli->query($query);
i want change query
insert `price_data`(price_date , fund_id , currency_id , nav , nav_change) values () on duplicate key update nav='',nav_change='';
my attempt append original query isn't getting values nav , nav_chnge query.
$query .= ' on duplicate key update nav=$format$mysqli->real_escape_string($data[\'nav\']),nav_change=$format,$mysqli->real_escape_string($data[\'navchange\'])';
if echo resulting query looks following
insert `price_data` (`price_date`, `fund_id` , `currency_id`, `class_id`, `nav`, `nav_change`) values ('2013-04-29', 'aaaa', '0', 'a', 11.793300, 0.054000), on duplicate key update nav=$format$mysqli->real_escape_string($data["nav"]),nav_change=$format,$mysqli->real_escape_string($data["navchange"])
thanks help.
there might cleaner way this, appending query inside loop worked me.
$format = " ('%s', '%s', '%s', '%s', '%s', %f, %f)"; $format_update_nav = " '%f' , "; $format_update_nav_change = " '%f' ; "; $query = "insert `aprice_data` (`price_date`, `fund_id`, `manager_id` , `currency_id`, `class_id`, `nav`, `nav_change`) values "; $query .= sprintf( $format, $mysqli->real_escape_string($data['date']), $mysqli->real_escape_string($data['fund']), $mysqli->real_escape_string($data['manager']), $mysqli->real_escape_string($data['currency']), $mysqli->real_escape_string($data['class']), $mysqli->real_escape_string($data['nav']), $mysqli->real_escape_string($data['navchange']) ); $query .= ' on duplicate key update nav = '; $query .= sprintf( $format_update_nav, $mysqli->real_escape_string($data['nav']) ); $query .= 'nav_change = '; $query .= sprintf( $format_update_nav_change, $mysqli->real_escape_string($data['navchange']) ); $result = $mysqli->query($query);
this returned query looked this
insert `price_data` (`price_date`, `fund_id`, `manager_id` , `currency_id`, `class_id`, `nav`, `nav_change`) values ('2013-05-03', 'wxya', '17', '0', 'a', 456456.000000, 456456.000000) on duplicate key update nav = '456456.000000' , nav_change = '456456.000000' ; insert `price_data` (`price_date`, `fund_id`, `manager_id` , `currency_id`, `class_id`, `nav`, `nav_change`) values ('2013-05-03', 'bxya', '17', '0', 'a', 0.130000, 0.000000) on duplicate key update nav = '0.130000' , nav_change = '0.000000' ; . . .
Comments
Post a Comment