Jump to content

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


Neoraven456

Recommended Posts

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:

 


<?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:

 


<?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> </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:

 


<?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!

Link to comment
Share on other sites

no error messages? Very first thing I notice is in your 2nd code block, you forgot a ; after

 

$id = $_POST['id']

 

Fixed the ;, still no luck =/

 

no error messages, I'm a PHP noob, how would I go about putting in messages to catch errors?  Should be a pretty simple design, I was hoping wouldn't be a need for a lot of troubleshooting xD

Link to comment
Share on other sites

<?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>
<tr>
	<td colspan=2>
		<p> </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>

 

If I understand what you are looking to accomplish the above code should work.

Link to comment
Share on other sites

I managed to get everything working this afternoon. As of now I'm unsure what the problem was, I just rebuilt the pages over again, going line by line to find where it failed. I think my syntax

 

$query = "select ... id='$id'";

 

was off at some point, but it's all fixed now =D Thank you for all the help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.