Reputation: 43
Im tryin count the number of times a link has been ckicked then redirect to the affiliate website with that id. i have have managed to do the click part where it updates 1 in the database everytime the link is clicked but having problems with the redirect part, i send the id to track.php then retrive the id there but i have trouble redirecting from there. in the database i have affiliate_link and stored the url there. any help would be great.
on track.php
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/market/core/init.php';
//update count click in database
if(isset($_GET['id'])){
$id = is_numeric($_GET['id']);
$db->query("UPDATE credit_card_offers SET count_click = count_click + 1 WHERE id = '$id'");
header('Location: How_to_call_Affiliate_link_here_form_database?');
}
?>
on index.php .
<a href="track.php?id=<?= $product['id']; ?>">See Deal</a>
sql dump
-- phpMyAdmin SQL Dump
-- version 4.6.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 25, 2017 at 03:54 PM
-- Server version: 5.7.14
-- PHP Version: 5.6.25
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `dataz`
--
-- --------------------------------------------------------
--
-- Table structure for table `credit_card_offers`
--
CREATE TABLE `credit_card_offers` (
`id` int(11) NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`brand` int(11) NOT NULL,
`categories` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`image` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`balance_transfers` text COLLATE utf8_unicode_ci NOT NULL,
`purchases` text COLLATE utf8_unicode_ci NOT NULL,
`representative_apr` text COLLATE utf8_unicode_ci NOT NULL,
`representative_example` text COLLATE utf8_unicode_ci NOT NULL,
`affiliate_link` text COLLATE utf8_unicode_ci NOT NULL,
`featured` tinyint(4) NOT NULL DEFAULT '0',
`count_click` int(255) NOT NULL DEFAULT '0',
`deleted` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Dumping data for table `credit_card_offers`
--
INSERT INTO `credit_card_offers` (`id`, `title`, `brand`, `categories`, `image`, `description`, `balance_transfers`, `purchases`, `representative_apr`, `representative_example`, `affiliate_link`, `featured`, `count_click`, `deleted`) VALUES
(15, 'Barclaycard Platinum With Balance Transfer (25/25 Card)', 19, '19', '/market/images/products/5ac13b939568b60b71eb9fb1fa4d82df.png', 'A 2.5% fee is applied to balance transfers but a refund reduces this to 1.49% within 2 days (T&Cs apply). Transfer a balance within 60 days of opening an account to get the 0% deal; otherwise the rate will be 18.9% p.a. variable with no fee. ', '0% for 25 months with a 1.49% fee', '0% for 25 months', '18.9% APR', 'Representative Example: The standard interest rate on purchases is 18.9% p.a. (variable), so if you borrow £1,200 the Representative APR will be 18.9% APR (variable). ', 'https://www.barclaycard.co.uk/personal/platinum', 0, 0, 0);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `credit_card_offers`
--
ALTER TABLE `credit_card_offers`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `credit_card_offers`
--
ALTER TABLE `credit_card_offers`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=16;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Upvotes: 4
Views: 1246
Reputation: 23892
On track.php
you're going to need to use another query to pull the relevant record. An alternative approach would be to include the URL in the link, because the data is already accessible on the parent page. On index.php do:
<a href="track.php?id=<?= $product['id']; ?>&url=<?= urlencode($product['affiliate_link']);?>">See Deal</a>
then on track.php
you will have it as
$_GET['url']
you then could do:
if(!empty($_GET['url'])) {
header('Location: ' . $_GET['url']);
exit;
}
Also note the is_numeric
returns a boolean so that $id
assignment is also probably not correct.
You also could validate the URL, http://php.net/manual/en/function.filter-var.php and FILTER_VALIDATE_URL
.
Upvotes: 1