SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Location
    1
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i try executing my php code where you can add a joke to the data base. My Name.php file reports a parsing error on line 27 (referenced) Please help. Thanks. This is what i have:

    <?php

    //If the user wants to add a joke to the database

    if (isset ($addjoke) ) ;

    ?>

    <form action=" <?php echo ($PHP_SELF); ?>" method="post">
    <p>Type your Joke here:</P>
    <textarea name="joketext" rows="10" cols="40" wrap></textarea><br>
    <input type="submit" name="submitjoke" value="submit">
    </form>

    <?php
    else: // THIS IS MY LINE 27

    //Connect to database server
    $dbcnx =@mysql_connect ("localhost",
    "root", "mypassword");

    if (!$dbcnx) {
    echo (" <P> Unable to connect to the " .
    "database server at this time </P>");
    exit();
    }

    // Select Jokes database

    if (! @mysql_select_db("jokes") ) {
    echo ("<P> Unable to locate the joke " .
    "database at this time.</P>");
    exit();

    }

    //IF a joke has been submitted
    //add to database

    if ( "submit" == $submitjoke) {
    $sql = "INSERT INTO Jokes SET" .
    "JokeText = '$joketext', " .
    "JokeDate = CURDATE()";

    if ( mysql_query($sql) ) {
    echo ("<P> YOur joke has been submitted. </P?");
    } else {
    echo ("<P> Error submitting joke: " .
    mysql_erro() . "</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 all text of each joke in 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 displyed.

    echo ( "<P> <A HREF ='$PHP_SELF?addjoke=1'>" .
    "Add a Joke!</A></P>");
    endif;


    ?>

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    In the example prior to this one in the tutorial where all the jokes are selected and displayed I get this error:

    Call to undefined function: array() for my php code: Please look at code commented "this line..."


    while ($row = mysql_fetch_array ($result) ) {
    echo( "<P>" . $row ("JokeText") . "</P>" ); //This is line where I get an error

    [This message has been edited by syntax_error (edited July 21, 2000).]

    [This message has been edited by syntax_error (edited July 21, 2000).]

  2. #2
    SitePoint Wizard edshuck's Avatar
    Join Date
    Jul 2000
    Posts
    1,200
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi

    i would try $row["JokeText"] in place of the $row("JokeText")

    best to you. ed

  3. #3
    SitePoint Zealot
    Join Date
    Nov 1999
    Location
    Manhattan!
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is the correct way (at least it works for me on several sites).

    $result = mysql_query("SELECT name, age FROM db WHERE member_id=3");
    while($data = mysql_fetch_arry($result)) {
    echo $data['name'] ." - ". $data['age'];
    }

    OR, you can do:

    $result = mysql_query("SELECT name, age FROM db WHERE member_id=3");
    while($data = mysql_fetch_row($result)) {
    echo $data[0] ." - ". $data[1];
    }

    the array() function is used to creact/populate an array.

    Christian


  4. #4
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    syntax_error:

    The parse error is being caused by your if(isset($addjoke)) line, which needs a colong, not a semicolon, on the end of it:

    if (isset ($addjoke) ):



    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2000
    Location
    1
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The square brackets work [ ] also i missed the " : " after the isset function

    Now I am getting an SQL syntax_error hmm my eyes are squinting. Let me know of you can catch it!

  6. #6
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    $sql = "INSERT INTO Jokes SET" .
    "JokeText = '$joketext', " .
    "JokeDate = CURDATE()";


    Stick those three strings together, and you get this:

    $sql = "INSERT INTO Jokes SETJokeText = '$joketext', JokeDate = CURDATE()";

    See the problem? :-)


    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!


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
  •