SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing variables from one page to Another

    Hi,

    I've been having a problem with this script for awhile now. Every time I seem to fix something, something else goes wrong.

    I'm building a backend page for a client. I want the client to be able to edit her own file.

    Here is my problem.

    in my buzz.php page I have one line of code that should pass the id to the next page so the client can edit the text here is the code of buzz.php

    Code:
    // When clicked, this link will load this page
      // with the buzz submission form displayed.
      echo '<p><a href="editbuzz.php?id='.$id.'">edit</a></p>';

    So when the next page loads up, editbuzz.php all I get is a blank textbox meaning the id of the entry is not being passed.

    Here is the code for editbuzz.php

    Code:
    if (isset($_POST['joketext'])):
      // The joke's details have
      // been updated.
      
      $id = $_POST['id'];
      $joketext = $_POST['joketext'];
    
      $sql = "UPDATE joke SET
              joketext='$joketext'
              WHERE id='$id'";
      if (mysql_query($sql)) {
        echo '<p>Joke details updated.</p>';
      } else {
        exit('<p>Error updating joke details: ' .
            mysql_error() . '</p>');
      }
    ?>
    
    <p><a href="jokes.php">New joke search</a></p>
    
    <?php else: // Allow the user to edit the joke
    
      $id = $_GET['id'];
    
      $joke = @mysql_query(
        "SELECT joketext FROM joke WHERE id='$id'");
      if (!$joke) {
        exit('<p>Error fetching buzz details: ' .
            mysql_error() . '</p>');
      }
    
      $joke = mysql_fetch_array($joke);
    
      $joketext = $joke['joketext'];
      
       // Convert HTML special characters
      // in database value for use in
      // an HTML document.
      $joketext = htmlspecialchars($joketext);
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Edit the joke:<br />
    <textarea name="joketext" rows="15" cols="45">
    <?php echo $joketext; ?></textarea>
    <input type="hidden" name="id" value="<?php echo $id; ?>" />
    <input type="submit" value="SUBMIT" />
    </form>
    
    <?php endif; ?>
    here is the page to look at what is happening.

    www.clhdesigns.com/site2/jokes.php

    thanks
    chris
    Your Websites Your Way!!
    www.clhdesigns.com

  2. #2
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your $id variable is simply remaining null. You haven't pasted that segment of the code, however, so I am unable to diagnose your issue at this stage.
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  3. #3
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok the edit buzz.php is the complete script.

    Here is the complete script for the jokes.php page.

    Code:
    <?php if (isset($_GET['addjoke'])): // If the user wants to add a joke
    ?>
    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label>Type your joke here:<br />
    <textarea name="joketext" rows="10" cols="40">
    </textarea></label><br />
    <input type="submit" value="SUBMIT" />
    </form>
    
    <?php else: // Default page display
    
      // Connect to the database server
      $dbcnx = @mysql_connect('localhost', 'clhdesi_christ', 'hitman1');
      if (!$dbcnx) {
        exit('<p>Unable to connect to the ' .
            'database server at this time.</p>');
      }
    
      // Select the jokes database
      if (!@mysql_select_db('clhdesi_clhdesigns')) {
        exit('<p>Unable to locate the joke ' .
            'database at this time.</p>');
      }
    
      // If a joke has been submitted,
      // add it to the database.
      if (isset($_POST['joketext'])) {
        $joketext = $_POST['joketext'];
        $sql = "INSERT INTO joke SET
            joketext='$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 joke');
      if (!$result) {
        exit('<p>Error performing query: ' .
            mysql_error() . '</p>');
      }
    
      // 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="editbuzz.php?id='.$id.'">edit</a></p>';
    
    endif;
    ?>
    Thanks
    chris
    Your Websites Your Way!!
    www.clhdesigns.com

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    edge of nowhere
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all you are not passing along anything in $_GET['id'] so better fix that in case you haven't noticed it.
    If that's working and the script still doesn't perform, the probable cause is your if else is not working the way you want/expect it to. Use some debug statements to figure out on which branch you are at each step.
    Programming boils down to three things: fast, good and cheap.
    Please pick two.

  5. #5
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    North Richmond
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Chris,

    As WildHoney stated, your $id variable doesn't hold any value. You have not assigned it by any means.

    Your script does not hold or ask for a user id anywhere within jokes.php - $id needs to be established on this page somewhere then passed over to the next editbuzz.php

    Also, just to let you know, you should never post your true details of your dbcx. Most understand the dbcx. All you need to do is just replace your dbcx details with xxxx.
    DON'T
    PHP Code:
    <?php
    // Connect to the database server
      
    $dbcnx = @mysql_connect('localhost''clhdesi_christ''hitman1');
    ?>
    DO
    PHP Code:
    <?php
    // Connect to the database server
      
    $dbcnx = @mysql_connect('localhost''xxxx''xxxx');
    ?>
    Regards,
    BJ Duncan

  6. #6
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if the $Get id attribute is not passing anything what should I be putting in to get it to pass?
    Your Websites Your Way!!
    www.clhdesigns.com

  7. #7
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok I have actually figured out how to get the text to display in the text box

    Seeing that there is always only going to be 1 record in this table I just set the id attribute in the edit buzz page to it's actual id number

    here is the update line of code.
    [CODE]$joke = @mysql_query(
    "SELECT joketext FROM joke WHERE id='14'");
    if (!$joke) {
    exit('<p>Error fetching buzz details: ' .
    mysql_error() . '</p>');
    }

    Thanks for the help now I'll have to put in an update statement to get the record to update
    Your Websites Your Way!!
    www.clhdesigns.com

  8. #8
    SitePoint Evangelist BJ Duncan's Avatar
    Join Date
    Jun 2007
    Location
    North Richmond
    Posts
    495
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will your script require a user to log in?
    Regards,
    BJ Duncan

  9. #9
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm glad you've fixed your issues. However, may I ask what is the point in having a table with only 1 column? Surely your WHERE id = 14 clearly says in itself there are multiple columns. Also, strictly speaking it should be WHERE id = 14 and not WHERE id = '14' because 14 is an integer. Use sprintf() to set it as so or use typecasting to set it as so before it enters the database.
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  10. #10
    SitePoint Addict
    Join Date
    Apr 2003
    Location
    canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Answer to first question yes it will a user to require to log in. This is just in the testing phase at the moment.

    Answer to second question. The client will only be updating the same file. I could always put it a make active link to allow multiple entries. Thanks for the heads up on the where clause.
    Your Websites Your Way!!
    www.clhdesigns.com


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
  •