Reputation: 19
so I know this is a basic question, and I've tried to search around, but everything I've found isn't really working.
Anyways, I have a system where somebody can only view a page once per day. Now, I currently have a button that can be pressed, that already updates the MySQL Row.. but obviously, they can just keep refreshing the page, and they'll have unlimited access to it.
The way it would work is, when the page is loading.. the database row will be updated, as the same it would with the button.
This is one of my first projects, so I greatly appreciate any help that is given. Below is something I already tried, but it doesn't seem to work..
Update.php
<?php
include '../view.php'
mysqli_query($con, "UPDATE `users` SET `viewedtd`=`viewedtd`+1 WHERE `username` = '$username'") or die("Please contact us.");
JS Part
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "inc/update.php"
success: function(data) {
//blah
}
});
});
</script>
View.php
<?php
session_start();
include 'inc/config.php';
if (!isset($_SESSION['userSession'])) {
header("Location: login.php?access=denied");
}
$query = $con->query("SELECT * FROM users WHERE uid=" . $_SESSION['userSession']);
$userRow = $query->fetch_array();
include 'inc/global-checks.php';
if (isset($_GET['reported']) && $_GET['reported'] == 'true') {
$message = '<div class="alert alert-success"><span>Thanks! Our team will review it.</span></div>';
}
$ip = mysqli_real_escape_string($con, htmlspecialchars($_SERVER['REMOTE_ADDR']));
date_default_timezone_set('America/New_York');
$date = date('Y-m-d H:i:s');
if ($userRow['viewedtd'] >= $userRow['viewlimit']) {
header("Location: membership.php?lr=true");
}
$accounts = $con->query('SELECT epc FROM gen ORDER BY RAND() LIMIT 1;');
$result = $accounts->fetch_assoc();
if (isset($_POST['generate'])) {
$username = $userRow['username'];
$generated = strip_tags($_POST['generated']);
$generated = $con->real_escape_string($generated);
if ($userRow['viewedtd'] >= $userRow['viewlimit']) {
header("Location: membership.php?lr=true");
}
$insert = mysqli_query($con, "UPDATE `users` SET `viewedtd`=`viewedtd`+1 WHERE `username` = '$username'") or die("Please contact us.");
$insert2 = mysqli_query($con, "UPDATE `users` SET `waitperiod`= '$date' WHERE `username` = '$username'") or die("Please contact us.");
}
if (isset($_POST['rba'])) {
$generated = strip_tags($_POST['generated']);
$generated = $con->real_escape_string($generated);
$insert = mysqli_query($con, "UPDATE `gen` SET `working`= 'No' WHERE `epc` = '$generated'") or die("Please contact us.");
header("Location: generator.php?reported=true");
}
?>
<!DOCTYPE html>
<html class="no-focus" lang="en">
<head>
<meta charset="utf-8">
<title>Generator - <?php echo $site_name; ?></title>
<meta name="description" content="<?php echo $description; ?>">
<meta name="author" content="<?php echo $author; ?>">
<meta name="robots" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="shortcut icon" href="assets/img/favicons/favicon.png">
<link rel="icon" type="image/png" href="assets/img/favicons/favicon-16x16.png" sizes="16x16">
<link rel="icon" type="image/png" href="assets/img/favicons/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="assets/img/favicons/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="assets/img/favicons/favicon-160x160.png" sizes="160x160">
<link rel="icon" type="image/png" href="assets/img/favicons/favicon-192x192.png" sizes="192x192">
<link rel="apple-touch-icon" sizes="57x57" href="assets/img/favicons/apple-touch-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="assets/img/favicons/apple-touch-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="assets/img/favicons/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="assets/img/favicons/apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="assets/img/favicons/apple-touch-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="assets/img/favicons/apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="assets/img/favicons/apple-touch-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="assets/img/favicons/apple-touch-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="assets/img/favicons/apple-touch-icon-180x180.png">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400italic,600,700%7COpen+Sans:300,400,400italic,600,700">
<link rel="stylesheet" href="assets/js/plugins/slick/slick.min.css">
<link rel="stylesheet" href="assets/js/plugins/slick/slick-theme.min.css">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<link rel="stylesheet" id="css-main" href="assets/css/oneui.css">
</head>
<body>
<div id="page-container" class="sidebar-l sidebar-o side-scroll header-navbar-fixed">
<nav id="sidebar">
<div id="sidebar-scroll">
<div class="sidebar-content">
<div class="side-header side-content bg-white-op">
<button class="btn btn-link text-gray pull-right hidden-md hidden-lg" type="button" data-toggle="layout" data-action="sidebar_close">
<i class="fa fa-times"></i>
</button>
<a class="h5 text-white" href="index.php">
<span class="h4 font-w600 sidebar-mini-hide"><?php echo $site_name; ?></span>
</a>
</div>
<div class="side-content">
<ul class="nav-main">
<li>
<a class="" href="index.php"><i class="si si-home"></i><span class="sidebar-mini-hide">Home</span></a>
</li>
<li>
<a class="" href="membership.php"><i class="si si-credit-card"></i><span class="sidebar-mini-hide">Membership</span></a>
</li>
<li>
<a class="active" href="generator.php"><i class="si si-refresh"></i><span class="sidebar-mini-hide">Generator</span></a>
</li>
</ul>
</div>
</div>
</div>
</nav>
<header id="header-navbar" class="content-mini content-mini-full">
<ul class="nav-header pull-right">
<li>
<div class="btn-group">
<button class="btn btn-default btn-image dropdown-toggle" data-toggle="dropdown" type="button">
<img src="assets/img/avatars/avatar10.jpg" alt="Avatar">
<span class="caret"></span>
</button>
<ul class="dropdown-menu dropdown-menu-right">
<li class="dropdown-header">Profile</li>
<li>
<a tabindex="-1" href="settings.php">
<i class="si si-settings pull-right"></i>Settings
</a>
</li>
<li class="divider"></li>
<li class="dropdown-header">Actions</li>
<li>
<a tabindex="-1" href="logout.php">
<i class="si si-logout pull-right"></i>Log out
</a>
</li>
</ul>
</div>
</li>
</ul>
</header>
<main id="main-container">
<div class="content">
<div class="row">
<div class="col-lg-4">
<div class="block block-themed">
<div class="block-header bg-danger">
<h3 class="block-title">Generator</h3>
</div>
<div class="block-content">
<div class="text-center push-10-t push-30">
<img class="img-avatar img-avatar96" src="assets/img/photos/p1.png" alt="">
</div>
<form class="form-horizontal" action="generator.php" method="post">
<div class="form-group">
<label class="col-xs-12" for="lock1-password">Random Info Below</label>
<div class="col-xs-12">
<input class="form-control" type="text" id="generated" name="generated" readonly="" value="<?php echo $result['epc'] ?>" placeholder="Click 'Generate'">
</div>
</div>
<div class="form-group">
<div class="col-xs-4">
<button class="btn btn-sm btn-danger" name="generate" type="submit"><i class="fa fa-refresh push-5-r"></i> Generate</button>
</div>
<div class="col-xs-4">
<button class="btn btn-sm btn-danger" name="rba" type="submit"><i class="si si-ban push-5-r"></i> Report Broken Account</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</main>
<?php include 'inc/footer2.php'; ?>
</div>
<script src="assets/js/core/jquery.min.js"></script>
<script src="assets/js/core/bootstrap.min.js"></script>
<script src="assets/js/core/jquery.slimscroll.min.js"></script>
<script src="assets/js/core/jquery.scrollLock.min.js"></script>
<script src="assets/js/core/jquery.appear.min.js"></script>
<script src="assets/js/core/jquery.countTo.min.js"></script>
<script src="assets/js/core/jquery.placeholder.min.js"></script>
<script src="assets/js/core/js.cookie.min.js"></script>
<script src="assets/js/app.js"></script>
<script src="assets/js/plugins/slick/slick.min.js"></script>
<script src="assets/js/plugins/chartjs/Chart.min.js"></script>
<script>
jQuery(function () {
App.initHelpers('slick');
});
</script>
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "inc/update.php"
success: function(data) {
alert("Page is loaded");
}
});
});
</script>
</body>
</html>
(Note; I also have include 'inc/update.php'; in my initial PHP statement on the regular page, I'm not sure if it's needed.
Update: This is the error I get when going to update.php
Parse error: syntax error, unexpected 'mysqli_query' (T_STRING) in C:\xampp\htdocs\gen\inc\update.php on line 3
Upvotes: 1
Views: 467
Reputation: 21
There is a very basic mistake you have committed in your query.
UPDATE `users` SET `viewedtd`=+1 WHERE `username` = '$username';
should be
UPDATE `users` SET `viewedtd`=viewedtd+1 WHERE `username` = '$username'
You are constantly assigning "+1" integer to your column 'viewedtd', instead of incrementing it.
Upvotes: 1