article

Wednesday, May 5, 2010

PHP MYSQL - INSERT and UPDATE records function

PHP MYSQL - INSERT and UPDATE records function





$record['userid'] = $_GET['iuser'] ;
$record['busid'] = $_GET['busid'] ;
$record['cid'] = $_GET['cid'];
$record['dateprint'] = $dateadd;
$record['noprinted'] = +1;
$record['dateexpire'] = $dateexp;

$dateadd = date("Y-m-d H:m:s");
$tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));
$xtr = date("Y-m-d",$tomorrow);
$dateexp = $xtr." ".date("H:m:s");

$sqld = "SELECT * FROM dir_coupon_print WHERE busid = '{$_GET['busid']}' AND userid = '{$_GET['iuser']}'";
$result = mysql_query($sqld);
$total = mysql_num_rows($result);//total
//echo $total;
if (mysql_num_rows($result)>0){ //no records founds
while ($rw = mysql_fetch_array($result, MYSQL_ASSOC)){
if (($dateadd >! $rw['dateexpire'])) {
echo "You already consume coupon for this business. wait 24 hours before printing the coupon again. ";
}else{
updateCoupon($rw['id']); //UPDATE RECORDS
}
}
}else {
insertCoupon($record); //INSERT RECORDS
}

function updateCoupon($updateid)
{
$iUserId = intval($iUserId);
$run_sql = "UPDATE dir_coupon_print SET noprinted=noprinted+1 WHERE id = '$updateid'";
$iAlbumId = mysql_query($run_sql) or die( mysql_error()."-cant query");
return $iAlbumId;
}
function insertCoupon($record)
{
$iUserId = intval($iUserId);
$run_sql = "INSERT INTO dir_coupon_print
(userid, cid, busid, dateprint, noprinted, dateexpire)
VALUES ('".$record['userid']."', '".$record['cid']."', '".$record['busid']."', '".$record['dateprint']."', '".$record['noprinted']."', '".$record['dateexpire']."')";
$iAlbumId = mysql_query($run_sql) or die( mysql_error()."-cant query");
return $iAlbumId;
}

Related Post