Can someone please explain to me why this code doesn't work correctly?

I know it's a bit messy, but I'm a newb!

The code displays data from a record in a form and allow the user to change it. It should, once the submit button is clicked, update the record with the new data. It runs through fine and never gives any errors but the record is not updated.

I think the problem could be than it's actually updating the record with the old data, but I'm not sure.

Can anyone spot my problem?

<?php
session_start();

if (!$_SESSION["valid_user"])
	{
	// User not logged in, redirect to login page
	Header("Location: login.php");
	}
include ("dbConfig.php");

$result = mysql_query("SELECT * FROM posts WHERE id='$id'");
$myrow = mysql_fetch_array($result);
$user = $myrow["user_id"];
$title = $myrow["title"];
$text = $myrow["text"];

?>
    <META name="description" content="ITComedy.com your complete tech humor web site!">
    <META name="keywords" content=", itcomedy, it comedy, tech humor,tech support, tech 

support humor, computer humor, tech humor, helpdesk,humor,
      tech support sories, computer tech humor, computer stories, computer, support humor, 

help desk, humor">
<title>ITCOMEDY.COM :: Tech Support Comedy</title>
<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" 

width="621" id="AutoNumber1">
    <tr>
      <td width="621" colspan="5"><?php
    virtual('title.htm');
  ?></td>
    </tr>
    <tr>
      <td width="467" colspan="3" rowspan="5" valign="top">
      <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" 

width="100%" id="AutoNumber3" height="7">
        <tr>
          <td width="100%" colspan="3" height="12"><font size="1">&nbsp;</font></td>
        </tr>
        <tr>
          <td width="100%" colspan="3" height="19">
          <img border="0" src="images/postmessage.jpg"></td>
        </tr>
        <tr>
          <td width="33%" height="1"></td>
          <td width="33%" height="1"></td>
          <td width="34%" height="1"></td>
        </tr>
        <tr>
          <td width="100%" height="19" colspan="3">
          <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: 

collapse" width="100%" id="AutoNumber4" height="1">
            <tr>
              <td width="1%" height="1"><font size="2">&nbsp;</font></td>
              <td width="33%" height="1"><font face="Verdana" size="2">&nbsp;</font></td>
              <td width="1%" height="1"><font size="2">&nbsp;</font></td>
              <td width="68%" height="1"><font face="Verdana" size="2">&nbsp;</font></td>
            </tr>
            <tr>
              <td width="103%" height="1" colspan="4"><font face=verdana size=2>

<?php

if ($submit) {
include ("dbConfig.php");
$query="UPDATE posts SET title_update='$title', text_update='$text' WHERE id='$id'";
mysql_query($query);
  echo "Thank you! Your changes have been saved.\n";

} else{
?>

<form method="post" action="<?php echo $PHP_SELF?>">

  Subject: &nbsp;&nbsp;&nbsp;<input type="Text" name="title_update" value="<?php echo 

"$title"; ?>"><br>

  <br>Message:<br> <textarea rows="10" name="text_update" cols="40"><?php echo "$text"; 

?></textarea><br><br>

  <input type="Submit" name="submit" value="Save Changes">

  </form>
<?php



} // end if



?>
</td>
            </tr>
            <tr>
              <td width="15%" height="1"><font size="1">&nbsp;</font></td>
              <td width="18%" height="1"><font size="1">&nbsp;</font></td>
              <td width="14%" height="1"><font size="1">&nbsp;</font></td>
              <td width="53%" height="1"><font size="1">&nbsp;</font></td>
            </tr>
          </table>
          </td>
        </tr>
        </table>
      </td>
      <td width="8">&nbsp;</td>
      <td width="146" rowspan="5" valign="top">
      <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" 

