Jump to content

linking variable to database


elviapw

Recommended Posts

I've made a blog where users can post and make comments on each others' posts. I'd like to make it possible to click on the name of the person who made a post in order to see ALL the posts they have made. In order to do this, my plan was to pass the name of the poster through the URL to the page "personal.php", and then display the posts WHERE the name of the poster matches the name in the field 'Human' in my mysql table. I think I  must be going about this all wrong. Help!

 

Here is the php code for the page so far (it includes a maximum of 10 rows per page stipulation):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>

<div style='border-style:solid;
border-width:1px;
width:100px;
bordercolor:white;
cellpadding=:20;
position: absolute;
top: 20px;
left:200px;
width:800px;
text-align: left'>


<?php

$dbh = mysqli_connect("", "", "", "");

 

$pagenum = $_GET['pagenum'];

 

if (!(isset($pagenum)))

{

$pagenum = 1;

}

 

 

$sql = "SELECT * FROM Posts";

 

 

$query = mysqli_query($dbh,$sql);

 

 

while($feed = mysqli_fetch_assoc($query))

{

 

$title = $feed['Title'];

$post = $feed['Content'];

$date = $feed['Date'];

$id = $feed['id'];

$human = $feed['Human'];

 

$pic = $feed['image_url'];

$pic2 = $feed['image_url2'];

$pic3 = $feed['image_url3'];

 

$personal = $_GET['human'];

 

echo "personal $personal$personal$personal/$human";

 

 

$sql3= "SELECT * FROM Posts ORDER BY id DESC $max";

 

$query3 = mysqli_query($dbh,$sql3);

 

$rows = mysqli_num_rows($query3);

 

$page_rows = 10;

$last = ceil($rows/$page_rows);

 

if ($pagenum < 1)

{

$pagenum = 1;

}

elseif ($pagenum > $last)

{

$pagenum = $last;

}

 

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

 

echo "<a style='text-decoration:none' href='personal.php?human=$human'><font style='font-style:italic;font-size:11px' face='Times' align='left'>$human</font></a><br/>";

echo "<font  style='font-size:11px' face='Times' align='left'>$date</font><br/><br/>";

echo "<font face='Georgia, Times New Roman, Times, serif' color='#999999'>$title</font><br/>\n";

echo " <p>$post</p><br/>\n";

 

if ( !empty($feed['image_url']) ) {

echo "<img src= '$pic'/><br/><br/>";

} else {

echo " ";

 

if ( !empty($feed['image_url2']) ) {

echo "<img src= '$pic2'/><br/><br/>";

} else {

echo " ";

}

 

if ( !empty($feed['image_url3']) ) {

echo "<img src= '$pic3'/><br/><br/>";

} else {

echo " ";

}

 

echo "<div align='right'>";

 

echo " <a style='text-decoration:none' href='comment1.php?id=$id&Title=$title&Content=$post&Date=$date&image_url=$pic&image_url2=$pic2&image_url3=$pic3&human=$human'><font color='#FFCC33' style='font-style:italic' face='Times'>comment on this post</font><br/></a>\n";

 

echo " <a  style='text-decoration:none' href='delete.php?id=$id&Title=$title&Content=$post&Date=$date&image_url=$pic&image_url2=$pic2&image_url3=$pic3&human=$human'><font color='#66FF99' style='font-style:italic' face='Times'>delete this post</font><br/></a>\n";

 

$sql4 = "SELECT comment_id FROM Comments WHERE comment_id=$id";

$query4 = mysqli_query($dbh,$sql4);

 

if($query4) {

 

 

 

$feed2 = mysqli_fetch_assoc($query4);

 

$commentid = $feed2['comment_id'];

 

 

if ( !empty($feed2['comment_id']) ) {

echo " <a  style='text-decoration:none' href='viewcomments.php?id=$id&Title=$title&Content=$post&Date=$date&image_url=$pic&image_url2=$pic2&image_url3=$pic3'><font color='#66FF99' style='font-style:italic' face='Times'> see comments on this post</font><br/></a>\n";

 

} else {

echo "no comments yet";

}

}

 

echo "</div><hr />";

 

}

 

echo "</center>";

echo "<font color='#FF6600'><center><br/>Page $pagenum of $last</center></font>";

 

if ($pagenum == 1)

{

}

else

{

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <center>  <<-First </center></a> ";

echo " ";

$previous = $pagenum-1;

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <center>  <-Previous </center></a> ";

}

 

if ($pagenum == $last)

{

}

else {

$next = $pagenum+1;

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'> <center> Next -> </center></a> ";

echo " ";

echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'> <center> Last ->> </center> </a> ";

}

 

 

echo"</body>

</html>"

 

?>

 

</body>

</html>

 

and here's the link to the page from the main page:

echo "<a style='text-decoration:none' href='personal.php?human=$human'><font style='font-style:italic;font-size:11px' face='Times' align='left'>$human</font></a><br/>";

Link to comment
https://forums.phpfreaks.com/topic/147840-linking-variable-to-database/
Share on other sites

the 1st div tag u have doesnt appear to be closed</div>, also I would use a userID instead of username as its possible to have users with the same name, if you use a unique autoincrement ID for the user you can link the blog posts and the user to the userID in the user table.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.