很多的網站都有積分的這個功能,當積分累積到一定的數量,可以使用這些積分來兌換一些東西,這篇文章主要介紹了PHP積分兌換介面,執行個體分析了積分兌換介面的邏輯處理與資料庫操作技巧,需要的朋友可以參考下,具體實現方法如下:
exchange.php介面內容如下:
<?php /*session_start(); if(!isset($_SESSION['zaszh_user_id'])){ echo json_encode(array('status'=>'error','msg'=>'連線逾時,請重新開啟頁面。')); exit; } $user_id = $_SESSION['zaszh_user_id'];*/ $user_id = 1; // 測試用 $exchange_points = intval($_GET['exchange_points']); require('connect_database.php'); // 扣除答題積分 $mysqli->query("update zaszh_user set answer_points=answer_points-{$exchange_points} where id='{$user_id}' and answer_points>={$exchange_points}"); if($mysqli->affected_rows){ // 有積分 switch($exchange_points){ // 5元話費 case 200: $mysqli->query("update zaszh_telephone_charge_surplus set charge_surplus=charge_surplus-5 where date=substring(now(),1,10) and charge_surplus>=5"); if($mysqli->affected_rows){ // 有剩餘 $mysqli->query("insert into zaszh_award(user_id,prize,create_date) values('{$user_id}','5元話費',unix_timestamp(now()))"); if($mysqli->affected_rows){ echo json_encode(array('status'=>'success','msg'=>'5元話費')); }else{ // 獲獎失敗 } }else{ // 無剩餘 // 恢複答題積分 $mysqli->query("update zaszh_user set answer_points=answer_points+{$exchange_points} where id='{$user_id}'"); } break; // 10元話費 case 400: $mysqli->query("update zaszh_telephone_charge_surplus set charge_surplus=charge_surplus-10 where date=substring(now(),1,10) and charge_surplus>=10"); if($mysqli->affected_rows){ // 有剩餘 $mysqli->query("insert into zaszh_award(user_id,prize,create_date) values('{$user_id}','10元話費',unix_timestamp(now()))"); if($mysqli->affected_rows){ echo json_encode(array('status'=>'success','msg'=>'10元話費')); }else{ // 獲獎失敗 } }else{ // 無剩餘 // 恢複答題積分 $mysqli->query("update zaszh_user set answer_points=answer_points+{$exchange_points} where id='{$user_id}'"); } break; } // 記錄積分消耗 $mysqli->query("insert into zaszh_answer_points_consume(user_id,points_consume,consume_for,create_date) values('{$user_id}','{$exchange_points}','exchange',unix_timestamp(now()))"); }else{ // 無積分 echo json_encode(array('status'=>'error','msg'=>'您的積分不足。')); } $mysqli->close();