SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding submitted data to mySQL database

    Ok having problems with the first example in Kevin Yank's book. Everything works except adding a joke into the database. I can get it to retreive with no problem. I've went over the example in the book three times and I can't see any mistakes. Here's the code:

    PHP Code:
    <html>
    <head>
    <title></title>
    </head>
    <body>

    <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">
    </form>
    <?php

    $dbcnx 
    = @mysql_connect("localhost""root""password");
    if (!
    $dbcnx){
        print(
    "Unable to connect to the database server at this time.<p>");
        exit();
        }
        
        
        
    if (! @
    mysql_select_db("jokes")){
        print(
    "<p>Unable to locate jokes database at this time.");
        exit();
        }

    if(
    $submitjoke == "SUBMIT"){
        
    $sql "INSERT INTO jokes SET
        JokeText ='
    $joketext',
        JokeDate =CURDATE()"
    ;
        if(@
    mysql_query($sql)){
            print(
    "<p>Your joke was successfully entered.");
        }else{print(
    "<p>Error adding submitted joke: " mysql_error() . "");
    }
    }

    print(
    "Here are all the jokes in our database:<p><blockquote>");
    ?>
    <?php    
    $result 
    = @mysql_query("SELECT JokeText FROM jokes");
    if (!
    $result){
        print(
    "<p>Error preforming query: "mysql_error() ."");
        exit();
        }


        
    while( 
    $row mysql_fetch_array($result)){
        print(
    "<p>" $row["JokeText"] ."</p>");
        }
    print(
    "</blockquote>");


    ?>
    </body>
    </html>
    See anything wrong?
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  2. #2
    SitePoint Guru
    Join Date
    Dec 2000
    Location
    Karachi, Pakistan
    Posts
    913
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    Try using this as your SQL Query for adding the joke in the database.

    PHP Code:
    if($submitjoke == "SUBMIT"){
         
    $sql "insert into jokes(JokeText,JokeDate) values('$joketext',CURDATE())"

    Let me know if it works or gives any error!
    36Host.com - $36/year web hosting [affiliates earn 30%]
    * Affordable Small Business Web Hosting since 2003! *
    "500mb space, 10gb bandwith, 50 pop/ftp accounts, php,
    mysql, pre-installed php scripts, 24/7 support & more...."



  3. #3
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response Omair.

    I switched the statement to the one you posted and it still doesn't write it to the database. In fact, I don't even get an error message. I hit submit and the page reloads and looks the same. I checked the database and nothing is written there either. Not sure what's wrong. Thanks again.
    JRF2k
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  4. #4
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anything else to try?
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  5. #5
    SitePoint Guru
    Join Date
    Dec 2000
    Location
    Karachi, Pakistan
    Posts
    913
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello JRF2K,

    Is it possible for you to post the mySQL dump of your databasE?



    -Omair
    36Host.com - $36/year web hosting [affiliates earn 30%]
    * Affordable Small Business Web Hosting since 2003! *
    "500mb space, 10gb bandwith, 50 pop/ftp accounts, php,
    mysql, pre-installed php scripts, 24/7 support & more...."



  6. #6
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just curious; this may in fact do nothing, but best not doubt. Change <?=$PHP_SELF?> to <?php echo($_SERVER['PHP_SELF']); ?>, and change $joketext to $_POST['joketext']. Does anything change?

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  7. #7
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably, if I knew how to do that. PHP/mySQL newbie here. How do you dump the database? Thanks.
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  8. #8
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make these changes instead:
    PHP Code:
    <form action="<?php echo $_SERVER['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">
    </form>
    <?php

    // snip

    if($_POST['submitjoke'] == "SUBMIT"){
        
    $sql "INSERT INTO jokes SET
        JokeText ='
    $joketext',
        JokeDate =CURDATE()"
    ;

    // snip
    see if it works

  9. #9
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Darn those new arrays. I always forget to use them! That got it putting stuff in the database, however, all it puts in it are blank entries. No matter what I type all I get is a blank fields for JokeText and JokeDate. Thanks for getting me this far.
    JRF2k
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  10. #10
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found my error. I have it working like a charm now. Again, thanks for the reminder! I need to post those things somewhere.

    JRF2k
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  11. #11
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe you can help me i seem to be getting the same errors as you were getting( adding nothing to the database but an empty string)

    here is the code i am using
    PHP Code:
     [CODE
    <
    html>
    <
    head>
    <
    titleThe Internet Joke Database </title>
    </
    head>
    <
    body
    PHP Code:
    [size=2][color=#c0c000]<?php
    if (isset($_GET['addjoke'])): // If the user wants to add a joke
    ?>
    [/color][/size][size=2]<form action=[/size][size=2][color=#0000f0]"<?php echo $_SERVER['PHP_SELF']?>"[/color][/size][size=2] method=[/size][size=2][color=#0000f0]"post"[/color][/size][size=2]>
    <p>Type your joke here:<br />
    <textarea name=[/size][size=2][color=#0000f0]"joketext"[/color][/size][size=2] rows=[/size][size=2][color=#0000f0]"10"[/color][/size][size=2] cols=[/size][size=2][color=#0000f0]"40"[/color][/size][size=2] wrap></textarea><br />
    <input type=[/size][size=2][color=#0000f0]"submit"[/color][/size][size=2] name=[/size][size=2][color=#0000f0]"submitjoke"[/color][/size][size=2] value=[/size][size=2][color=#0000f0]"SUBMIT"[/color][/size][size=2] /></p>
    </form>
    [/size][size=2][color=#c0c000]<?php
    else: // Default page display
    // Connect to the database server
    $dbcnx = @mysql_connect("localhost""root""choltas");
    if (!
    $dbcnx) {
    echo( 
    "<p>Unable to connect to the " .
    "database server at this time.</p>" );
    exit();
    }
    // Select the jokes database
    if (! @mysql_select_db("mysql") ) {
    echo( 
    "<p>Unable to locate the joke " .
    "database at this time.</p>" );
    exit();
    }
    // If a joke has been submitted,
    // add it to the database.
    if ($_POST['submitjoke'] == "SUBMIT"){
    $sql "INSERT INTO jokes SET
    JokeText='
    $_POST['joketext']',
    JokeDate=CURDATE()"
    ;
    if (@
    mysql_query($sql)) {
    echo(
    "<p>Your joke has been added.</p>");
    } else {
    echo(
    "<p>Error adding submitted joke: " .
    mysql_error() . "</p>");
    }
    }

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

    // Request the text of all the jokes
    $result = @mysql_query("SELECT JokeText FROM jokes");
    if (!
    $result) {
    echo(
    "<p>Error performing query: " mysql_error() . "</p>");
    exit();
    }

    // Display the text of each joke in a paragraph
    while ( $row mysql_fetch_array($result)) {
    echo(
    "<p>" $row["JokeText"] . "</p>");
    }

    // When clicked, this link will load this page
    // with the joke submission form displayed.
    echo("<p><a href="$_SERVER["PHP_SELF']?addjoke=1'>Add a Joke!</a></p>");

    endif;

    ?>
    [/color][/size][size=2]</body>
    </html>
    [/size]

    any help would be appreciated

  12. #12
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey peegee,

    Change:

    if ($_POST['submitjoke'] == "SUBMIT" )

    to:

    if ($_POST['submitjoke'] = "SUBMIT" )

    Single equal sign. If memory serves me right the one above checks to see if the Submit button is checked and I suppose since there is no real data associated with the Submit button you get blank database entries. The bottom says if it's clicked then do this with the data in $_POST.

    I think that's right. Try it out and let me know. I could be wrong. No harm in trying.

    JRF2k


    Quote Originally Posted by peegee
    maybe you can help me i seem to be getting the same errors as you were getting( adding nothing to the database but an empty string)

    here is the code i am using
    PHP Code:
     [CODE
    <
    html>
    <
    head>
    <
    titleThe Internet Joke Database </title>
    </
    head>
    <
    body
    PHP Code:
    [size=2][color=#c0c000]<?php
    if (isset($_GET['addjoke'])): // If the user wants to add a joke
    ?>
    [/color][/size][size=2]<form action=[/size][size=2][color=#0000f0]"<?php echo $_SERVER['PHP_SELF']?>"[/color][/size][size=2] method=[/size][size=2][color=#0000f0]"post"[/color][/size][size=2]>
    <p>Type your joke here:<br />
    <textarea name=[/size][size=2][color=#0000f0]"joketext"[/color][/size][size=2] rows=[/size][size=2][color=#0000f0]"10"[/color][/size][size=2] cols=[/size][size=2][color=#0000f0]"40"[/color][/size][size=2] wrap></textarea><br />
    <input type=[/size][size=2][color=#0000f0]"submit"[/color][/size][size=2] name=[/size][size=2][color=#0000f0]"submitjoke"[/color][/size][size=2] value=[/size][size=2][color=#0000f0]"SUBMIT"[/color][/size][size=2] /></p>
    </form>
    [/size][size=2][color=#c0c000]<?php
    else: // Default page display
    // Connect to the database server
    $dbcnx = @mysql_connect("localhost""root""choltas" );
    if (!
    $dbcnx) {
    echo( 
    "<p>Unable to connect to the " .
    "database server at this time.</p>" );
    exit();
    }
    // Select the jokes database
    if (! @mysql_select_db("mysql" ) ) {
    echo( 
    "<p>Unable to locate the joke " .
    "database at this time.</p>" );
    exit();
    }
    // If a joke has been submitted,
    // add it to the database.
    if ($_POST['submitjoke'] == "SUBMIT" ){
    $sql "INSERT INTO jokes SET
    JokeText='
    $_POST['joketext']',
    JokeDate=CURDATE()"
    ;
    if (@
    mysql_query($sql)) {
    echo(
    "<p>Your joke has been added.</p>" );
    } else {
    echo(
    "<p>Error adding submitted joke: " .
    mysql_error() . "</p>" );
    }
    }
     
    echo(
    "<p> Here are all the jokes in our database: </p>" );
     
    // Request the text of all the jokes
    $result = @mysql_query("SELECT JokeText FROM jokes" );
    if (!
    $result) {
    echo(
    "<p>Error performing query: " mysql_error() . "</p>" );
    exit();
    }
     
    // Display the text of each joke in a paragraph
    while ( $row mysql_fetch_array($result)) {
    echo(
    "<p>" $row["JokeText"] . "</p>" );
    }
     
    // When clicked, this link will load this page
    // with the joke submission form displayed.
    echo("<p><a href="$_SERVER["PHP_SELF']?addjoke=1'>Add a Joke!</a></p>" );
     
    endif;
     
    ?>
    [/color][/size][size=2]</body>
    </html>
    [/size]

    any help would be appreciated
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  13. #13
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JRF2k
    Change:

    if ($_POST['submitjoke'] == "SUBMIT" )

    to:

    if ($_POST['submitjoke'] = "SUBMIT" )
    the second's not checking anything. it's the same as doing

    if ('SUBMIT')

    which is the same as

    if (1)

    which is the same as no if at all. the == is needed for comparing.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  14. #14
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your problem also lies in your INSERT statement.

    $sql = "INSERT INTO jokes SET JokeText='$_POST['joketext']', JokeDate=CURDATE()";

    Try this one:
    $joketext = $_POST['joketext'];
    $sql = "INSERT INTO jokes VALUES('', '$joketext',CURDATE())";

  15. #15
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry peegee, I assumed that your table had 3 columns in it. I normally create an id column for each table I have in my database. Use this INSERT instead:

    Code:
     
    $joketext = $_POST['joketext'];
    $sql = "INSERT INTO jokes VALUES('$joketext',CURDATE())";

  16. #16
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    o.k. i seem to be getting somewhere, got it adding info to the database
    still no joy with the delete function
    can someone confirm the following
    if (isset($_GET['deletejoke']))
    '$_POST[deletejoke]'";
    '$_POST[jokeid] = $row["ID"]';
    '$_POST[joketext]= $row["JokeText"]';
    '$_POST[joketext]'
    "<a href="</FONT><B><FONT size=2><FONT color=#000000>$_SERVER</FONT></B></FONT><FONT color=#0000f0 size=2>["PHP_SELF']?deletejoke=$_POST[jokeid]'>"







  17. #17
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think that code will work.
    It's a bit hard to understand what do you want to accomplish?
    Do you want to display a link with an id, so when you click on it the joke gets deleted,
    or do you want the code that actual delete the joke after such a link has been clicked?


    -Helge

  18. #18
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i want to display a link with an id, so when you click on it the joke gets deleted,

  19. #19
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Quote Originally Posted by peegee
    i want to display a link with an id, so when you click on it the joke gets deleted,
    In the script below, I've added a link after each displayed joketext that contains that jokes id.
    When you click on that link the joke gets deleted from the db. I've added code to do that aswell.
    PHP Code:
     
    <?php
    error_reporting
    (E_ALL);
    ?>
    <html>
    <head>
    <title> The Internet Joke Database </title>
    </head>
    <body>
    <?php
    if (isset($_GET['addjoke'])) { // If the user wants to add a joke
    ?>
        <form action="<?php echo $_SERVER['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
    } else { // Default page display
        // Connect to the database server
        
    $dbcnx = @mysql_connect("localhost""root""choltas" );
        if (!
    $dbcnx) {
            echo 
    '<p>Unable to connect to the database server at this time.</p>';
            exit();
        }
        
    // Select the jokes database
        
    if (! @mysql_select_db("mysql" ) ) {
            echo 
    '<p>Unable to locate the joke database at this time.</p>';
            exit();
        }
        
    // If a joke has been submitted, add it to the database.
        
    if (isset($_POST['addjoke']) && $_POST['submitjoke'] == "SUBMIT" ){
            
    $sql "INSERT INTO jokes SET
                    JokeText='
    $_POST['joketext']',
                    JokeDate=CURDATE()"
    ;
            if (@
    mysql_query($sql)) {
                echo 
    '<p>Your joke has been added.</p>';
            } else {
                echo 
    '<p>Error adding submitted joke: ' mysql_error() . '</p>';
            }
        } elseif(isset(
    $_GET['deletejoke']) && is_int($_GET['deletejoke'])) {
            
    $sql "DELETE FROM jokes
                    WHERE ID= 
    $_GET[id]";
            if (@
    mysql_query($sql)) {
                echo 
    '<p>The joke has been deleted.</p>';
            } else {
                echo 
    '<p>Error deleting joke: ' mysql_error() . '</p>';
            }
        }

        echo 
    '<p> Here are all the jokes in our database:</p>';
        
    // Request the ID and text of all the jokes
        
    $result = @mysql_query("SELECT ID, JokeText FROM jokes" );
        if (!
    $result) {
            echo 
    '<p>Error performing query: ' mysql_error() . '</p>';
            exit();
        }
        
    // Display the text of each joke in a paragraph
        
    while ( $row mysql_fetch_array($result)) {
            echo 
    '<p>' $row["JokeText"] . '<br />';
            echo 
    '<a href="' $_SERVER['PHP_SELF'] . '?deletejoke='$row['ID'] . '">Delete this Joke!</a></p>';
        }
        
    // When clicked, this link will load this page
        // with the joke submission form displayed.
        
    echo '<p><a href="' $_SERVER['PHP_SELF'] . '?addjoke=1">Add a Joke!</a></p>';
    }
    ?>
    </body>
    </html>
    I've not tested it, but hopfully it will work.

    -Helge

  20. #20
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found an error:
    Replace the addjoke sql with this:
    PHP Code:
     
    $sql 
    "INSERT INTO jokes SET
                    JokeText='
    $_POST[joketext]',
                    JokeDate=CURDATE()"

    -Helge
    Last edited by Helge; Mar 7, 2003 at 04:19.

  21. #21
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    thanks to all , i now have the script adding and deleting working like a gem



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
  •