Echoing forum Id to new page to display content? - php

can someone please help me i am having problems creating my forum.
At the moment users can create posts, the post title is listed down the page and then the user is suppose to be able to click the title link and be taken to read_post.php and then this should take the user to another page where the post content can be viewed, i am trying to do this by echoing the forum post id but it doesnt seem to want to work, instead i get this error:
Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
can someonee please show me where im going wrong.
here is my sql function:
function read_forum() {
global $connection;
global $forum_id;
$query = "SELECT *
FROM ptb_forum, ptb_profiles
WHERE ptb_forum.id = $forum_id ";
$forum_set = mysql_query($query, $connection);
confirm_query($forum_set);
return $forum_set;
}
here is the link code that takes the user to read_post.php which suppose to echo the forum id and display the content for each individual post.
<?
$forum_set = get_forum();
while ($forum = mysql_fetch_array($forum_set)) {
?>
<div class="forumcase" id="forumcase">
<div class="pend-forum-content">
<?php echo "<strong>{$forum['title']}</strong> - Posted by {$user['first_name']}"; ?>
</div>
here's my code for read_post.php:
<?php
$page_title = "Read Post";
include('includes/header.php');
include ('includes/mod_login/login_form2.php'); ?>
<?php
confirm_logged_in();
if (isset ($_GET['frm'])) {
$forum_id = $_GET['frm'];
}
?>
<?php include('includes/copyrightbar.php'); ?>
<div class="modtitle">
<div class="modtitle-text">Messages Between <?php echo "{$forum['display_name']}"; ?> & You</div>
</div>
<div class="modcontent57">
<br /><br /><br/><br/>
<div class="forum">
<div class="forum-pic"><?php echo "<img src=\"data/photos/{$_SESSION['user_id']}/_default.jpg\" width=\"100\" height=\"100\" border=\"0\" align=\"right\" class=\"img-with-border-forum\" />";?>
</div>
<div class="message-links">
<strong><< Back to Forum
</div>
<br /><br /><br/><br/>
<?php
$datesent1 = $inbox['date_sent']; ?>
<?php
$forum_set = read_forum();
while ($forum = mysql_fetch_array($forum_set)) {
$prof_photo = "data/photos/{$message['user_id']}/_default.jpg";
$result = mysql_query("UPDATE ptb_forum SET ptb_forum.read_forum='1' WHERE ptb_forum.id='$forum_id'")
or die(mysql_error());
?>
<div class="message-date">
<?php echo "".date('D M jS, Y - g:ia', strtotime($message['date_sent'])).""; ?></div>
<div class="img-with-border-msg-read"><?php echo "<img width=\"60px\" height=\"60px\" src=\"{$prof_photo}\"><br />"; ?></div>
<div class="conversation-text">
<?php echo "<i>Conversations between you and </i>{$forum['display_name']}.<br /> "; ?></div>
<div class="message-content">
<?php echo "<strong>Message Subject: </strong><i>{$forum['subject']}</i>"; ?>
<br/>
<br/>
<br/>
<br/>
<?php echo "<strong>Message:<br/></strong></br ><i>{$forum['content']}</i>"; ?>
</div>
<div class="reply-box">
<? include ('message_reply.php'); ?>
</div>
<?php
}
?>
<br/>
<br/>
<br/>
</div>
</div>
<?php include('includes/footer.php'); ?>
</div>

You have an error in your query... Your parameter is not quoted...
$query = "SELECT *
FROM ptb_forum, ptb_profiles
WHERE ptb_forum.id = '$forum_id'";
However... I suggest that you refrain from using the mysql_ family of functions. They are deprecated and due to be removed from PHP in a future release. You should be using parameterized queries using MySQLi or PDO.
Also, global is evil. I've never had a need to use it in 10 years of PHP programming. Neither should you.

Related

Loading data from database with pagination in PHP PDO

I have a website developed using PHP PDO programming. In this website there is one page where the details of holiday packages is listed from database along with pagination. My problem is only 6 records from database is loading in the page, but the pagination shows more than one page. Actually I am using PDO for the first time and I can't manage this problem.
Here is my code
index.class (all database functions are written here)
<?php
include_once "class/common.php";
class INDEX {
var $doAction;
var $msg;
var $objScr;
var $objCommonFunc; // This is common function object.
var $rsltInfo; // A common result set used by this class in various queries.
var $rsltOp; // A common result set used by this class in various queries.
var $arrSysVar; // Array that stores the System Variable those are used in screen.
// Other variables
var $adminEmail;
var $arrMetaContent; // For storing content and meta information.
var $leftMenuId; // Recid of 'menugroup' table for managing leftbar.
var $rightMenuId; // Recid of 'menugroup' table for managing rightbar.
var $menuPos;
function getHolydays($rgnId,$start_from, $record_per_page){
$qrySel = "SELECT h.recid recid, h.regionid regionid, h.name name, h.subtitle subtitle, h.txtDays txtDays, h.txtNights txtNights, h.pricingfrom pricingfrom, h.thumb_img thumb_img
FROM holydays h";
if($rgnId!=0){ $qrySel.= " WHERE h.regionid=".$rgnId; }
$qrySel .=" LIMIT $start_from,$record_per_page";
$objDbResult = $this->rsltInfo->getResult($qrySel);
return $objDbResult;
}
function getHolydaysPagntnCount($rgnId){
$qrySel = "SELECT h.recid recid, h.regionid regionid, h.name name, h.subtitle subtitle, h.txtDays txtDays, h.txtNights txtNights, h.pricingfrom pricingfrom, h.thumb_img thumb_img
FROM holydays h";
if($rgnId!=0){ $qrySel.= " WHERE h.regionid=".$rgnId; }
//echo $qrySel ;
$objDbResult = mysql_query($qrySel);
return $objDbResult;
}
}
$objScr = new INDEX();
?>
international-holidays.php
<?php $active_menu = 'Holidays';
include_once("index.class.php");
include_once 'header.php';
$objScr = new INDEX();
//$region_name="";
//if($_REQUEST['mcId'])$mcId=$_REQUEST['mcId'];
$rgnId=0;
if($_REQUEST['rgnId'])$rgnId=$_REQUEST['rgnId'];
if($_REQUEST['pgId'])
$pgId=$_REQUEST['pgId'];
$start_from=!empty($_REQUEST['page'])?$_REQUEST['page']:0;
//echo $start_from;
$record_per_page=6;
$rsltrgn = $objScr->getRegionname();
$rsltsdrgn = $objScr->getRegionname();
$rsltholyDetails = $objScr->getHolydays($rgnId,$start_from, $record_per_page);
$holydetailscount = mysql_numrows($objScr->getHolydayscount());
$holydaycount = mysql_numrows($objScr->getHolydaysPagntnCount($rgnId));
$total_pages = ceil($holydaycount/$record_per_page);
$rsltbannerDets= $objScr->getbannerImg($rgnId);
$rowbannerdts = $rsltbannerDets->fetchAssoc();
?>
<div class="col-md-8 col-sm-12 col-8">
<div class="right">
<div class="pagination-wrp clearfix">
<nav class="nav-pagination">
<?php //Here is the code for pagination
if($total_pages>0){?>
<ul class="pagination">
<?php if($pgId>1){?>
<li class="active"> <?php $prvpages=$start_from-6; $prev = ($pgId - 1); ?>
<a href="international-holidays-details.php?<?php echo "page=".$prvpages."&rgnId=".$rgnId."&pgId=".$prev; ?>" aria-label="Previous">
<img src="images/left-arrow.svg" alt=""></a></li>
<?php }?>
<?php
if($pgId!="" && $pgId!=1){ $spage=$pgId-1; }else{ $spage=1; $pgId=1; }
$maxPages=5;
$maxDisplayPage = $pgId + $maxPages - 1;
if($total_pages-$spage<5){ $spage=$total_pages-5; }
if($spage<=0){ $spage=1; }
for($i=$spage;$i<=min($maxDisplayPage, $total_pages);$i++){
if($i==1){ $st=0; }
else{
$st = ($i - 1) * $record_per_page;
}
?>
<li <?php if($i==$pgId){?> class="active" <?php }?>>
<a href="international-holidays-details.php?<?php echo "page=".$st."&rgnId=".$rgnId."&pgId=".$i; ?>" >
<?php echo $i; ?></a>
</li>
<?php
}
?>
<li class="active"><?php $nextpages=$start_from+6; if($pgId<$total_pages){ $next = ($pgId + 1); ?>
<a href="international-holidays-details.php?<?php echo "page=".$nextpages."&rgnId=".$rgnId."&pgId=".$next; ?>" aria-label="Next">
<img src="images/right-arrow.svg" alt=""> </a></li>
<?php }?></ul><?php }?>
</nav>
</div>
<?php
if($holydetailscount > 0) {
while($rowhly = $rsltholyDetails->fetchAssoc())
{
//$catgryname=str_split("Hello",3);
?>
<div class="col-sm-6 wow fadeInUp">
//Here the holiday packages as shown in boxes.
<a href="international-holidays-details.php?holyId=<?php echo $rowhly['recid']; ?>" class="offer-thumb">
<div class="offer-img"><img src="../sysimages/origimages/<?php echo $rowhly['thumb_img']; ?>" class="w-100" alt=""></div>
<div class="details clearfix">
<div class="title"><?php echo $rowhly['name']; ?><span><?php echo $rowhly['subtitle']; ?> </span></div>
<div class="price"><span>aed <?php echo $rowhly['pricingfrom']; ?></span>
<?php echo $rowhly['txtDays']; ?> days <?php echo $rowhly['txtNights']; ?> night</div>
</div>
</a>
</div>
<?php } }?>
</div>
</div>
The same code works perfectly in other page. The thing is the pagination shows 5 pages, but when i click on any page for example 2, it doesn't load the 2nd page rather it shows the first page itself.
I didn't understand what is happening. This code has been developed by someone else and my task is to do some updates on this.
Can anyone please help me.. I really needed to get this done.
What about if you use this pluggin? In my case it was very helpfull.
https://mottie.github.io/tablesorter/beta-testing/example-pager-custom-controls.html
Or you can just do it manually using mysql.
Pagination using MySQL LIMIT, OFFSET

Issue with bootstrap grid and mysql response

I have an issue with bootstrap and creating a 4 column responsive grid from a mysql response.
The problem is that if the second mysql query has a variable number of results, it brakes the grid.
Here is my code (where the first query has 9 results and the second query has a variable number of results):
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result)) {?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $row['username'];
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
which gives me this:
enter image description here
instead of this (obviously with many 'Hello world'):
enter image description here
Any help greatly appreciated!
Bootstrap doesn't claim to do any kind of elegant bin-packing on panels with different sizes. You could do some programming or css work to make all your panels the same size.
If that doesn't work for your application, you're going to need a layout library that does bin-packing so these panels of different sizes align properly.
There are a few such libraries available as jQuery plugins.
In this, $row[username] is wrong as it should be $row['username'].
$b = "SELECT * FROM $table_presents WHERE bought_for='$row[username]' OR bought_for='' ORDER BY id";
BTW, I changed your code bit. Please Try this.
<?php
$a = "SELECT * FROM $table_users ORDER BY username";
$result = mysql_query($a);
?>
<div class="container">
<div class="row">
<?php while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
?>
<div class="col-xs-3" style="background-color:aqua;">
<?php echo $username;
$b = "SELECT * FROM $table_presents WHERE bought_for='$username' OR bought_for='' ORDER BY id";
$result_presents = mysql_query($b) or die(mysql_error());
while ($row_presents = mysql_fetch_array($result_presents)) {
?>
<div style="background-color:red;">
Hello world!
</div>
<?php }?>
</div>
<?php }?>
</div>
</div>
[NOTE: Users can inject your SQL commands. Use prepared statements and parameterized queries. For more info, click Prevent SQL Injections

How to display certain comments stored on a database

I've created a form for a comment box that I'm going to use on a couple of different pages. It sends the data to a table called "comments" in my database that has columns "id" "author" "body" "created" and "page_name". I'm new to php and I'm trying to figure out how to just display the comments that have the same "page_name" value as the current page.
To get the "page_name", I used this code in the form:
<input type="hidden" name="page_name" value="<?=$_SERVER['REQUEST_URI']?>" />
This is the code I'm building to display the comments:
<div id="comments">
<?php foreach($comments as $comment): ?>
<div class="comment" style="margin-bottom: 2em;">
<div class="author">
<b><?php echo htmlentities($comment->author); ?>:</b>
</div>
<div class="meta-info" style="font-size: 0.8em;">
<?php echo datetime_to_text($comment->created); ?>
</div>
<div class="body">
<?php echo strip_tags($comment->body, '<strong><em><p>'); ?>
</div>
</div>
<?php endforeach; ?>
<?php if(empty($comments)) { echo "No Comments."; } ?>
</div>
I'm just not sure how to tell the page to display only the comments that have the same "page_name" value as the page the user is on. Any help would be appreciated!
You have to query the data corresponding to the url like.
For database connection use the code below:
<?php
// Create connection
$db_con=mysql_connect('servername','db username','dbpassword');
if (!$db_con)
{
die('Could not connect: ' . mysql_error());
}
$connection_string=mysql_select_db('db name',$db_con);
?>
For query use the code:
$url=$_SERVER['REQUEST_URI'];
$query=mysql_query("select * from comments where page_name='".$url."'");
while($result=mysql_fetch_row($query))
{
echo $result[0]."<br>".$result[1]."<br>"; // echo all values that are needed for displaying. You can do the coding for displaying formatted values inside this loop.
}

Render data from PHP DB query in HTML

I am a newbie to PHP but trying to learn it to enhance my programming skillset
So far i have the following PHP code in my page to return some data from my Database:
<?php
//code missing to retrieve my data
while($row = mysql_fetch_array($result))
{
echo '$row['Name']';
}
mysql_close($connection);
?>
This is working in that I can see the names from my database displayed on screen. I have seen as well that I can include html in the echo to format the data. However if I have the html code like below in a jQuery accordion outside my PHP code in the page - how can I dynamically place the Names in the specific h3 tags - so the first name in my table is Joe so that comes back in [0] element of array - is there a way I can reference this from my html code outside the php?
<div id="accordion">
<h3>Joe</h3>
<div>
<p>
Some blurb about Joe
</p>
</div>
<h3>Jane</h3>
<div>
<p>
Some blurb about Jane
</p>
</div>
<h3>John</h3>
<div>
<p>
Some Blurb about John.
</p>
</div>
</div>
Try something like this:
<?php while($row = mysql_fetch_array($result)) { ?>
<h3><?php echo $row['name']; ?></h3>
<div>
<p>Some blurb about Joe</p>
</div>
<?php } ?>
I'm assuming 'Some blurb about Joe' would also have to be replaced by a field in the DB, which you can accomplish in the same manner as the name.
#Gert is correct - the original mysql API is deprecated and should not be used anymore. Look into mysqli or PDO, instead.
add your html in while loop like this
<?php
//code missing to retrieve my data
while($row = mysql_fetch_array($result))
{
?>
<h3><?php echo $row['Name']?></h3>
<div>
<p>
Some blurb about <?php echo $row['Name']?>
</p>
</div>
<?php
}
mysql_close($connection);
?>
Like this :
<div id="accordion">
<?php
while($row = mysql_fetch_array($result))
{
<h3><?php echo $row['Name'] ?></h3>
<div>
<p>
Some blurb about Joe
</p>
</div>
} ?>
</div>

update value from '0' to '1' on image click?

I'm new to php and mysql so sorry if i'm doing it wrong. i have a page on my site that lists the reviews that members give to other other users.
Basically i have approved and deleted in my database which means that after a user sends the review it has to be reviewed by the user before it gets displayed.
once the user clicks the approved image which is a tick it goes to approved_review.php and in their i have my sql code to update the value from 0 to 1 in my database.
It should work exactly the same for the delete but obviously instead of updating the approved column it will update deleted.
the code i have tried is not working i have been working on this for quite some time and can;t figure it out.
Can someone please tell me where i'm going wrong?
Heres the code:
<?php
$reviews_set = get_pending_reviews();
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<p> </p>
<div class="pending-review-content">
<?php
$date = $reviews['date_added'];
?>
<div class="prof-content-pend-reviews" id="reviews">
<div class="message_pic"><?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">
<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\" /></a>";?>
</div>
<div class="reviews-date"><? echo "$date"; ?></div>
<div class="reviews-from">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\">{$reviews['display_name']}"; ?>
</a> Wrote:
</div>
<div class="reviews-content">
<?php echo "{$reviews['content']}"; ?>
</div>
</div>
<div class="reviews-approve">
<img src="assets/img/icons/tick.png" width="30" height="25" /></div>
<div class="reviews-delete">
<img src="assets/img/icons/cross.png" width="30" height="25" />
</div>
<? } ?>
approved_review.php function:
<?
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
echo "<div class=\"infobox1\">review approved.</div>";
?>
Your approach seems logical. After you loop through your reviews, you click on the tick or delete pngs to update or delete.
So, in approved_review.php
<?php
//you are missing the connection to your mysql database...
$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);
$sql = "UPDATE `playtime`.`ptb_reviews` SET `approved` = '1' WHERE `ptb_reviews`.`id` =".$_SESSION['user_id']."";
//execute the mysql query
$r = mysql_query($sql);
if (!mysql_error())
{
echo "<div class=\"infobox1\">Review Approved.</div>";
}
?>
a little edit rrrfusco's post
// or die for details if mysql_query won't work correct
$r = mysql_query($sql) or die (mysql_error());

Categories