mysqliでON DUPLICATE KEY UPDATEとREPLACE INTOの書き方
ON DUPLICATE KEY UPDATEはあったら更新なかったらインサートします。
UNIQUE インデックスまたは PRIMARY KEYが必要となります。
//ON DUPLICATE KEY UPDATEの方
function thanks1countinsert($t_id,$t1_id,$thanks1_c_id,$t1_u_cnt,$thanks1_update1,$thanks1_update1_id)
{
$db = new mysqli($this->server, $this->user, $this->password, $this->dbname);
$stmt = $db->prepare("INSERT INTO ".$this->tablename13." VALUES (?,?,?,?,?)
ON DUPLICATE KEY UPDATE
counter1_id= VALUES(counter1_id),
counter1_c_id= VALUES(counter1_c_id),
counter1_user= VALUES(counter1_user),
counter1_count= VALUES(counter1_count),
counter1_day1= VALUES(counter1_day1)
");
$stmt->bind_param('sssss',
$counter1_id,
$counter1_c_id,
$counter1_user,
$counter1_count,
$counter1_day1
);
//代入
$counter1_id="$t1_id"."-"."$thanks1_update1_id";
$counter1_c_id="$thanks1_c_id";
$counter1_user="$t1_id";
$counter1_count="$t1_u_cnt";
$counter1_day1="$thanks1_update1";
$stmt->execute();
$cc_id = $stmt->insert_id;//最後のID
if (!empty($cc_id)) {
return $cc_id;
}
$stmt->close();
$db->close();
}
REPLACE INTO は削除してからインサートとなります。
こちらもUNIQUE インデックスまたは PRIMARY KEYが必要となります。
//REPLACE INTOの方
function thanks1countinsert($t_id,$t1_id,$thanks1_c_id,$t1_u_cnt,$thanks1_update1,$thanks1_update1_id)
$db = new mysqli($this->server, $this->user, $this->password, $this->dbname);
$stmt = $db->prepare("REPLACE INTO ".$this->tablename13." VALUES (?,?,?,?,?)");
$stmt->bind_param('sssss',
$counter1_id,
$counter1_c_id,
$counter1_user,
$counter1_count,
$counter1_day1
);
//代入
$counter1_id="$t1_id"."-"."$thanks1_update1_id";
$counter1_c_id="$thanks1_c_id";
$counter1_user="$t1_id";
$counter1_count="$t1_u_cnt";
$counter1_day1="$thanks1_update1";
$stmt->execute();
$cc_id = $stmt->insert_id;//最後のID
if (!empty($cc_id)) {
return $cc_id;
}
$stmt->close();
$db->close();
}
コメント