SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    \|/
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Me: Editing a text field via a form

    I have been going through Kevin's PHP MySQL book, and have come across something I can't quite figure out. Where you could create a joke database, you could also add a "Delete the joke" link by passing the joke id on and using the DELETE query. What I want to do is UPDATE the table via a link right under this link that allows you to edit the joke. You would have to pass the info back to the form so that it could be reupdated. Unfortunately I'm not too sure how to do this. This is what I have so far:

    PHP Code:

    <?php

    // Connect to the database server

    $dbcnx = @mysql_connect("localhost""user""pass");
        if (!
    $dbcnx) {
            echo(
    "<p>Unable to connect to the database server at this time.</p>");
        exit();
    }

    // Select the Test Database

    mysql_select_db("mulls007_testdb"$dbcnx);
        if (!@
    mysql_select_db("mulls007_testdb")) {
            echo(
    "<p>Unable to locate the "." Database at this time.</p>");
        exit ();
    }

    //If a joke has been submitted, add it to the database

    if ($submitjoke=="SUBMIT") {
        
    $sql "INSERT INTO Jokes SET JokeText='$joketext',JokeDate=CURDATE()";
        if (@
    mysql_query($sql)) {
                echo(
    "<p>Error adding submitted joke: "mysql_error() ." </p>");
            }
             
    // Redirect to prevent re-submit on reload
            
    header("Location: $PHP_SELF?insertok=1"); // **** NEW LINE ****
        
    }

     
    //If a joke has been deleted, delete it from the database

     
    if (isset($deletejoke)) {
         
    $sql "DELETE FROM Jokes WHERE ID=$deletejoke";
        if (@
    mysql_query($sql)) {
            echo(
    "<p>$deletejoke has been deleted.</p>");
        } else { 
                echo(
    "<p>Error deleting joke: "mysql_error() ." </p>");
        }

     }

     
    //If a joke has been edited, edit the database

     
    if (isset($editjoke)) {
         
    $sql "UPDATE Jokes SET JokeText=$editjoke";
        if (@
    mysql_query($sql)) {
            echo(
    "<p>$editjoke has been changed.</p>");
        } else { 
                echo(
    "<p>Error updating joke: "mysql_error() ." </p>");
        }

     }

    ?>

    <html>
    <head>
    <title>All of our Jokes</title>
    </head>
    <body>

    <?php
        
    if (isset($addjoke)): // If the user wants to add a joke
    ?>

    <form action="<?=$PHP_SELF?>" method="post">
    <p>Type your joke here:<br>
    <textarea name="joketext" rows="10" cols="40" wrap>
    </textarea><br>
    <input type="submit" name="submitjoke" value="SUBMIT">
    </p>
    </form>

    <?php
        
    if (isset($editjoke)): // If the user wants to edit a joke
    ?>

    <form action="<?=$PHP_SELF?>" method="post">
    <p>Type your joke here:<br>
    <textarea name="joketext" rows="10" cols="40" wrap> <?="$joketext"?>
    </textarea><br>
    <input type="hidden" name="id" value ="<?=$id?>">
    <input type="submit" name="submitjoke" value="SUBMIT">
    </p>
    </form>

    <?php
         
    else: // Default page display

    if (isset($insertok)) {
        echo (
    "<p>Your joke has been submitted.</p>");
    }

    echo (
    "<p>Here are all the jokes in our database: "." </p>");


    // Request the text and date of our jokes

    $result = @mysql_query("SELECT JokeText, JokeDate, ID, DATE_FORMAT(JokeDate, '%W, %b. %e, %Y') AS date_formatted FROM Jokes");
        if (!
    $result) {
            echo(
    "<p>Error performing query: "mysql_error() ."</p>");
        exit ();
    }

    // Display the text of each joke in a paragraph with the date next to it

    while ($row mysql_fetch_array($result)) {
        
    $joketext $row["JokeText"];
        
    $jokedate $row["date_formatted"];
        
    $jokeid $row["ID"];
        echo(
    "<p>

    <table width='400' border='0' cellspacing='0'>
      <tr bgcolor='#006699'> 
        <td><b><font color='#FFFFFF' size='2' face='Arial, Helvetica, sans-serif'>
    $joketext</font></b></td>
      </tr>
      <tr bgcolor='#CCCCCC'> 
        <td><font size='1' color='#FF0033' face='Arial, Helvetica, sans-serif'> 
    $jokedate </font><br>
        "
    ." <a href='$PHP_SELF?deletejoke=$jokeid'> "." Delete This Joke</a><br>
        "
    ." <a href='$PHP_SELF?editjoke=$jokeid'> "." Edit This Joke</a><br>
        </td>
      </tr>
    </table>

    </p>"
    );
    }

    // When clicked, this link will load this page with the joke submission form displayed

    echo("<p><a href='$PHP_SELF?addjoke=1'> "." Add a Joke! </a></p>");

        endif;

    ?>

    </body>
    </html>
    What's the problem here?
    Last edited by mulls; Mar 20, 2002 at 00:58.
    PeopleCards: The Official People Trading Card
    http://www.peoplecards.net

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    \|/
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, so i bagged trubg to get it to load with $PHP_SELF, and instead just created a new file called editjoke.php, and pass the ID along to it. It then loads a new page with all of the jokes submitetd already in the form fields, so it appears to be passing it on OK. The problem is they don't update in the database, even though I get a succesful message. here's the code:

    PHP Code:

    <?php

    // Connect to the database server

    $dbcnx = @mysql_connect("localhost""user""pass");
        if (!
    $dbcnx) {
            echo(
    "<p>Unable to connect to the database server at this time.</p>");
        exit();
    }

    // Select the Test Database

    mysql_select_db("mulls007_testdb"$dbcnx);
        if (!@
    mysql_select_db("mulls007_testdb")) {
            echo(
    "<p>Unable to locate the "." Database at this time.</p>");
        exit ();
    }


      if (
    $edit == "edit") {
       
       
    $sql= ("UPDATE Jokes SET JokeText = '$joketext' , JokeDate = '$jokedate' WHERE ID = '$id'");
       if (@
    mysql_query($sql)) {
           exit (
    "added"); }
       else { exit (
    "error<br>"); }
       
       } 

    // Request the text and date of our jokes

    $result = @mysql_query("SELECT JokeText, JokeDate, ID, DATE_FORMAT(JokeDate, '%W, %b. %e, %Y') AS date_formatted FROM Jokes");
        if (!
    $result) {
            echo(
    "<p>Error performing query: "mysql_error() ."</p>");
        exit ();
    }

    // Display the text of each joke in a paragraph with the date next to it

    while ($row mysql_fetch_array($result)) {
        
    $joketext $row["JokeText"];
        
    $jokedate $row["date_formatted"];
        
    $jokeid $row["ID"];
                
    ?>

    ID:<?=$id?>
    <form method="post" action="<?=$PHP_SELF?>">
    <input type="text" name="jokedate" value="<?=$jokedate?>" size="20" maxlength="50"><br>
    <input type="text" name="id" value="<?=$jokeid?>" size="20" maxlength="50"><br>
    <textarea name="message" cols="50" rows="10"> <?=$joketext?> </textarea><br>
    <input type="submit" name="edit" value="edit">
    </form>

    <?php 
     
    }
    ?>
    Argh! Any ideas?
    PeopleCards: The Official People Trading Card
    http://www.peoplecards.net

  3. #3
    Shiver me timbers!! anthony_irl's Avatar
    Join Date
    Aug 1999
    Location
    Dublin, Ireland
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing really obvious strikes me about the code but it could be the SQL query to update the database. I haven't seen kev's code in a while but I think the ID field is normally an INT. In your query you have done this:

    ID = '$id'

    I reckon that could be causing the problem as numeric values arent supposed to be enclosed by quotation marks. Try replacing the SQL query with this:

    PHP Code:
    UPDATE Jokes SET JokeText '$joketext' JokeDate '$jokedate' WHERE ID $id 
    Let me know how it goes.
    Anthony - How's tings?

    24 hours in a day, 24 beers in a case. Coincidence? I think not.
    Contact me by: PM Email NEW! Carrier Pigeon

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    \|/
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post weird

    I took out the 2 single quotes around $id but now it deletes the whole joke! That's not right...
    PeopleCards: The Official People Trading Card
    http://www.peoplecards.net

  5. #5
    Shiver me timbers!! anthony_irl's Avatar
    Join Date
    Aug 1999
    Location
    Dublin, Ireland
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you mean "delete"? Does it take the whole joke out of the database altogether or does it replace it with a row of empty values?
    Anthony - How's tings?

    24 hours in a day, 24 beers in a case. Coincidence? I think not.
    Contact me by: PM Email NEW! Carrier Pigeon

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    \|/
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It actually removes the JokeText and Date from the database, but still keeps the row and ID. So it shows the row but it's blank.
    PeopleCards: The Official People Trading Card
    http://www.peoplecards.net

  7. #7
    Shiver me timbers!! anthony_irl's Avatar
    Join Date
    Aug 1999
    Location
    Dublin, Ireland
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doheth!!! Can't believe I didn't see this before. YThe textfield that holds the joketext is named wrong. Here's what you have:

    PHP Code:
    <textarea name="message" cols="50" rows="10"> <?=$joketext?> </textarea><br>
    Change it to:

    PHP Code:
    <textarea name="joketext" cols="50" rows="10"> <?=$joketext?> </textarea><br>
    As regards the date. I think it's not inserting it properly due to the fact that you formatted it on the way out and it cant insert it back in that form.
    Anthony - How's tings?

    24 hours in a day, 24 beers in a case. Coincidence? I think not.
    Contact me by: PM Email NEW! Carrier Pigeon

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    \|/
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AHHHHHH! It's always the simplest stuff, I guess the devil really is in the details. Thanks, it works now.

    Mulls
    PeopleCards: The Official People Trading Card
    http://www.peoplecards.net

  9. #9
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Guess I'm a bit late then. Pleased its working...
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  10. #10
    SitePoint Enthusiast
    Join Date
    Sep 2001
    Location
    \|/
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooooh, not as pleased as I am...
    PeopleCards: The Official People Trading Card
    http://www.peoplecards.net


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •