I am trying to create a tracker code, which is when a user enter my page, I will record and add the visitor view by 1
The problem with my code is on every monday, it not only reset the week stats, it also reset the month stats, I paste my increment code and reset code in php below.
Thanks for helping me, I try to figure it out for a few days but can't spot where I code wrongly.
This is my ads.php which I run it by do
<img src="ads.php" width="1" height="1">
at every page
This is my ads.php code
include 'conn.php';
$postID = $_GET['pid'];
$todayDate = date("m-d-y");
//Query with postDate
$sql = "SELECT * FROM wp_tracker WHERE postID LIKE '" . $postID . "'";
$result = mysql_query($sql);
$exist = 0;
while ($row = mysql_fetch_array($result)) {
$postDate = $row['postDate'];
$dayView = $row['dayView'];
$weekView = $row['weekView'];
$monthlyView = $row['monthlyView'];
$exist++;
}
if ($exist == 0) {
//create new record
$sql = "INSERT INTO wp_tracker (postID, postDate) VALUES ('$postID ','$todayDate')";
if (!mysql_query($sql, $con)) {
die('Error: ' . mysql_error());
}
}
if ($exist > 0) {
//if record exist
//check if it same day
if ($postDate == $todayDate) {
//if same day - just increment views
$dayView = $dayView + 1;
$weekView = $weekView + 1;
$monthlyView = $monthlyView + 1;
$sql2 = "UPDATE wp_tracker SET dayView = '$dayView',weekView = '$weekView',monthlyView = '$monthlyView' WHERE postID='$postID '";
$result2 = mysql_query($sql2);
}
}//end if exist
Below is my reset code (reset.php)
$todayDate = date("m-d-y");
//Query with postDate
$sql = "SELECT postDate FROM wp_tracker order by postDate ASC";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$postDate = $row[0];
$timestamp = time();
if ($postDate != $todayDate) {
//a new day -- update dayView to 0
$dayView = 1;
$sql3 = "UPDATE wp_tracker SET postDate= '" . $todayDate . "', dayView = '$dayView'";
$result3 = mysql_query($sql3);
}
if (date('D', $timestamp) === 'Mon') {
$weekView = 1;
$sql3 = "UPDATE wp_tracker SET weekView = '$weekView'";
$result3 = mysql_query($sql3);
}
if (date('d', $timestamp) === '01') {
$monthlyView = 1;
$sql3 = "UPDATE wp_tracker SET monthlyView= '$monthlyView'";
$result3 = mysql_query($sql3);
}
Related
When i run this php code the counter variable only goes 1 and 2 but i want to make it go 5 what is wrong here anyone can help me ? why the counter dosent go up from 2 ?
<?php
session_start();
include "sql.php";
$date = $_POST['date'];
$email = $_SESSION[email];
$counter = 0;
$check = " select * from reservations where date = '$date'";
$result = mysqli_query($con, $check);
$num = mysqli_num_rows($result);
if($num = 1){
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
echo $counter;
if ($counter < 5){
$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
}
else{
echo "no tables available";
}
}
else{
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
}
I think you should put it on a looping.
Check code below:
<?php
session_start();
include "sql.php";
$date = $_POST['date'];
$email = $_SESSION[email];
$counter = 0;
$check = " select * from reservations where date = '$date'";
$result = mysqli_query($con, $check);
$num = mysqli_num_rows($result);
if($num = 1){
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
echo $counter;
if ($counter < 5){
while($counter <= 5){
//$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
$counter++;
}
}
else{
echo "no tables available";
}
}
else{
$data = " select * from reservations where date = '$date'";
$dresult = mysqli_query($con, $data);
$row = mysqli_fetch_array($dresult);
$counter = $row[counter];
$counter=$counter+1;
$reg= "insert into reservations(date,counter,email) values('$date' , '$counter' , '$email')";
mysqli_query($con, $reg);
}
User will enter like more than one payment card 1.00,cash 2.00,card 10,00,cash 20.00 etc...After these all values insert into payment_details table one by one along with current date.So after this i need to insert data to another table called moneybox table.
Count of total cash and total card will store into money box group by current date.Always two rows ie; card and cash will be there in money table,going to total of cash and card will be store based on current date.
As far as I understand the requirements, this may help...
<?php
if (isset($_POST["getamount"])) {
$getinvoiceid = $_POST['getinvoiceid'];
$getstorepaymode = $_POST['getstorepaymode'];
$getamount = $_POST['getamount'];
$sql1 = "select date from moneybox order by ID desc limit 1";
$result1 = mysqli_query($link, $sql1);
$row1 = mysqli_fetch_array($result1);
//echo json_encode($row1);
$last_moneybox_created_date = $row1['date'];
$sqlclosebalcash = "select closing_balance from moneybox where date='$last_moneybox_created_date' and type='cash'";
$resultclosebal_cash = mysqli_query($link, $sqlclosebalcash);
$rowclosebal_cash = mysqli_fetch_array($resultclosebal_cash);
//echo json_encode($row1);
//$last_moneybox_closingbalanacecash = $rowclosebal_cash['closing_balance'];
$sqlclosebalcard = "select closing_balance from moneybox where date='$last_moneybox_created_date' and type='bank'";
$resultclosebal_card = mysqli_query($link, $sqlclosebalcard);
$rowclosebal_card = mysqli_fetch_array($resultclosebal_card);
//$last_moneybox_closingbalanacecard = $rowclosebal_card['closing_balance'];
$tz = 'Asia/Dubai'; // your required location time zone.
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
$todayDate = $dt->format('Y-m-d');
if ($rowclosebal_cash['closing_balance'] == '') {
$last_moneybox_closingbalanacecash = "0.00";
} else {
$last_moneybox_closingbalanacecash = $rowclosebal_cash['closing_balance'];
}
if ($rowclosebal_card['closing_balance'] == '') {
$last_moneybox_closingbalanacecard = "0.00";
} else {
$last_moneybox_closingbalanacecard = $rowclosebal_card['closing_balance'];
}
for ($count = 0; $count < count($getamount); $count++) {
$payamt_clean = $getamount[$count];
$getstorepaymode_clean = $getstorepaymode[$count];
date_default_timezone_set('Asia/Dubai');
$created = date("y-m-d H:i:s");
$query = 'INSERT INTO payment_details (invoiceID,paymentMode,Amount,created)
VALUES ("' . $getinvoiceid . '" , "' . $getstorepaymode_clean . '", "' . $payamt_clean . '", "' . $created . '");
';
mysqli_query($link, $query);
// check whether card or cash or both rows are already there or not
$sql1 = "select * from moneybox WHERE date='$todayDate' AND type='cash' ";
$resultcash = mysqli_query($link, $sql1);
if(mysqli_num_rows($resultcash)) {
$cashMode = 1;
}
else {
$cashMode = 0;
}
$sql1 = "select * from moneybox WHERE date='$todayDate' AND type='bank' ";
$resultcard = mysqli_query($link, $sql1);
if(mysqli_num_rows($resultcard)) {
$cardMode = 1;
}
else {
$cardMode = 0;
}
$cal_closingbalancecash = $last_moneybox_closingbalanacecash - $payamt_clean;
$cal_closingbalancecard = $last_moneybox_closingbalanacecard - $payamt_clean;
switch($getstorepaymode_clean) {
case "CASH":
if($cashMode === 0) {
echo 'Different Date cash'; //insert happen based on the type
$last_moneybox_created_date = $todayDate;
$cashMode = 1;
$query = "INSERT INTO moneybox (type,inflow,date)
VALUES ('cash','$payamt_clean','$todayDate');";
}
else {
echo 'Same Date cash'; //update happen based on type and date
$query = "UPDATE moneybox SET
inflow = inflow + $payamt_clean,
closing_balance= opening_balance + inflow - outflow
WHERE type = 'cash' and date = '$todayDate';";
}
break;
case "CARD":
if($cardMode === 0) {
echo 'Different Date card'; //insert happen based on the type
$last_moneybox_created_date = $todayDate;
$cardMode = 1;
$query = "INSERT INTO moneybox (type,inflow,date)
VALUES ('bank','$payamt_clean','$todayDate');";
}
else {
echo 'Same Date card'; //update happen based on type and date
$query = "UPDATE moneybox SET
inflow = inflow + $payamt_clean,
closing_balance= opening_balance + inflow - outflow
WHERE type = 'bank' and date = '$todayDate';";
}
break;
} // end switch case
if (mysqli_query($link, $query)) {
echo 'paydetails Inserted';
}
else {
echo "Error: " . $query . "<br>" . mysqli_error($link);
}
}
}
?>
I am trying to generate a either 0 or 1 and when it is generated update a row with the value. 1 being heads and 0 being tails, like a coin flip. I can't use external libraries for this.
The problem is when it generates a 0 it won't update the row but with a 1 it does, and I've no idea as to why.
<?php
include_once('../library/user.php');
include_once('../config/connect.php');
$sql = "SELECT * FROM coinroulette ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$rows = $result->fetch_array(MYSQLI_ASSOC);
$id = $rows['id'];
$user = $_SESSION["user"]->getUsername();
$beginGame = new DateTime($rows['time']);
$currentTime = new DateTime(date("Y-m-d H:i:s"));
$diff = $currentTime->diff($beginGame);
if ($currentTime < $beginGame) {
echo "Remaining:" . $diff->format('%S') . "<br>";
}
$beginGame->sub(new DateInterval('PT15S'));
//echo $beginGame->format('Y-m-d H:i:s') . "<br>" . $currentTime->format('Y-m-d H:i:s') . "<br>" . $rows['time'] . "<br>";
//echo rand(0,1);
//Check is game is in place. (result == empty) game in place.
//Otherwise its over and a new game is made.
if (empty($rows['result'])) {
echo "Accepting bets.";
if ($currentTime >= $beginGame) {
//Needs to be converted to an INT or else it cant be inserted into datbase. Took me 3 hours to figure this error out. :(
$gamble = mt_rand(0,1);
$sql = "UPDATE coinroulette SET result = '$gamble' WHERE id = '$id'";
$result = $conn->query($sql);
}
} else {
echo "No longer accepting bets.<br>Result: ";
if ($rows['result'] == "1") {
echo "Heads<br>";
} else {
echo "Tails<br>";
}
$sql = "SELECT * FROM bets WHERE gameID = '$id' AND user = '$user'";
$result = $conn->query($sql);
$betRows = $result->fetch_array(MYSQLI_ASSOC);
if ($result->num_rows > 0) {
echo "InGame<br>";
if ($rows['result'] == $betRows['guess']) {
if(empty($betRows['result'])){
$sql = "UPDATE bets SET result = 1 WHERE user = '$user'";
$result = $conn->query($sql);
$winAmount = $betRows['amount'] * 2;
$sql = "UPDATE users SET points = points + '$winAmount' WHERE username = '$user'";
$result = $conn->query($sql);
}
echo "Winner";
} else {
$sql = "UPDATE bets SET result = 0 WHERE user = '$user'";
$result = $conn->query($sql);
echo "Loser";
}
} else {
echo "Not<br>";
}
}
?>
Solved it myself, you cant update a null field. So I set it as -1.
I have this code:
<?php
include 'config.php';
date_default_timezone_set('America/Los_Angeles');
$d = date('Y-m-d');
$m = date("m");
$day = date("d");
$t = date("His");
$ip = $_SERVER['REMOTE_ADDR'];
$c = file_get_contents('http://api.wipmania.com/'.$ip);
echo "<h2>ALL RESULTS TODAY:</h2><table>";
$_GET['c'] = $c;
$sc = $_GET['sc'];
if($c === "key"){
if($sc === "t"){
$result = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
while($row = mysqli_fetch_array($result))
{echo "<tr><td>".$row['key'] . "</td><td> " . $row['country']."</td><td>".$row['ip']."</td></tr>"; }
}
}
echo '</table>';
?>
i have tried without $con: mysqli_fetch_array($result), but it was the same...
But nothings appear...
no error no results...
Please help... Thanks!
You have not connected to the database or queried your results:
$conn = mysqli_connect($hostname,$username,$password,$dbname) or die(mysqli_error());
//...
$your_query = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
$result = mysqli_query($conn, $your_query);
while ($row = mysqli_fetch_array($result)){
//...
}
you forgot mysqli_query.
replace this
$result = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
by
$result =mysqli_query("SELECT * FROM main WHERE date = '$d' ORDER BY time");
You forgot to perform the query.
$result = mysqli_query($con, "SELECT * FROM main WHERE date = '$d' ORDER BY time");
I have an attendance page which outputs a list of students in a class through the following loop:
$sql10 = "SELECT class.name, student_to_class.class_id, student_to_class.student_id
FROM
student_to_class
INNER JOIN
class
ON class.id=student_to_class.class_id
WHERE
class.name = '$classid'";
$result10 = mysql_query($sql10) or die(mysql_error());
while ($row = mysql_fetch_array($result10)) {
$student = $row['student_id'];
$classid = $row['class_id'];
$sql3 = "select * from student where id = '$student'";
$result3 = mysql_query($sql3) or die(mysql_error());
$row3 = mysql_fetch_assoc($result3);
$studentfname = $row3['first_name'];
$studentlname = $row3['last_name'];
$sql4 = "select * from student where first_name = '$studentfname' AND last_name = '$studentlname'";
$result4 = mysql_query($sql4) or die(mysql_error());
$row4 = mysql_fetch_assoc($result4);
$studentrfid = $row4['rfid'];
$sql5 = "select * from class where id = '$classid'";
$result5 = mysql_query($sql5) or die(mysql_error());
$row5 = mysql_fetch_assoc($result5);
$class_name = $row5['name'];
//Define the default variables assuming attendance hasn't been taken.
$david = "select * from student where rfid='$studentrfid'";
$davidresult = mysql_query($david) or die(mysql_error());
$drow = mysql_fetch_assoc($davidresult);
if (($drow['excused'] == '1') && ($drow['excuseddate'] == $date)) {
//if($drow['excuseddate'] == $date;
$excusedabsense = '<option value="Excused Absense" label="Excused Absense" selected="selected">Excused Absense</option>';
} else {
$excusedabsense = '';
}
$presentpunctual = '<option value="Present" label="Present">Present</option>';
$presenttardy = '<option value="Tardy" label="Tardy">Tardy</option>';
$unexcusedabsense = '<option value="Absent" label="Absent">Absent</option>';
if (isset($_POST['editdate'])) {
$date = $_POST['date'];
}
$realfname = $studentfname;
$reallname = $studentlname;
$sql4 = "select * from attendance_main where StudentID = '$studentrfid' AND date = '$date' AND classID = '$class_name'";
$result4 = mysql_query($sql4) or die(mysql_error());
$row4 = mysql_fetch_assoc($result4);
if ($row4['status'] == "Present") {
$presentpunctual = '<option value="Present" label="Present" selected="selected">Present</option>';
} else {
$presentpunctual = '<option value="Present" label="Present">Present</option>';
}
if ($row4['status'] == "Tardy") {
$presenttardy = '<option value="Tardy" label="Tardy" selected="selected">Tardy</option>';
} else {
$presenttardy = '<option value="Tardy" label="Tardy">Tardy</option>';
}
if ($row4['status'] == "Absent") {
$unexcusedabsense = '<option value="Absent" label="Absent" selected="selected">Absent</option>';
} else {
$unexcusedabsense = '<option value="Absent" label="Absent">Absent</option>';
}
$b++;
echo "<tr>";
if (!isset($dateform)) {
$dateform = date('m/d/Y');
}
$date = date('m/d/Y');
echo '<td><iframe src="flag.php?&flagdate=' . $dateform . '&curdate=' . $date . '&class=' . $classid . '&flag=1&user=' . $studentrfid . '&curflag=' . $realrfid['flag'] . '&flagclass=' . $classname . '" width="50" height="30" frameborder="0" scrolling="no"> </iframe></td>';
//Yesterday
$sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$yesterdaysql' AND classID = '$class_name'";
$result8 = mysql_query($sql8) or die(mysql_error());
$tooltiprow = mysql_fetch_assoc($result8);
if (mysql_num_rows($result8) == 0) {
$tooltipresult_yesterday = "N/A";
} else {
$tooltipresult_yesterday = $tooltiprow['status'];
}
//2 days
$sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$days2sql' AND classID = '$classid'";
$result8 = mysql_query($sql8) or die(mysql_error());
$tooltiprow = mysql_fetch_assoc($result8);
if (mysql_num_rows($result8) == 0) {
$tooltipresult_2days = "N/A";
} else {
$tooltipresult_2days = $tooltiprow['status'];
}
//3 days
$sql8 = "select * from attendance_main where StudentID = '$studentrfid' AND date='$days3sql' AND classID = '$class_name'";
$result8 = mysql_query($sql8) or die(mysql_error());
$tooltiprow = mysql_fetch_assoc($result8);
if (mysql_num_rows($result8) == 0) {
$tooltipresult_3days = "N/A";
} else {
$tooltipresult_3days = $tooltiprow['status'];
}
$tooltip = "<b>" . $yesterday . ":</b> " . $tooltipresult_yesterday . " - <b>" . $days2 . ":</b> " . $tooltipresult_2days . " - <b>" . $days3 . ":</b> " . $tooltipresult_3days;
echo "
<!-- Loop #" . $b . " --> <td><a href='#'";
?> onMouseover="ddrivetip('<?php
echo $tooltip;
?>')"; onMouseout="hideddrivetip()"> <?php
echo $realfname . " " . $reallname . "</a></td>";
echo '<td>
<select name="status' . $b . '">
' . $presentpunctual . '
' . $presenttardy . '
' . $excusedabsense . '
' . $unexcusedabsense . '
</select>
' . $hiddenfield . '
<input type="hidden" name="i" value="' . $b . '" />
<input type="hidden" name="studentid' . $b . '" value="' . $studentrfid . '">
<input type="hidden" name="classid" value="' . $class_name . '"></td>
<td><input type="text" name="comments' . $b . '" size="40" /></td></tr>
<!-- End Loop -->';
}
}
}
It essentially prints out student name and a drop down of statuses (if attendance was taken that day, the status will be whatever is set in the database). The date, flag, and tooltip functions are extra additions. (Date is for previous days, tooltip shows previous attendance on hover)
This data is being executed through the following loop:
if (isset($_GET['update'])) {
mysql_query("UPDATE teacher_accounts SET attendance = '1' WHERE username = '$username'") or die(mysql_error());
$error = 0;
$limit = $_GET['i'];
$starter = 0;
$num = 0;
while ($starter < $limit) {
$num++;
$statusinc = "status" . $num;
$studentinc = "studentid" . $num;
$commentsinc = "comments" . $num;
$starter++;
$studentID = $_GET[$studentinc];
$status = $_GET[$statusinc];
$comments = $_GET[$commentsinc];
$date = date("m/d/Y");
$sql = "select * from student where id = '$studentID'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$classid = $_GET['classid'];
if (isset($_GET['dateedit'])) {
$date = $_GET['dateedit'];
$count = "select * from attendance_main where StudentID = '$studentID' AND date = '$date' AND classID='$classid'";
$cresult = mysql_query($count) or die(mysql_error());
if (mysql_num_rows($cresult) > 0) {
$sql = "UPDATE attendance_main SET status='$status',comments='$comments',date='$date',classID='$classid' where StudentID = '$studentID'";
} else {
$sql = "INSERT INTO attendance_main (StudentID,status,comments,date,classID) VALUES ('$studentID','$status','$comments','$date','$classid')";
}
if (mysql_query($sql)) {
$return = "<h3>Successfully updated the attendance.</h3>";
}
} else {
$count = "select * from attendance_main where StudentID = '$studentID' AND date = '$date' AND classID='$classid'";
$cresult = mysql_query($count) or die(mysql_error());
if (mysql_num_rows($cresult) > 0) {
$sql = "UPDATE attendance_main SET status='$status',comments='$comments',date='$date',classID='$classid' where StudentID = '$studentID'";
if (mysql_query($sql)) {
$return = "<h3>Successfully updated the attendance for " . $num . " students.</h3>";
}
} else {
$sql = "INSERT INTO attendance_main (StudentID,status,comments,date,classID) VALUES ('$studentID','$status','$comments','$date','$classid')";
if (mysql_query($sql)) {
$return = "<h3>Successfully inserted today's attendance for " . $num . " students.";
}
}
}
}
echo $return;
For some reason, data is sometimes not being inserted properly. For example, a teacher might submit attendance on 02/08/2011, for a specific class, and certain students might appear twice under that attendance. This shouldn't be the case according to the code, because it should first check if they exist and, if they do, update the record rather than insert.
I've also seen cases where records are randomly deleted altogether. When a teacher takes attendance, all statuses are automatically set to Present. However, when I searched records on a certain date in the database, 2 students were missing records (which isn't even possible unless its being deleted)
Anyone have any idea why this might happen? I've tried replicating it myself (by repeatedly submitting the form, refreshing the page after it's processed, etc, to no avail.)
Thank you for the help!
Your query that check if a record exists is looking for all 3. 1) $studentID, 2) $classid and 3) $classid However the UPDATE statement is just looking for $studentID.
I would suggest you create a PRIMARY KEY (or UNIQUE INDEX) on StudentID,date,classID, then use the MySql INSERT ON DUPLICATE KEY UPDATE...
INSERT INTO attendance_main (StudentID,status,comments,date,classID)
VALUES ('$studentID','$status','$comments','$date','$classid')
ON DUPLICATE KEY UPDATE
status = VALUES(status),
comments = VALUES(comments)
Don't forget to sanitize the database input by using mysql_real_escape_string for example $status = mysql_real_escape_string($_GET[$statusinc]);.