php - Correct Syntax For Preparing MySQL INSERT INTO ON DUPLICATE KEY UPDATE? -
i have been troubleshooting code, searching stackoverflow find correct syntax php. can't figure out how on duplicate key update syntax works prepared statements:
insert placements_by_date (dateval, placementname, placementid, campaignid, adname, format, tagid, impressions, clicks, leads, mediacost) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) on duplicate key update // here comes tricky syntax: dateval = values(dateval), placementname = values(placementname), placementid = values(placementid), campaignid = values(campaignid), adname = values(adname), format = values(format), tagid = values(tagid), impressions = ?, clicks = ?, leads = ?, mediacost = ?
i have tried many variations:
...on duplicate key update dateval = values('$dateval'), placementname = values('$placementname'), placementid = values('$placementid'), campaignid = values('$campaignid'), adname = values('$adname'), format = values('$format'), tagid = values('$tagid'), impressions = values(?), clicks = values(?), leads = values(?), mediacost = values(?)";
i store string, $sql
, , usual stuff:
$mysqli = new mysqli(... ... $stmt = $mysqli -> prepare($sql); $stmt -> bind_param('sissiiiis', $placementname, $placementid, $adname, $format, $tagid, $impressions, $clicks, $leads, $mediacost); ...
but maybe parameters should bound differently??
i enabled tried reading through feedback generic no actionable insights.
$driver = new mysqli_driver(); $driver->report_mode = mysqli_report_all;
hoping have insight share?
thanks!
why not continue using values()
method?
insert placements_by_date(dateval, placementname, placementid, campaignid, adname, format, tagid, impressions, clicks, leads, mediacost ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) on duplicate key update dateval = values(dateval), placementname = values(placementname), placementid = values(placementid), campaignid = values(campaignid), adname = values(adname), format = values(format), tagid = values(tagid), impressions = values(impressions), clicks = values(clicks), leads = values(leads), mediacost = values(mediacost);
you don't specify unique
key is. however, don't need include columns in update
statements, because same.
if, perchance, want increment last 4 values rather assign them, can too:
on duplicate key update dateval = values(dateval), placementname = values(placementname), placementid = values(placementid), campaignid = values(campaignid), adname = values(adname), format = values(format), tagid = values(tagid), impressions = impressions + values(impressions), clicks = clicks + values(clicks), leads = leads + values(leads), mediacost = mediacost + values(mediacost)
edit:
the following should work update last 4 columns:
insert placements_by_date(dateval, placementname, placementid, campaignid, adname, format, tagid, impressions, clicks, leads, mediacost ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) on duplicate key update impressions = values(impressions), clicks = values(clicks), leads = values(leads), mediacost = values(mediacost);
Comments
Post a Comment