Page 1 of 1

help building a basic blog using php

Posted: Wed Jul 22, 2009 11:22 am
by taralee02
I actually have two I tried, but here is the first one.
I am not sure why it doesn’t show up on the page. It does post on my hosting server when I fill in the information, but it doesn’t show up on the page. I guess it’s not putting the database info on the web page and I am not really understanding what I am doing wrong. I just graduated and still kinda learning so. :-S The website is taraleedesigns.com/blog.php, or even maybe a better easier way to do this I am just taking it from an assignment we did in my PHP class. Thank you for any advice or help. :-)

blog.php code:

Code: Select all

 
<?php
 
$connectID = mysqli_connect("mysql8.000webhost.com", "a6964267_taralee", "*******", 
    "a6964267_taralee") or die ("connect-unable to connect to the database.");
 
if ($_GET['delete_id']) {
  $id = ($_GET['delete_id']);
 
    $success = mysqli_query ($connectID, "DELETE FROM ref_links WHERE id = $id")
    or die ("Unable to delete record from database");
  if ($success) { 
    print "Record successfully deleted"; 
    }
}
    
?>
    
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TaraLeeDesigns - Blog</title>
<!--the layoout and text stylesheets-->
<link href="3_col.css" type="text/css" rel="stylesheet" />
<link href="text_n_colors.css" type="text/css" rel="stylesheet" />
<!--<script type="text/javascript"  src="file:///C|/Documents and Settings/helgeson/My Documents/CIS152/layouts/javascript/minmax.js"></script>-->
<style type="text/css">
<!--
#apDiv1 {
    position:absolute;
    left:158px;
    top:71px;
    width:457px;
    height:420px;
    z-index:1;
}
.style1 {color: #f6bbd5}
.style3 {   font-size: xx-small
}
.style4 {font-family: Harrington}
-->
</style>
</head>
 
 
<body class="violet">
<div id="main_wrapper">
<div id="header">
    <div id="header_inner">
        <h1 class="style4"><a href="http://www.000webhost.com/" onClick="this.href='http://www.000webhost.com/186602.html'" target="_blank"><img src="http://www.000webhost.com/images/banners/728x90/banner5.gif" alt="Web Hosting" width="728" height="90" border="0" /></a><meta name="keywords" content="taraleedesigns, tara lee, tara degenstein, web developement, tech support, north dakota, bismarck, web design" />
<meta name="description" content="This site is a portfolio. Has information about me and what I have done. I recently graduated college with an AAS..." />
</h1>
        <h1 class="style4">Tara Lee Designs </h1>
    </div>
    <!-- end header_inner -->
</div>
<!-- end header -->
 
<div id="nav">
    <div id="nav_inner">
        <ul>
            <li><a href="index.php">Home</a></li>
            <li><a href="portfolio.php">Portfolio</a></li>
            <li><a href="made.php">Pages
                Made</a></li>
            <li><a href="fav.php">Fav.
                Pages</a> <a href="file:///C|/Users/Tara/Documents/Site%20Design/Mystic%20Flower/Mystic%20Flower/contact.php"></a></li>
            <li><a href="find.php">Find
                Me</a></li>
            <li><a href="resume.php">Resume</a></li>
            <li><a href="blog.php">Blog</a></li>
            <li><a href="contact.php">Contact
                Me</a></li>
            <li><a href="map.php">Site
                Map </a></li>
            <li></li>
      </ul>
    </div>
    <!-- end nav_inner -->
</div><!-- end nav -->
<div id="content">
  <div id="content_inner">
    <p>You are here:
      <a href="blog.php">Blog</a></p>
    <p> Coming Soon!</p>
    <p><a href="add.php">Add new link</a><br />
      <a href="links.php">View listings page</a></p>
    <?php
if ($_GET['added']) print '<p style="color:red; font-weight: bold;">Record successfully created!</p>';
if ($_GET['updated']) print '<p style="color:red; font-weight: bold;">Record successfully updated!</p>';
 
//query the database
$myResult = mysqli_query($connectID, 'SELECT ref_links.*, ref_categories.category_name FROM ref_links, ref_categories WHERE ref_links.ref_categories_id=ref_categories.id')
  or die ("Unable to select from database");
//loop through the returned records
 
print '<table>'."\n";
while ($row = mysqli_fetch_array($myResult, MYSQL_ASSOC)) {
    //category heading only written out if a listing is in diff cat from previous listing
//$thisCat is used to test if a new heading needs to be written out
   $thisCat= $row['category_name'];
   if ($lastCat<>$thisCat) { // true first time($lastCat not set), and each time a new category is found
 //then write out a table heading with the category name
            print '<th colspan="6" align="left">'; //heading spans all columns of table
            print "<h3>".$row['category_name']."</h3>";  // print the next category heading
            print "</th>\n";
     }
     print '<tr>';
 
   print '<tr>'."\n";
     print '<td>'.$row['topic_name'].'</td>'."\n";
     print '<td class="description">'.$row['topic_desc'].'</td>'."\n";
     //Here I trim the displayed URL down to 30 chars (0 thru 30)
     //otherwise long URLs force the table to be very wide
     print '<td><a href="'.$row['topic_url'].'">'.(substr($row['topic_url'],0,29));
     //add ellipsis if URL is trimmed (> 30 chars)
     if ((strlen($row['topic_url']) > 30)) {print "&#8230;";}
    print'</a></td>';
     $id=$row['id'];
      print '<td><a href="add.php?modify_id='.$id.'">Edit</td>';
    print '<td><a href="';
    print ($_SERVER['PHP_SELF']);
    print '?delete_id='.$id.'">Delete</a></td>';
     print '</tr>'."\n";
     // finally, we store the current category so next time the loop repeats,
    // we can tell if the next item is in a new category or not
    // if so, we write out a new heading    - see if ($lastCat<>$thisCat) above
    $lastCat = $row['category_name'];
}
 
print '</table>'."\n";
?>
    <p>&nbsp;</p>
    <a href="add.php">Add new link</a><br />
    <a href="links.php">View listings page</a>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp; </p>
    <p><br />
        <script language="JavaScript" src="http://www.counter160.com/js.js?img=7" type="text/javascript"></script>
</p>
      <p>&nbsp;</p>
    </div>
</div>
 
<div id="promo">
    <div id="promo_inner">
    <p><a href="http://www.000webhost.com/" onClick="this.href='http://www.000webhost.com/186602.html'" target="_blank"><img src="http://www.000webhost.com/images/banners/120x120/banner2.gif" alt="Free Website Hosting" width="120" height="120" border="0" /></a></p>
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div><!-- end nav_inner -->
</div><!-- end nav -->
 
<div id="footer">
    <div id="footer_inner">
        <p><a href="index.php">Home</a> - <a href="portfolio.php">Portfolio</a> - <a href="made.php">PagesMade</a> - <a href="fav.php">Fav.Pages </a> - <a href="find.php">FindMe</a> - <a href="resume.php">Resume</a> - <a href="blog.php">Blog</a> - <a href="contact.php">ContactMe</a> - <a href="map.php">Map</a></p>
        <p>&nbsp;</p>
        <p class="style3">&copy; taraleedesigns
           Created by
          Tara Degenstein</p>
    </div>
</div>
 
</div></body>
</html>
<?php
// close the connection
mysqli_close($connectID);
?>
 
add.php code:

Code: Select all

 
<?php
 
$connectID = mysqli_connect("mysql8.000webhost.com", "a6964267_taralee,"*****", 
    "a6964267_taralee") or die ("connect-unable to connect to the database.");
 
    $category=($_POST['category']);
    $topic_name=($_POST['topic_name']);
    $topic_description=($_POST['topic_description']);
    $topic_url=($_POST['topic_url']);   
    $id = ($_GET['modify_id']);
if (($_POST['submitted']) && (!$_GET['modify_id'])) {
    // the user has submitted a new listing
  //write to database
    mysqli_query ($connectID, "INSERT into ref_links (ref_categories_id, topic_name, topic_desc, topic_url) VALUES ('$category', '$topic_name', '$topic_description', '$topic_url')")
  or die ("Unable to insert record into database");
  print "Record successfully added";
    if ($success) { 
    header ('Location: blog.php?added=1');
        }
} elseif  ((!$_POST['submitted']) && ($_GET['modify_id'])) {
       // the admin page has passed ID of record to be updated
             
                $this_Record = mysqli_query($connectID, "SELECT ref_links.*,ref_categories.category_name, ref_categories.id AS cat_id FROM ref_links, ref_categories WHERE ref_categories.id = ref_links.ref_categories_id AND ref_links.id = '$id'")
                or die ("Can't read the this record.");
$record_data = mysqli_fetch_array($this_Record, MYSQL_ASSOC);
 
} elseif  (($_POST['submitted']) && ($_GET['modify_id'])) {
       // the user has submitted the form to update a listing
             $success = mysqli_query("UPDATE ref_links SET ref_categories_id = '$category', topic_name = '$topic_name', topic_desc = '$topic_description', topic_url = '$topic_url' WHERE id='$id'", $connectID);
             if ($success) {
             header ('Location: blog.php?updated=1');
             }
} else {
// The user has loaded the page to enter a new listing
// do nothing - just let the page load
}
    
?>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TaraLeeDesigns - Blog</title>
<!--the layoout and text stylesheets-->
<link href="3_col.css" type="text/css" rel="stylesheet" />
<link href="text_n_colors.css" type="text/css" rel="stylesheet" />
<!--<script type="text/javascript"  src="file:///C|/Documents and Settings/helgeson/My Documents/CIS152/layouts/javascript/minmax.js"></script>-->
<style type="text/css">
<!--
#apDiv1 {
    position:absolute;
    left:158px;
    top:71px;
    width:457px;
    height:420px;
    z-index:1;
}
.style1 {color: #f6bbd5}
.style3 {   font-size: xx-small
}
.style4 {font-family: Harrington}
-->
</style>
</head>
 
 
<body class="violet">
<div id="main_wrapper">
<div id="header">
    <div id="header_inner">
        <h1 class="style4"><a href="http://www.000webhost.com/" onClick="this.href='http://www.000webhost.com/186602.html'" target="_blank"><img src="http://www.000webhost.com/images/banners/728x90/banner5.gif" alt="Web Hosting" width="728" height="90" border="0" /></a><meta name="keywords" content="taraleedesigns, tara lee, tara degenstein, web developement, tech support, north dakota, bismarck, web design" />
<meta name="description" content="This site is a portfolio. Has information about me and what I have done. I recently graduated college with an AAS..." />
</h1>
        <h1 class="style4">Tara Lee Designs </h1>
    </div>
    <!-- end header_inner -->
</div>
<!-- end header -->
 
<div id="nav">
    <div id="nav_inner">
        <ul>
            <li><a href="index.php">Home</a></li>
            <li><a href="portfolio.php">Portfolio</a></li>
            <li><a href="made.php">Pages
                Made</a></li>
            <li><a href="fav.php">Fav.
                Pages</a> <a href="file:///C|/Users/Tara/Documents/Site%20Design/Mystic%20Flower/Mystic%20Flower/contact.php"></a></li>
            <li><a href="find.php">Find
                Me</a></li>
            <li><a href="resume.php">Resume</a></li>
            <li><a href="blog.php">Blog</a></li>
            <li><a href="contact.php">Contact
                Me</a></li>
            <li><a href="map.php">Site
                Map </a></li>
            <li></li>
      </ul>
    </div>
    <!-- end nav_inner -->
</div><!-- end nav -->
<div id="content">
  <div id="content_inner">
    <p>You are here:
      <a href="blog.php">Blog</a></p>
      <h3>Add or modify a listing</h3>
      </br>
<a href="blog.php">Back
to Blog </a>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
 
<!--drop-down menu-->
<!--<label for="category">Topic Category</label><br/>-->
<?php 
//print '<select name="category">'."\n";
//print '<option value="none">Select a Catagory</option>'."\n";
$Categories = mysqli_query($connectID, 'SELECT * FROM ref_categories');while ($Category = mysqli_fetch_array($Categories, MYSQL_ASSOC)){
 
print   '<option value="'.$Category['id'].'"';
    if($record_data['cat_id']==$Category['id']) print " selected=\"selected\" "; 
print '>'.$Category['category_name']."</option>\n";
 
}
print '</select>'."<br />\n";
?><br/>
<!--topic_name-->
<label for="topic_name" >Topic Name</label><br/>
<input name="topic_name" type="text" size="30" id="topic_name" value="<?php print $record_data['topic_name'] ?>" /><br/>
<!--topic_description-->
<label for="topic_description">Topic Description</label><br/>
<textarea name="topic_description"><?php print $record_data['topic_desc'] ?></textarea>
<!--topic_url--><br/>
<label for="topic_url">Topic URL</label><br/>
<input name="topic_url" type="text" size="30" id="topic_url" value="<?php print $record_data['topic_url'] ?>" /><br/>
<input type="submit" value="Submit" name="submitted" />
</form>
</form>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp; </p>
    <p><br />
        <script language="JavaScript" src="http://www.counter160.com/js.js?img=7" type="text/javascript"></script>
</p>
      <p>&nbsp;</p>
    </div>
</div>
 
<div id="promo">
    <div id="promo_inner">
    <p><a href="http://www.000webhost.com/" onClick="this.href='http://www.000webhost.com/186602.html'" target="_blank"><img src="http://www.000webhost.com/images/banners/120x120/banner2.gif" alt="Free Website Hosting" width="120" height="120" border="0" /></a></p>
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div><!-- end nav_inner -->
</div><!-- end nav -->
 
<div id="footer">
    <div id="footer_inner">
        <p><a href="index.php">Home</a> - <a href="portfolio.php">Portfolio</a> - <a href="made.php">PagesMade</a> - <a href="fav.php">Fav.Pages </a> - <a href="find.php">FindMe</a> - <a href="resume.php">Resume</a> - <a href="blog.php">Blog</a> - <a href="contact.php">ContactMe</a> - <a href="map.php">Map</a></p>
        <p>&nbsp;</p>
        <p class="style3">&copy; taraleedesigns
           Created by
          Tara Degenstein</p>
    </div>
</div>
 
</div></body>
</html>
<?php
// close the connection
mysqli_close($connectID);
?>
 
links.php code:

Code: Select all

 
 
<?php
 
$connectID = mysqli_connect("mysql8.000webhost.com", "a6964267_taralee", "*********", 
    "a6964267_taralee") or die ("connect-unable to connect to the database.");
?>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TaraLeeDesigns - Blog</title>
<!--the layoout and text stylesheets-->
<link href="3_col.css" type="text/css" rel="stylesheet" />
<link href="text_n_colors.css" type="text/css" rel="stylesheet" />
<!--<script type="text/javascript"  src="file:///C|/Documents and Settings/helgeson/My Documents/CIS152/layouts/javascript/minmax.js"></script>-->
<style type="text/css">
<!--
#apDiv1 {
    position:absolute;
    left:158px;
    top:71px;
    width:457px;
    height:420px;
    z-index:1;
}
.style1 {color: #f6bbd5}
.style3 {   font-size: xx-small
}
.style4 {font-family: Harrington}
-->
</style>
</head>
 
 
<body class="violet">
<div id="main_wrapper">
<div id="header">
    <div id="header_inner">
        <h1 class="style4"><a href="http://www.000webhost.com/" onClick="this.href='http://www.000webhost.com/186602.html'" target="_blank"><img src="http://www.000webhost.com/images/banners/728x90/banner5.gif" alt="Web Hosting" width="728" height="90" border="0" /></a><meta name="keywords" content="taraleedesigns, tara lee, tara degenstein, web developement, tech support, north dakota, bismarck, web design" />
<meta name="description" content="This site is a portfolio. Has information about me and what I have done. I recently graduated college with an AAS..." />
</h1>
        <h1 class="style4">Tara Lee Designs </h1>
    </div>
    <!-- end header_inner -->
</div>
<!-- end header -->
 
<div id="nav">
    <div id="nav_inner">
        <ul>
            <li><a href="index.php">Home</a></li>
            <li><a href="portfolio.php">Portfolio</a></li>
            <li><a href="made.php">Pages
                Made</a></li>
            <li><a href="fav.php">Fav.
                Pages</a> <a href="file:///C|/Users/Tara/Documents/Site%20Design/Mystic%20Flower/Mystic%20Flower/contact.php"></a></li>
            <li><a href="find.php">Find
                Me</a></li>
            <li><a href="resume.php">Resume</a></li>
            <li><a href="blog.php">Blog</a></li>
            <li><a href="contact.php">Contact
                Me</a></li>
            <li><a href="map.php">Site
                Map </a></li>
            <li></li>
      </ul>
    </div>
    <!-- end nav_inner -->
</div><!-- end nav -->
<div id="content">
  <div id="content_inner">
    <p>You are here:
      <a href="blog.php">Blog</a></p>
    <p> <a href="add.php">Add
        new link</a><br />
      <a href="links_listing.php">View
      listings page</a></p>
    <?php 
$myResult = mysqli_query($connectID, 'SELECT ref_links.*, ref_categories.category_name FROM ref_links, ref_categories WHERE ref_links.ref_categories_id=ref_categories.id ORDER BY ref_categories_id, ref_links.topic_name')
  or die ("Unable to select from database");
 
    while ($row=mysqli_fetch_array($myResult, MYSQL_ASSOC)) {
  $thisCat= $row['category_name'];
    //print the category heading, but only once for the listings in that catagory
 if ($lastCat<>$thisCat) { // true first time($lastCat not set), and each time a new category is found
            print "<h2>".$row['category_name']."</h2>";  // print the next category heading
        }
        print '<div id="ref_links">';
        print "<h3>".$row['topic_name']."</h3>\n";
        print "<p>".$row['topic_desc']."</p>\n";
        print "<a href=".$row['topic_url']." target=\"_blank\">".$row['topic_url']."</a>\n";
        
      $lastCat = $row['category_name']; // record which category this listing was in so we can test if the cat. changes
      
    print "</div>\n";  //end of ref_links div
}
?>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp; </p>
    <p><br />
        <script language="JavaScript" src="http://www.counter160.com/js.js?img=7" type="text/javascript"></script>
</p>
      <p>&nbsp;</p>
    </div>
</div>
 
<div id="promo">
    <div id="promo_inner">
    <p><a href="http://www.000webhost.com/" onClick="this.href='http://www.000webhost.com/186602.html'" target="_blank"><img src="http://www.000webhost.com/images/banners/120x120/banner2.gif" alt="Free Website Hosting" width="120" height="120" border="0" /></a></p>
        <ul>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div><!-- end nav_inner -->
</div><!-- end nav -->
 
<div id="footer">
    <div id="footer_inner">
        <p><a href="index.php">Home</a> - <a href="portfolio.php">Portfolio</a> - <a href="made.php">PagesMade</a> - <a href="fav.php">Fav.Pages </a> - <a href="find.php">FindMe</a> - <a href="resume.php">Resume</a> - <a href="blog.php">Blog</a> - <a href="contact.php">ContactMe</a> - <a href="map.php">Map</a></p>
        <p>&nbsp;</p>
        <p class="style3">&copy; taraleedesigns
           Created by
          Tara Degenstein</p>
    </div>
</div>
 
</div></body>
</html>
<?php
// close the connection
mysqli_close($connectID);
?>

Re: help building a basic blog using php

Posted: Wed Jul 22, 2009 11:02 pm
by superdezign
You need to at least attempt to pinpoint the problem before asking us. The data is in the database, right? If so, then the problem would be in the display. Does your query get the data? If so, is the data not displaying or not displaying correctly? Do your loops run or not?

Debug your code by echoing all of the information that you can to pinpoint the problem. Make your program tell you what it is doing and when, and verify that the behavior is correct. Even if you think that you are *sure* something is working, test it and output it.