Help - Search - Members - Calendar
Full Version: [resolved] part 6 edit entries - invalid entry ID problem
Codegrrl.com Forums > Script Help > Tutorial Help
plaidmelon
First, this is a great tutorial. Everything has been working great until now.

I stripped out all of my modifications to the code, but am still receiving the invalid entry ID after hitting the update button

CODE
<?php
include("config.php");

mysql_connect ($dbhost, $dbuser, $dbpass) or die ('Could not connect to the database because ' . mysql_error());
mysql_select_db ($dbname) or die ('Could not select database');

if (isset($_POST['update'])) {

    $id = htmlspecialchars(strip_tags($_POST['id']));
    $month = htmlspecialchars(strip_tags($_POST['month']));
    $date = htmlspecialchars(strip_tags($_POST['date']));
    $year = htmlspecialchars(strip_tags($_POST['year']));
    $time = htmlspecialchars(strip_tags($_POST['time']));
    $entry = $_POST['entry'];
    $title = htmlspecialchars(strip_tags($_POST['title']));
    

    $entry = nl2br($entry);

    if (!get_magic_quotes_gpc()) {
        $title = addslashes($title);
        $entry = addslashes($entry);
    }

    $timestamp = strtotime ($month . " " . $date . " " . $year . " " . $time);

    $result = mysql_query("UPDATE blog SET timestamp='$timestamp', title='$title', entry='$entry' WHERE id='$id' LIMIT 1") or print ("Can't update entry.<br />" . mysql_error());

    header("Location: singlepost.php?id=" . $id);

}
if (!isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid entry ID.");
}
else {
    $id = (int)$_GET['id'];
}

$result = mysql_query ("SELECT * FROM blog WHERE id='$id'") or print ("Can't select entry.<br />" . $sql . "<br />" . mysql_error());

while ($row = mysql_fetch_array($result)) {
    $old_timestamp = $row['timestamp'];
    $old_title = stripslashes($row['title']);
    $old_entry = stripslashes($row['entry']);

    $old_title = str_replace('"','\'',$old_title);
    $old_entry = str_replace('<br />', '', $old_entry);

    $old_month = date("F",$old_timestamp);
    $old_date = date("d",$old_timestamp);
    $old_year = date("Y",$old_timestamp);
    $old_time = date("H:i",$old_timestamp);
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<p><input type="hidden" name="id" value="<?php echo $id; ?>" />

<strong><label for="month">Date (month, day, year):</label></strong>

<select name="month" id="month">
<option value="<?php echo $old_month; ?>"><?php echo $old_month; ?></option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>

<input type="text" name="date" id="date" size="2" value="<?php echo $old_date; ?>" />

<select name="year" id="year">
<option value="<?php echo $old_year; ?>"><?php echo $old_year; ?></option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2008</option>
<option value="2010">2010</option>
</select>

<strong><label for="time">Time:</label></strong> <input type="text" name="time" id="time" size="5" value="<?php echo $old_time; ?>" /></p>

<p><strong><label for="title">Title:</label></strong> <input type="text" name="title" id="title" value="<?php echo $old_title; ?>" size="40" /> </p>



<p><textarea cols="80" rows="20" name="entry" id="entry"><?php echo $old_entry; ?></textarea></p>

<p><input type="submit" name="update" id="update" value="Update"></p>

</form>

<?php

mysql_close();
?>


The entry is getting updated correctly, but the page just displays the "Invalid entry ID" from
CODE
if (!isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid entry ID.");
}
else {
    $id = (int)$_GET['id'];
}



I can't seem to find what is causing it to not continue the way I think it is supposed to work.
Any guidance would be greatly appreciated.

Apologies for the excessive post length.
Amelie
You're getting that error on the singlepost.php page (the update page redirects there)... Basically it thinks there is no blog entry with that ID. Can you post the code for that?

The invalid ID message for the update page only appears if you forget to add the entry you want to update onto the end, e.g. going directly to update.php instead of update.php?id=1.
plaidmelon
I may have tracked down the problem. Thanks for the reply.

The script works fine as long as I don't include my header file or stylesheet. Somewhere I think the <div id="column"> in my header is throwing it off. All the other portions of the tutorial are working fine with it, except for this one. If I can figure out why I'll post again in case anyone else is having this problem.
plaidmelon
Well, I got it to workaround the invalid ID - sort of...

the entry was being updated, but the page would only display Invalid ID after, so

I changed this line
CODE
header("Location: singlepost.php?id=" . $id);


to

CODE
print "<meta HTTP-EQUIV=\"REFRESH\" CONTENT=\"0; URL=singlepost.php?id=$id\">";


Now it flashes Invalid ID for a second, then loads to singlepost.php?id=# correctly.
A bit ugly, but it works for now.
Amelie
That's odd... Are you on a Windows server by any chance? I've heard that the header("location..") code doesn't work properly on there sometimes.
plaidmelon
No, it's a unix server. I'm not sure what flavor though.
Amelie
Hmmm. Odd. Oh well, at least you got it working. smile.gif
sam adams
i know this is ages late, but i have just done the tutorial, and the answer i found to the problem with the header file breaking the refresh on the update is to place the header code after the php section, and before the form/html section in the update script. Now it works fine, and still displays my header files.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.