How do i change the URL id? - php

Hi how do i change the URL id if the id is above max id in the db?
If i get the id from a database and use it in a read more button to make people read the data in a new page with the id as different url query?
--The real question
How do i make sure that if there only are 4 news in the db that if you write etc
newsTest.php?id=5 in the browser the browser will not execute or go back to max page?
//read more button
Læs mere
<?php
include_once 'includes/db.php';
$sql1 = "SELECT COUNT(id) AS total FROM rock_news ";
$result1 = $dbCon->query($sql1);
$row1 = $result1->fetch_assoc();
$total_pages = $row1["total"];
$thisVar = $_GET['id'];
if($total_pages >= $thisVar){
echo "alt er godt";
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
// mod sqlinjection
$id = $dbCon->real_escape_string($id);
$sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;
$result = $dbCon->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_object();
$id = $row->id;
$heading = utf8_encode($row->heading);
$subheading = utf8_encode($row->subheading);
$description = utf8_encode($row->description);
$created = $row->created;
$author = utf8_encode($row->author);
$output .= $id . "<br>" . $heading . "<br>" . $description;
};
// udskriv output til bruger
echo $output;
};
} else {
$_GET['id'] = $total_pages ;
echo "nothing";
}
?>
I am very new to php

Check if total_pages is less than id then show show total_pages id.
<?php
include_once 'includes/db.php';
$sql1 = "SELECT COUNT(id) AS total FROM rock_news ";
$result1 = $dbCon->query($sql1);
$row1 = $result1->fetch_assoc();
if(isset($_GET['id']) && !empty($_GET['id'])){
$total_pages = $row1["total"];
$thisVar = $_GET['id'];
if($total_pages < $thisVar){
$thisVar=$total_pages
$id = $thisVar;
// mod sqlinjection
$id = $dbCon->real_escape_string($id);
$sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;

Related

PREV and NEXT using the GET function with PHP and MYSQL

I have a previous and next button, I do not know how to decrease and increase the id using the get function. Can anyone please help, thank you.
PREVIOUS BUTTON CODE
<?php
include('db.php');
//now execut Select statement on table
$title = $_GET['title'];
$SQL = mysqli_query($db, "
SELECT *
FROM imageverse
WHERE (
id = IFNULL((SELECT max(id) FROM imageverse WHERE id < id='".$title."'),0)
);"
);
while ($row = mysqli_fetch_array($SQL)) {
$date = $row['date'];
$title = $row['title'];
echo "<h3>Prev - <a href = 'bibleread_post1.php?title=" . $title . "'#top'>". $title ."</a></h3>";
}
// Close your database connection
mysqli_close($db_conx);
?>
NEXT BUTTON CODE
<?php
include('db.php');
//now execut Select statement on table
$title = $_GET['title'];
$SQL = mysqli_query($db, "
SELECT * FROM imageverse
WHERE (
id = IFNULL((SELECT min(id) FROM imageverse WHERE id > id='".$title."'),0)
);"
);
while ($row = mysqli_fetch_array($SQL)) {
$date = $row['date'];
$title = $row['title'];
echo "<h3><a href = 'bibleread_post1.php?title=" . $title . "'#top'>". $title ."</a> - Next</h3>";
}
// Close your database connection
mysqli_close($db_conx);
?>

mysql SELECT WHERE query inside elseif loop not doing anything and no error

I run my script ( see below ) but the place where industry & keywordrw vars are set it just doesn't do anything
The other queries where just the industry is set works fine
the other 2 that involve doing multiple LIKE queries on the keyword variable don't do anything
<?php
if (!isset($industry)) {
$industry = '';
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $keywordrw;
} elseif (!isset($keyword)) {
$industry = $_GET['industry'];
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE category LIKE '$industry' LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $industry;
} elseif (isset($keyword) && ($industry)) {
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') AND (category = '$industry') LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $keywordrw . '-' . $industry;
} else {
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
//echo $keywordrw.'-'.$industry;
}
?>
Changed a few things and updated working code is below
if(!isset($industry))
{
$industry = '';
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE title LIKE '%keywordrw%'",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} elseif(!isset($keyword)) {
$industry = $_GET['industry'];
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE category LIKE '$industry' LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} elseif(isset($keyword) &&($industry)){
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') AND (category = '$industry') LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} else {
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
//echo $keywordrw.'-'.$industry;
}

SQL statement only runs when page is refreshed, not on initial view

So I have attached a few view counters to pages on my website, and one of the just will not work. It only updates the attribute 'views' when the page is refreshed, not on the initial load for the user. This is of course a problem because I cannot track page views if the user must refresh each time they view an album.
Here is my code:
$sql = "SELECT * FROM albums WHERE path='" . $albumPath . "'";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
$albumName = $row['name'];
$views = $row['views'];
$id = $row['id'];
}
$views = intval($views) + 1;
$sql = "UPDATE albums SET views='9' WHERE id='" . $id . "'";
$result = mysql_query($result);
Thank you for the help.
Looks like your last line should be $result = mysql_query($sql); instead of mysql_query($result). Also, on your second to last line, don't you mean to set views equal to $views instead of 9?
$sql = "SELECT * FROM albums WHERE path='" . $albumPath . "'";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
$albumName = $row['name'];
$views = $row['views'];
$id = $row['id'];
}
$views = intval($views) + 1;
$sql = "UPDATE albums SET views='$views' WHERE id='" . $id . "'";
$result = mysql_query($sql);

Track code on reset base on day,week,month

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);
}

How to show multiple items from mySQL database

Hi I am building a blog using html and php and have run into a problem with my sql. In my blog I would like to show all the comments that have been put in by users in the comments section that have the same article ID. In my database I am saving these parameters via $_POST and a query ID, ArticleID, Comments. However only the last comment that has been inserted in the database with that articleID is showing up.
this is the code that I am using. Can anyone help me please?
if(isset($_POST['submit']))
{
$comment = htmlentities($_POST["comment"]);
$articleID = $_GET['artId'];
$query = "INSERT INTO tbl_comments (comment, ArticleID) VALUES ('$comment', $articleID)";
$result = mysqli_query($connection, $query) or die("Error in query: ". mysqli_error($connection));
}
$query1 = "SELECT * FROM tbl_comments WHERE ArticleID = $artId";
$result1 = mysqli_query($connection, $query1) or die("Error in query: ". mysqli_error($connection));
while($row = mysqli_fetch_assoc($result1))
{
$articleId = $row['ArticleID'];
$comment = $row['comment'];
}
if(isset($comment))
{
echo "<div class='comments'>";
if (isset($comment))
{
echo "<div class='commentName'>";
echo $comment;
echo "</div>";
}
change
while($row = mysqli_fetch_assoc($result1))
{
$articleId = $row['ArticleID'];
$comment = $row['comment'];
}
to:
$comment = '';
while($row = mysqli_fetch_assoc($result1))
$comment .= $row['comment'] . '<br/>';

Categories