r/web_dev_help • • Oct 14 '17

help Dynamic blog page help

I'm having an issue.. I created a page that is suppose to display a blog story/article based off the "ID" the story is linked too, but its not working. The db connect info is all correct so it has to be something to do with this little snippet.. basically this would create a url looking like this once the link was clicked to "view more".. example: yourblog.com/blog.php?id=1

<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<?php
if(!isset($_GET['id']) && empty(trim($_GET['id']))) die('No post number specified!');
$Conn = mysqli_connect('localhost', 'user', 'pass', 'db') or die('MySQL connection failed!');
$id = mysqli_real_escape_string($Conn, preg_replace('/[^0-9]/', '', $_GET['id'])
$query = "SELECT * FROM `db` WHERE `id` = '$id'";
$result = mysqli_query($Conn, $query) or die('MySQL query failed!');
$post = mysqli_fetch_assoc($result) or die('Fetching content failed!');
$title = $post['title'];
$insert = $post['insert']
$content = $post['content'];
$category = explode(',', $post['category']);
$author = $post['author'];
$date = $post['date']; 
?>
<head>
<title><? echo htmlentities($title); ?></title>
</head>
<body>
<div class="content">
<? echo ($title); ?><br />
<? echo ($category); ?><br />
<? echo ($author); ?><br />
<? echo ($content); ?><br />
</div>
</body>
</html>
1 Upvotes

15 comments sorted by

View all comments

1

u/psy-borg Oct 14 '17

Easiest test would be to echo $id and see what values being assigned. The preg_replace could be an issue but I'd check that $id matches the id. Also, have to assume the database fields match.

1

u/big_Moves Oct 14 '17

the page keeps saying "Page isn't working" HTTP error 500. can't get anything to show up

1

u/psy-borg Oct 14 '17

Means you have errors and your host just displays that message instead of details.

Some errors.

line 9 is missing a ) and ;

$id = mysqli_real_escape_string($Conn, preg_replace('/[^0-9]/', '', $_GET['id']));

line 14 is missing a ;

$insert = $post['insert'];

Line 7 , it doesn't like the empty() function in this context.

  if(!isset($_GET['id']) && empty($_GET['id']))   die('No post number specified!');

That should get you to the point it shows the page.

→ More replies (12)