Hi, I'm hoping that someone can help me with my update script. I'm been playing around with php for a few months but I'm still a beginner so I apologize in advance if my code is hard to read, or fraught with errors.

I'm making an update script for my friend and it's just not working. I've created a private members area which works fine and now I'm trying to create a text box which has the old information already in it so that it's easy to modify.

Here are my problems:

1) The old info does not appear in the text box.

2) The script will not even post to the database.

It was actually posting before I made changes to it, but it would make an additional 2 blank duplicates and it would not edit the current entry - it would just make a new one.

Below is my code. I've been tweaking it for about 2 weeks now and I'm stuck.

<?php
require_once("connect.php");

// check session variable
  if (isset($_SESSION['valid_user'])) 
  {
      echo '<p>You are logged in as '.$_SESSION['valid_user'].'</p>';



// Gets the article_ID from link and sets it to a variable.  
$ArticleID = $_GET['article_ID']; $Content = $_GET['articles_content']; // Selects the fields that will be displayed $query="SELECT articles_ID,articles_content FROM articles WHERE articles_ID=".$_GET['articles_ID'].""; $result=mysql_query($query,$connect); ?> <html> <head> <title>Edit Article</title> </head> <body> <table> <form action="editarticles.php" method=GET>
<font size=2> <tr><td>Content</td><td><textarea name="articles_content" rows="3" cols="20"><?php echo $Content ?></textarea></td></tr> <tr><td><input type=submit name="submit" value="submit"></td> <td><input type=Reset name="reset" value="Reset"></td></tr> </font> </form> </table> <?php // This receives and handles the data generated from the form" //$edit_content=$_GET['articles_content']; $query="UPDATE articles SET articles_content='$articles_content' WHERE articles_ID=".$_GET['articles_ID'].""; $result=mysql_query($query,$connect); if ($_GET['submit'] == true) //This means the form HAS been submitted: { if (mysql_query($query,$connect)) { echo "Article has been edited\n"; echo "<a href='addarticles.php'>Click here to add another story</a>"; }else { echo "System error. Article was not edited\n"; } } mysql_close($connect); } // Not logged in else { echo '<p>You are not logged in.</p>'; echo '<p>Only logged in members may see this page.</p>'; } echo '<a href="authmain.php">Back to main page</a>'; ?>

A friend of mine told me that I need to put this line somewhere:

list($articles_content) = mysql_fetch_row($query);

Is this correct and if so why would I need it? I'm guessing that it would go right after my SELECT query.

Thanks.

    I've been thinking about what could be wrong and I might have figured out part of what's wrong.

    In the admin area it lists the articles but only as a link. So when I click on the "edit article" link, all it does is pass the article_ID, nothing else.

    Do I need to make another script that lists the articles_ID and the articles_content and pass it through to the editarticles script or can I just attach the articles_content via the link?

    I'm sure that I've made some coding errors but I think I'm missing a step as well.

      A friend of mine told me that I need to put this line somewhere:

      list($articles_content) = mysql_fetch_row($query);

      Is this correct and if so why would I need it? I'm guessing that it would go right after my SELECT query.

      Thanks.

      Kinda.

      You should use something like:

      $query="SELECT articles_ID,articles_content FROM articles WHERE articles_ID=".$_GET['articles_ID']."";
      $result=mysql_query($query,$connect);
      $row = mysql_fetch_array($result);
      

      This will give you an array of collums per your query. So you could retrieve the contents by accessing:

      $row['articles_ID'] (interchange for whatever collum you want)

      I've been thinking about what could be wrong and I might have figured out part of what's wrong.

      In the admin area it lists the articles but only as a link. So when I click on the "edit article" link, all it does is pass the article_ID, nothing else.

      Do I need to make another script that lists the articles_ID and the articles_content and pass it through to the editarticles script or can I just attach the articles_content via the link?

      I'm sure that I've made some coding errors but I think I'm missing a step as well.

      You should be able to just pass article_ID through your link and use that to run your mysql query. Then just pull articles_content from the db.

      HTH

        $query="UPDATE articles

        SET articles_content='$articles_content'
        WHERE articles_ID=".$_GET['articles_ID']."";

        // should read

        $query="UPDATE articles

        SET articles_content='$articles_oldcontent . $articles_content'
        WHERE articles_ID=".$_GET['articles_ID'].";

        This would input the old data before the new data or store the new data in a file by finding the old articles id maybe 64 so the new articles content would be 65

        Have Fun:p

          Thanks for replying guys. Seeing how it should be done makes me realize how flawed my logic is. 😕

          I'm going to modify the script with your corrections.

          Any idea why it posts 3 times when I submit the form? I haven't been able to spot the problem.

            I've tried to run the script after making the corrections you guys noted above and after changing a few other things I'm getting the following error:

            error - Parse error: parse error, unexpected T_STRING in C:\apache\htdocs\members\editarticles.php on line 64 (might be different...I edited a few things afterwards but the error is the same)

            This line is on line 64:

            echo "Article has been edited \n";

            Can you guys please take a look at the script again? I'm still not sure if I setup the form correctly and I don't know what's causing the error so I'm not able to test it out to see if it will update correctly.

            
            <?php
            require_once("connect.php");
            
            // check session variable
              if (isset($_SESSION['valid_user'])) 
              {
                  echo '<p>You are logged in as '.$_SESSION['valid_user'].'</p>';
            
            
            // Gets the article_ID from link and sets it to a variable.  
            $ArticleID = $_GET['article_ID']; $Content = $_GET['articles_content']; // Selects the fields that will be displayed $query="SELECT articles_ID,articles_content FROM articles WHERE articles_ID=".$_GET['articles_ID'].""; $result=mysql_query($query,$connect); $row = mysql_fetch_array($result); $OldContent = $_GET['articles_content']; ?> <html> <head> <title>Edit Article</title> </head> <body> <table> <form action="editarticles.php" method=GET>
            <font size=2> <tr><td>Content</td><td><textarea name="newarticles_content" rows="3" cols="20"><?php echo $OldContent ?></textarea></td></tr> <tr><td><input type=submit name="submit" value="submit"></td> <td><input type=Reset name="reset" value="Reset"></td></tr> </font> </form> </table> <?php //$NewContent = $_GET['newarticles_content']; // This receives and handles the data generated from the form" if ($_GET['submit'] == true) //This means the form HAS been submitted: { $query="UPDATE articles SET articles_content=".$_GET['newarticles_content']." WHERE articles_ID=".$_GET['articles_ID']."; $result=mysql_query($query,$connect); if (mysql_query($query,$connect)) { echo "Article has been edited \n"; echo "<a href='addarticles.php'>Click here to add another story</a>"; }else { echo "System error. Article was not edited\n"; } } mysql_close($connect); } // Not logged in else { echo '<p>You are not logged in.</p>'; echo '<p>Only logged in members may see this page.</p>'; } echo '<a href="authmain.php">Back to main page</a>'; ?>

              Look at the syntax highlightling. You're not closing your string in you query.

              $query="UPDATE articles 
                        SET articles_content=".$_GET['newarticles_content']." 
                        WHERE articles_ID=".$_GET['articles_ID']; 
              

              That should work

              [edit]
              I just noticed something else. You running the same query twice in a row.

              $result=mysql_query($query,$connect); 
              
              
                if (mysql_query($query,$connect)) { 
              
              //change to:
              
              $result=mysql_query($query,$connect); 
              
              
              if ($result) { doo stuff; }
              

              [/edit]

                Thanks. I really appreciate the help.

                I made the changes you posted above but now I'm getting this error when I click submit :

                Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\apache\htdocs\members\editarticles.php on line 18

                I'll just post the block of code that Line 18 is in.

                <?php
                require_once("connect.php");
                
                // check session variable
                  if (isset($_SESSION['valid_user'])) 
                  {
                      echo '<p>You are logged in as '.$_SESSION['valid_user'].'</p>';
                
                
                // Gets the article_ID from link and sets it to a variable.  
                //$ArticleID = $_GET['article_ID']; //$Content = $_GET['articles_content']; // Selects the fields that will be displayed $query="SELECT articles_ID,articles_content FROM articles WHERE articles_ID=".$_GET['articles_ID'].""; $result=mysql_query($query,$connect); list($articles_ID,$articles_content)=mysql_fetch_row($result); // LINE 18 $OldContent = $_GET['articles_content']; ?> The "list" line is causing the problem. I referenced some other code I wrote in the past and I've used this same exact code on another little script I made and it's fine. I double checked everything and the names match with what I have in the database. Any idea what's wrong? Also, when I go to the edit script, the current info still doesn't show up in the form. Is this related to the error I'm getting now?

                  Ok, it is t hat list line that's giving you problems. So lets try assigning the vars one at a time.

                  $query="SELECT articles_ID,articles_content FROM articles WHERE articles_ID=".$_GET['articles_ID']."";
                  $result=mysql_query($query,$connect);
                  $row=mysql_fetch_row($result);
                  $articlesID = $row[0];
                  $artices_content=$row[1];
                  
                  print_r($row);//this should print all the contents of the $row array for testing purposes.
                  

                  And yes, it is most likely that this is why your form fields aren't being filled in.

                    When I go to the editarticles page the print statement returns the following:

                    Array ( [0] => 98 [1] => This is only a test )

                    But when I type something in the form and press submit I get this error message again:
                    Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in C:\apache\htdocs\members\editarticles.php on line 18

                    So it's reading the article ID and article content but something is getting messed up along the way when I submit the changes.

                    Is it possible that I have the form in the wrong place? Maybe I should have it at the bottom of the page after all of the code?

                      I haven't been able to figure this out yet. Would it be possible to get some further help with this?

                        Write a Reply...