SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Whats wrong witht his WHILE loop?

    PHP Code:
    $result mysql_query("SELECT JokeText FROM Jokes");
        
    $result1 mysql_query("SELECT JokeDate FROM Jokes");

    while ((
    $row mysql_fetch_array($result)) AND ($row1 mysql_fetch_array($result1)) {
            echo(
    "<P>" $row["JokeText"] . $row1["JokeDate"] . "</P>");
            } 
    Any ideas? Getting a parse error on the line with the WHILE argument?


    Cheers, Glen

  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One more closing bracket
    PHP Code:
    while (($row mysql_fetch_array($result)) AND ($row1 mysql_fetch_array($result1))) {
            echo(
    "<P>" $row["JokeText"] . $row1["JokeDate"] . "</P>"); 
    }

  3. #3
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou kindly, really must throughly check all syntax before posting here



  4. #4
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am having a problems here now where a variable seems to be not set and therefore isnt printing anything.

    Checked all my syntax and the script isnt throwing an error, just not printing the variable 'joketext' in the final echo of the script, just displaying the delete links.

    The problem is in the while loop or query but I just cant pin point it?
    Cheers

    PHP Code:
    <HTML>
    <HEAD>
    <TITLE> Our list of Jokes </TITLE>
    <HEAD>

    <BODY>
    <?
    // If the user wants to add a joke show the submission form
    if (isset($addjoke)) {
        
    ?>

        <FORM ACTION="<?php echo($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>

        <?
        
    }
    else {

        
    // Connect to the database
        
    $dbcnx mysql_connect("localhost""root""");

        
    // Select the jokes database
        
    mysql_select_db("jokes");

        
    // If a joke has been submitted, add it to the database.
        
    if ("SUBMIT" == $submitjoke) {
            
    $sql "INSERT INTO Jokes SET JokeText='$joketext',
                                          JokeDate=CURDATE()"
    ;
            
    mysql_query($sql);
            }
        
    // If user has selcted a to delete a link, delete it from the database
        
    if (isset($deletejoke)) {
                  
    $sql "DELETE FROM Jokes WHERE ID=$deletejoke";
        }

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

        
    // Request the text of all the jokes into jokedate variable
        
    $jokedata mysql_query("SELECT ID, joketext, jokedate FROM Jokes");

        
    //Pulls out 3 different columns of data and assigns each to a varible and prints each set out in a while loop
        
    while ($row mysql_fetch_array($jokedata)) {
            
    $joketext $row["JokeText"];
            
    $jokedate $row["JokeDate"];
            
    $jokeid $row["ID"];
            echo(
    "<p>$joketext "  "<a href='$PHP_SELF?deletejoke=$jokeid'>" "Delete this Joke</a></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>");
        }
    ?>
    </BODY>
    </HTML>

  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)
    Im no PHP expert and am only a young jedi when it comes to this sort of thing but I think the problem lies with this line:

    PHP Code:
    if ("SUBMIT" == $submitjoke) { 
    I think it should be:

    PHP Code:
    if ($submitjoke == "SUBMIT") { 
    Also it could be that your in your SQL query you reference the fields with lowercase:

    PHP Code:
    SELECT IDjoketextjokedate FROM Jokes 
    and then reference the fields in the array like this:

    PHP Code:
    $joketext $row["JokeText"]; 
    Maybe consider making them the same. Hope I may have helped.
    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
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,I know the SUBMIT string isnt the problem.

    Ill try your other suggestion.

    Glen

    [EDIT]

    Spot on with the case suggestion
    Cheers
    Last edited by glenplake; Mar 8, 2002 at 05:24.

  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)
    Realised the equality operator was alright after I posted. I'm just used to putting the variable on the left and the test on the right. Force of habit. Glad the other suggestion fixed it for ya anyway.
    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
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup

    Maybe you can help me on this one?

    Its not delecting the posts?

    Im passing deletejoke='ID' so deletejoke=1 or whatever and it passes a url like this to itself:

    http://localhost/dbconnect.php?deletejoke=2

    But nothing happens and the joke is still there? The above code is the same code I have got except with the errors from above corrected?

    Cheers

  9. #9
    Shiver me timbers!! anthony_irl's Avatar
    Join Date
    Aug 1999
    Location
    Dublin, Ireland
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think the problem lies here:

    PHP Code:
    if ("SUBMIT" == $submitjoke) {
            
    $sql "INSERT INTO Jokes SET JokeText='$joketext',
                                          JokeDate=CURDATE()"
    ;
            
    mysql_query($sql);
            }
        
    // If user has selcted a to delete a link, delete it from the database
        
    if (isset($deletejoke)) {
                  
    $sql "DELETE FROM Jokes WHERE ID=$deletejoke";
        }

        echo(
    "<P> Here are all the jokes in our database: </P>"); 
    You are never executing the sql to delete a joke so it isn't doing it at all. If you change the code to this it should work:

    PHP Code:
    if ("SUBMIT" == $submitjoke) {
            
    $sql "INSERT INTO Jokes SET JokeText='$joketext',
                                          JokeDate=CURDATE()"
    ;
            
            }
        
    // If user has selcted a to delete a link, delete it from the database
        
    if (isset($deletejoke)) {
                  
    $sql "DELETE FROM Jokes WHERE ID=$deletejoke";
        }

        if(isset(
    $sql) { mysql_query($sql); }
        echo(
    "<P> Here are all the jokes in our database: </P>"); 
    That means that if someone has either inserted a joke or deleted a joke then it will execute the query against the database. The if(isset()) was used so as to make sure that if someone came to the page without specifying a variable in the query string that the query would not try and execute an empty variable. Hope that helps.
    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

  10. #10
    Gone!
    Join Date
    Aug 2001
    Location
    Witty Location Parody
    Posts
    3,889
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    Thanks very much, works like a charm


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
  •