Reputation: 337
This is the code I have so far.
<?php
include 'connect.php';
include 'main.php';
$id = $_SESSION['id'];
$page = "page1.php";
$sql_chk = " select * from html where id = '$id' and lastpage = '$page' ";
$rs_chk = mysql_query($sql_chk);
$num_chk = mysql_num_rows($rs_chk);
if ($num_chk == 0) {
mysql_query("INSERT INTO `html` (`id`, `lastpage`) VALUES ('$id', '$page') ");
} else {
$sql = "UPDATE html SET lastpage='$page' WHERE id='$id' ";
mysql_query($sql) or die("MYSQL Query Failed : " . mysql_error());
}
?>
Basically it just inserts $page and $id into a table called html and stores the page that you're on. Now what I want to do is check if the data inside lastpage
on the database is equal to "page2.php" || "page3.php", etc... The reason for this is that when you first visit this page it will create the rows for you and add the data of the page and your user id. Now lets say you goto page2, I want it to update the lastpage
column to page2.php. But then if I go back to page1.php I don't want it to update again because I'm past that page and so on...
The two main issues:
Lets say I goto page1 and it sets lastpage
to page1 under my userid. Then I goto page2 where $page = "page2.php". It won't overwrite the data using this script.
I need this script to be able to check if the page is equal to page2.php and up to page10.php for example. If it is between page2 and page10 than it just won't update the data in the database. Although if it isn't equal to page2 than it will just go on with the script.
Does anybody have any idea how I would be able to do this?
I bet I am overthinking this and it is actually easier than I am thinking but I have been coding all day and I can't think clearly.
Page2.php
<?php
include 'connect.php';
include 'main.php';
$id = $_SESSION['id'];
$page = 2;
$sql_chk = " select * from html where id = '$id' and lastpage = '$page' ";
$rs_chk = mysql_query($sql_chk);
$num_chk = mysql_num_rows($rs_chk);
if ($num_chk == 0) {
mysql_query("INSERT INTO `html` (`id`, `lastpage`) VALUES ('$id', '$page') ");
} else {
$sql = "UPDATE html SET lastpage='$page' WHERE id='$id' and lastpage < $page";
mysql_query($sql) or die("MYSQL Query Failed : " . mysql_error());
}
$pageString = "page{$page}.php";
echo $pageString;
?>
Upvotes: 1
Views: 71
Reputation: 2992
Why not remove the "page2" and make it simply "2", and add the page bit after. Then you can do a numeric comparison.
"UPDATE html SET lastpage=$page WHERE id='$id' and lastpage < $page ";
Where $page is just the number.
Then wherever you would have compared 'page10.php' == 'page2.php'
or whatever, you can compare numbers, and if you really need to have the string just do this later:
$pageString = "page{$page}.php"
Upvotes: 2