width="100%" id="AutoNumber2">
        <tr>
          <td width="100%"><font size="1">&nbsp;</font></td>
        </tr>
        <tr>
          <td width="100%"><img border="0" src="images/latestposts.jpg"></td>
        </tr>
        <tr>
          <td width="100%"><font size="2">&nbsp;</font></td>
        </tr>
        <tr>
          <td width="100%"><?php
    virtual('rightbar.php');
  ?></td>
        </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td width="8">&nbsp;</td>
    </tr>
    <tr>
      <td width="8">&nbsp;</td>
    </tr>
    <tr>
      <td width="8">&nbsp;</td>
    </tr>
    <tr>
      <td width="8">&nbsp;</td>
    </tr>
    <tr>
      <td width="621" colspan="5">
      <img border="0" src="images/bottombar.jpg"></td>
    </tr>
    <tr>
      <td width="125">&nbsp;</td>
      <td width="124">&nbsp;</td>
      <td width="218">&nbsp;</td>
      <td width="8">&nbsp;</td>
      <td width="146">&nbsp;</td>
    </tr>
    <tr>
      <td width="621" colspan="5"><font face="Verdana">
      <p align="center"><font size="1">ITComedy.com (c) 2003
      <a href="mailto:Dave@itcomedy.com">Dave</a> all rights reserved. <br>
      Any and all trademarks acknowledged.</font></font></td>
    </tr>
  </table>
  </center>
