[SOLVED] Stuck =/ Need helping finding error in a blog updat

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Neoraven456
Forum Newbie
Posts: 3
Joined: Thu Jul 03, 2008 12:15 am

[SOLVED] Stuck =/ Need helping finding error in a blog updat

Post by Neoraven456 »

I'm creating a blog via PHP/MySQL. I have an add page working, and below I'm going to list three blocks of code that correspond to three pages in an update process. The first block lists all posts ordered by id, once a post is selected it points to the second page/script that should echo the rows into text fields for editing, and a third and final that should update the database and return a confirmation. I'm stuck between and 1 and 2, the list shows up, and when redirected the actual editing page is blank, and I can't seem to find the error.

This is my first post here, so I'm hoping I posted in the right section. Thanks!

Here is the first block that lists the posted blogs:

Code: Select all

 
 
<?php require ('sql_connect.php4');
 
sql_connect('nut_blog');
 
$query = "select * from blog ORDER BY id DESC";
?>
 
<html>
<head>
</head>
<style type="text/css">
  BODY { background: url(http://www.pickellnutrition.com/pics/mainback.jpg); background-repeat: no-repeat; overflow:hidden}
.style1 {font-family: Georgia, "Times New Roman", Times, serif}
</style>
 
 
<?php
If ($results = mysql_query ($query)) {
?>
    <table align="center" border="1" width="80%">
        <tr>
 
            <td colspan="2">
                <b><center>
                  Blog Edit Page
                </center></b>
            </td>
        </tr>
 
<?php
While ($row = mysql_fetch_array($results)) { 
        $title = $row['title'];
        $author = $row['author'];
        $id = $row['id'];
                $date = $row['date'];
        $entry = $row['entry'];
?>
  <tr>
        <td colspan="2">
            <table align="center" border="0" width="100%">
                <tr>
                    <td>
 
<b><?php echo $title ?></b> - Posted by: <b><?php echo $author; ?></b>
                </td>
                <td>
    <div align="right"><?php echo $date; ?></div>
 
            </td>
            </tr>
            </table>
            </td>
    </tr>
    <tr>
        <td colspan="2"><?php echo $entry; ?></td>
        </tr>
<tr>
    <td>
               <table align="center" width="200" border="0">
        <tr>
        <td>
                <form action="SimpleEdit.php" method="POST">
                <input type="hidden" name="id" value="<?= $id ?>">
                <input type="submit" name="submit" value="Edit">
                </form>
            </td>
            <td>
                <form action="delete.php" method="POST">
                <input type="hidden" name="id" value="<?= $id ?>">
                <input type="submit" name="submit" value="Delete">
                </form>
            </td>
                    </tr>
                </table>
            </td>
        </tr>
    <?php
    }
    ?>
    </table>
    <div align="center">
      <p>
        <?php
} else { 
die ("<p>Could not run query because: <b>" . mysql_error() . "</b></p>\n");
}
 
mysql_close(); 
?> 
    </div>
    </html>
 
Okay, so the first page works well. Does what it's supposed to do. Now the second block that is supposed to bring up all selected post from the first block into editable fields for updating:

Code: Select all

 
 
<?php require ('sql_connect.php4');
 
sql_connect('nut_blog');
 
$id = $_POST['id']
 
$query = "select * FROM blog WHERE id='$id'";
 
if ($results = mysql_query($query)) {
 
$row = mysql_fetch_array ($results){;
 
                $title = $row['title'];
        $author = $row['author'];
        $id = $row['id'];
                $date = $row['date'];
        $entry = $row['entry'];
 
?>
<html>
<head>
</head>
<style type="text/css">
  BODY { background: url(http://www.pickellnutrition.com/pics/mainback.jpg); background-repeat: no-repeat; overflow:hidden}
.style1 {font-family: Georgia, "Times New Roman", Times, serif}
</style>
 
 
<center>
<form action="SimpleEditSave.php" method="POST">
<table>
    <tr>
        <td>
            Entry Title:
        </td>
        <td>
            <input type="text" name="title" size="40" maxsize="100" value="<?= echo $title ?>" />
        </td>
        <td>
            User:
        </td>
        <td>
            <input type="text" name="title" size="20" maxsize="50" value="<?= echo $user ?>" />
        </td>
    </tr>
<?php
 
}
 
?>
    <tr>
        <td colspan=2>
            <p>&nbsp;</p>
            <p>Entry Text:
        </p></td>
  </tr>
    <tr>
        <td>
            <textarea name="entry" cols="100" rows="15"><?= echo $entry ?></textarea>
        </td>
    </tr>
    <tr>
        <td>
  <form action="SimpleEditSave.php" method="post">
                                 <input type="hidden" name="id" value="<?php echo $id ; ?>" />
                                 <input type="submit" name="submit" value="Edit" />
  </form>
        </td>
        <td>
          <form action="SimpleEditList.php" method="post">
                                 <input type="submit" name="submit" value="Cancel" />
          </form>
        </td>
    </tr>
</center>
</html>
 
That section doesn't work :( and so I have yet to be able to test the third section that updates the database but this is what I have:

Code: Select all

 
 
<?php require ('sql_connect.php4');
 
sql_connect('nut_blog');
 
$query = "select * from blog ORDER BY id DESC";
?>
 
<html>
<head>
</head>
<style type="text/css">
  BODY { background: url(http://www.pickellnutrition.com/pics/mainback.jpg); background-repeat: no-repeat; overflow:hidden}
.style1 {font-family: Georgia, "Times New Roman", Times, serif}
</style>
 
<?php
 
$title = $_POST['title'];
$entry = $_POST['entry'];
$author = $_POST['author'];
$id = $_POST['id'];
 
trim ($title);
trim ($entry);
trim ($author);
 
$title = addslashes ($title);
$entry = addslashes ($entry);
$author = addslashes ($author);
 
$query = "UPDATE blog SET title='$title', entry='$entry', author='$author' WHERE id='$id'";
 
$results = mysql_query ($query);
 
?>
<table>
<?php
if (mysql_affected_rows() == 1) { 
?>
<table>
    <tr>
        <td>
            Update Saved Successful
        </td>
    </tr>
<?php
 } else { 
?>
    <tr>
        <td>
            Update Failed
        </td>
    </tr>
 
<?php
} 
?>
 
    <tr>
        <td>
            <form action="simpleadmin.php" method="post">
            <input type="submit" name="submit" value="Ok" />
            </form>
        </td>
    </tr>
</table>
 
 
<?php
 
mysql_close(); //Closes our SQL session
 
?>
 
 
Okay so that's everything. Parts of this were taken from a guide a friend had directed me too, which I am obviously too inept to follow, and parts have been put together from what I do know of PHP. Thanks in advance!
Last edited by Neoraven456 on Thu Jul 03, 2008 10:34 pm, edited 1 time in total.
kilermedia
Forum Newbie
Posts: 7
Joined: Wed Jul 02, 2008 11:00 pm
Location: California, USA

Re: Stuck =/ Need helping finding error in a blog update scri

Post by kilermedia »

How are you linking to the second "page"? The way it's coded you should be linking to it like edit-blog.php?id=1 (using _GET) or POST-ing with a submit button or however you like.

From how it looks it should work. Pretty straight forward. I can't help much without knowing how you're using the files.

You really should sterilize your variables, by the way.

Code: Select all

$id = mysql_real_escape_string($_POST['id']);

*edited for clarity
Last edited by kilermedia on Fri Jul 04, 2008 3:37 am, edited 2 times in total.
Neoraven456
Forum Newbie
Posts: 3
Joined: Thu Jul 03, 2008 12:15 am

Re: Stuck =/ Need helping finding error in a blog update scri

Post by Neoraven456 »

kilermedia wrote:How are you linking to the second "page"? The way it's coded you should be linking to it like edit-blog.php?id=1

From how it looks it should work. Pretty straight forward. I can't help much without knowing how you're using the files.

You really should sterilize your variables, by the way.

Code: Select all

$id = mysql_real_escape_string($_POST['id']);
Oh by linking I simply mean on the first page, the edit button is a form that uses the POST method with a value of <?= $id => and is directed to the second page so it knows which entry is being edited.
Neoraven456
Forum Newbie
Posts: 3
Joined: Thu Jul 03, 2008 12:15 am

Re: Stuck =/ Need helping finding error in a blog update scri

Post by Neoraven456 »

All fixed and working. Thanks for all the help!
Post Reply