</div>

    Yeah, it's updating with the old data. It doesn't look like you are using any of the submitted values anywhere on the script.

    Use $title_update = $_POST['title_update']; etc. anywhere before your UPDATE query and change $title etc. in that query to whatever variables you're storing your submitted form data in.

      Did I follow your instructions right? I don't think I have because it's not working yet! 😃

      <?php
      session_start();
      
      if (!$_SESSION["valid_user"])
          {
          // User not logged in, redirect to login page
          Header("Location: login.php");
          }
      include ("dbConfig.php");
      
      $result = mysql_query("SELECT * FROM posts WHERE id='$id'");
      $myrow = mysql_fetch_array($result);
      $user = $myrow["user_id"];
      $title = $myrow["title"];
      $text = $myrow["text"];
      
      ?>
          <META name="description" content="ITComedy.com your complete tech humor web site!">
          <META name="keywords" content=", itcomedy, it comedy, tech humor,tech support, tech
      
      support humor, computer humor, tech humor, helpdesk,humor,
            tech support sories, computer tech humor, computer stories, computer, support humor,
      
      help desk, humor">
      <title>ITCOMEDY.COM :: Tech Support Comedy</title>
      <div align="center">
        <center>
        <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse"
      
      width="621" id="AutoNumber1">
          <tr>
            <td width="621" colspan="5"><?php
          virtual('title.htm');
        ?></td>
          </tr>
          <tr>
            <td width="467" colspan="3" rowspan="5" valign="top">
            <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse"
      
      width="100%" id="AutoNumber3" height="7">
              <tr>
                <td width="100%" colspan="3" height="12"><font size="1">&nbsp;</font></td>
              </tr>
              <tr>
                <td width="100%" colspan="3" height="19">
                <img border="0" src="images/postmessage.jpg"></td>
              </tr>
              <tr>
                <td width="33%" height="1"></td>
                <td width="33%" height="1"></td>
                <td width="34%" height="1"></td>
              </tr>
              <tr>
                <td width="100%" height="19" colspan="3">
                <table border="0" cellpadding="0" cellspacing="0" style="border-collapse:
      
      collapse" width="100%" id="AutoNumber4" height="1">
                  <tr>
                    <td width="1%" height="1"><font size="2">&nbsp;</font></td>
                    <td width="33%" height="1"><font face="Verdana" size="2">&nbsp;</font></td>
                    <td width="1%" height="1"><font size="2">&nbsp;</font></td>
                    <td width="68%" height="1"><font face="Verdana" size="2">&nbsp;</font></td>
                  </tr>
                  <tr>
                    <td width="103%" height="1" colspan="4"><font face=verdana size=2>
      
      <?php
      
      if ($submit) {
      $title_update = $_POST['title_update'];
      $text_update = $_POST['text_update'];
      
      $query="UPDATE posts SET title='$title_update', text='$text_update' WHERE id='$id'";
      mysql_query($query);
      
      echo "Thank you! Your changes have been saved.\n";
      
      } else{
      ?>
      
      <form method="post" action="<?php echo $PHP_SELF?>">
      
        Subject: &nbsp;&nbsp;&nbsp;<input type="Text" name="title_update" value="<?php echo 
      
      "$title"; ?>"><br>
      
        <br>Message:<br> <textarea rows="10" name="text_update" cols="40"><?php echo "$text"; 
      
      ?></textarea><br><br>
      
        <input type="Submit" name="submit" value="Save Changes">
      
        </form>
      <?php
      
      
      
      } // end if
      
      
      
      ?>
      </td>
                  </tr>
                  <tr>
                    <td width="15%" height="1"><font size="1">&nbsp;</font></td>
                    <td width="18%" height="1"><font size="1">&nbsp;</font></td>
                    <td width="14%" height="1"><font size="1">&nbsp;</font></td>
                    <td width="53%" height="1"><font size="1">&nbsp;</font></td>
                  </tr>
                </table>
                </td>
              </tr>
              </table>
            </td>
            <td width="8">&nbsp;</td>
            <td width="146" rowspan="5" valign="top">
            <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse"
      
      width="100%" id="AutoNumber2">
              <tr>
                <td width="100%"><font size="1">&nbsp;</font></td>
              </tr>
              <tr>
                <td width="100%"><img border="0" src="images/latestposts.jpg"></td>
              </tr>
              <tr>
                <td width="100%"><font size="2">&nbsp;</font></td>
              </tr>
              <tr>
                <td width="100%"><?php
          virtual('rightbar.php');
        ?></td>
              </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td width="8">&nbsp;</td>
          </tr>
          <tr>
            <td width="8">&nbsp;</td>
          </tr>
          <tr>
            <td width="8">&nbsp;</td>
          </tr>
          <tr>
            <td width="8">&nbsp;</td>
          </tr>
          <tr>
            <td width="621" colspan="5">
            <img border="0" src="images/bottombar.jpg"></td>
          </tr>
          <tr>
            <td width="125">&nbsp;</td>
            <td width="124">&nbsp;</td>
            <td width="218">&nbsp;</td>
            <td width="8">&nbsp;</td>
            <td width="146">&nbsp;</td>
          </tr>
          <tr>
            <td width="621" colspan="5"><font face="Verdana">
            <p align="center"><font size="1">ITComedy.com (c) 2003
            <a href="mailto<img src="images/smilies/biggrin.gif" border="0" alt="">ave@itcomedy.com">Dave</a> all rights reserved. 
      
      <br>
            Any and all trademarks acknowledged.</font></font></td>
          </tr>
        </table>
        </center>
      </div>
      

        You're almost there. All you need now is to define $submit.

        $submit = $_POST['submit'];

        And make sure it's set before your "if ($submit)" statement.

          I'm really greatful for the help but I'm still having the same problem. The database isn't updating at all.

            Change this:

            $query="UPDATE posts SET title='$title_update', text='$text_update' WHERE id='$id'";
            mysql_query($query); 
            

            to this:

            $query="UPDATE posts SET title='$title_update', text='$text_update' WHERE id='$id'";
            echo $query;
            mysql_query($query)
              or die("MySQL Error".mysql_error());
            

            And then we can see exactly what's wrong with it. Right now nothing jumps out at me.

              I get this as an output:

              UPDATE posts SET title='Stone Age Tech Support FDHGHGFHGF', text='The tech support problem dates back to long before the industrial revolution, when primitive tribesmen beat out a rhythm on drums to communicate: Groog (tech support): This fire help. Me Groog. Lorto: Me Lorto. Help. Fire not work. Groog: You have flint and stone? Lorto: Ugh. Groog: You hit them together? Lorto: Ugh. Groog: What happen? Lorto: Fire not work. Groog: (sigh) Make spark? Lorto: No spark, no fire, me confused. Fire work yesterday. Groog: sigh You change rock? Lorto: I change nothing. Groog: You sure? Lorto: Me make one change. Stone hot so me soak in stream so stone not burn Lorto hand. Small change, shouldn\'t keep Lorto from make fire. Groog: Grabs club and goes to Lorto\'s cave' WHERE id=''Thank you! Your changes have been saved.

              The only thing which sticks out at the is the WHERE id at the end.

                So the question that arises is "where does $id get its value from?"

                  The id comes from the URL, I did notice though that when the user clicks submit the id is no longer in the URL.

                  Is there a way to store it when the page "refreshes" after submit is clicked?

                    Everything from one request is forgotten by the time the next is made. That's just the way http is.

                    Well, you could put the id number in a hidden form field, so that it gets sent back along with the others.

                    There is a problem that (through accident or malice) the id number might get mangled before it gets back to the server - in which case the wrong post is updated or no post is found. If that could be a concern to you, you might want to see what PHP offers in the way of "sessions".

                      IT BLOODY WORKS!!!

                      Thanks guys I've been working on that for weeks!!!

                        Write a Reply...