SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP_SELF page not refreshing

    I'm new to PHP and I'm working through the sitepoint book "database driven website using php and mysql" .The problem I'm having is I have to manually refresh the page to update fields. One of the page's I'm having problems with is editauthor.php part of the joke CMS; See below
    What have I done wrong, I;ve checked and checked and cant find any issues!

    Thanks in advance

    </head>

    <body>
    <?
    $dbhost = '***';
    $dbuser = '***';
    $dbpass = '***';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
    $dbname = 'jokedb';
    mysql_select_db($dbname)
    or die ("hmm an error with database!!<BR>" . mysql_error());
    if (isset($_POST['name'])):
    $name = $_POST['name'];
    $email = $_POST['email'];
    $id = $_POST['id'];
    $sql = "UPDATE author SET
    name='$name',
    email='$email'
    WHERE id='$id'";
    if (@mysql_query($sql)) {
    echo '<p>New author updated</p>';
    } else {
    echo '<p> error updating new author: ' .
    mysql_error() . '<p/>';
    }
    ?>
    <p><a href="authors.php">Return to authors list</a></p>
    <? else:
    $id = $_GET['id'];
    $author = @mysql_query(
    "SELECT name, email FROM author WHERE id='$id'");
    if (!$author) {
    exit ('<p>Error fetching author details; ' .
    mysql_error() . '</p>');
    }
    $author = mysql_fetch_array($author);
    $name = $author['name'];
    $email = $author['email'];
    $name = htmlspecialchars($name);
    $email = htmlspecialchars($email);
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Edit an author/p>
    <label> Nameinput type="text" name="name" value="<? echo $name; ?>" /></label><br />
    <label> Emailinput type="text" name="email" value="<? echo $email; ?>"/></label><br />
    <input type="hidden" name="id" value="<? echo $id; ?>" />
    <input type="submit" value="Submit" />
    </form>
    <? endif; ?>
    </body>
    </html>

  2. #2
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What browser are you using? If you have problems with page refreshing it may just be a local cache problem.
    PHP | MySQL | (X)HTML | CSS

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Really without being confirmed with your problem, i have made some slight changes here in your code here. I hope this will work for you.
    PHP Code:
    <?php
        $dbhost 
    '***';
        
    $dbuser '***';
        
    $dbpass '***';
        
    $conn mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql');
        
    $dbname 'jokedb';
        
    mysql_select_db($dbname) or die ("hmm an error with database!!<BR>" mysql_error());
        
        if (isset(
    $_POST['name'])){
            
    $name $_POST['name'];
            
    $email $_POST['email'];
            
    $id $_POST['id'];
            
    $sql "UPDATE author SET name='$name',email='$email' WHERE id='$id'";
            if (@
    mysql_query($sql)){
                echo 
    '<p>New author updated</p>';
                
    header("Location:" $_SERVER['HTTP_REFERER']);
                exit();
            } else {
                echo 
    '<p> error updating new author: ' mysql_error() . '<p/>';
            }
        }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    <body>
    <p><a href="authors.php">Return to authors list</a></p>
    <?php
        $id 
    $_GET['id'];
        
    $author = @mysql_query("SELECT name, email FROM author WHERE id='$id'");
        if (!
    $author) {
            exit(
    '<p>Error fetching author details; ' mysql_error() . '</p>');
        }
        
    $author mysql_fetch_array($author);
        
    $name $author['name'];
        
    $email $author['email'];
        
    $name htmlspecialchars($name);
        
    $email htmlspecialchars($email);
    ?>
    <form action="<?php echo $PHP_SELF?>" method="post">
        <p>Edit an author</p>
        <label> Name <input type="text" name="name" value="<?php echo $name?>" /></label><br />
        <label> Email <input type="text" name="email" value="<?php echo $email?>"/></label><br />
        <input type="hidden" name="id" value="<?php echo $id?>" />
        <input type="submit" value="Submit" />
    </form>
    </body>
    </html>
    When do you want to refresh your page?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Guru brent5392's Avatar
    Join Date
    Dec 2005
    Location
    Australia
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @rajug: That will cause a page validation error as you are allowing content outside of the body.

    Something like this might be better
    PHP Code:
    if (@mysql_query($sql)){ 
                
    $result '<p>New author updated</p>'
                
    header("Location:" $_SERVER['HTTP_REFERER']); 
                exit(); 
            } else { 
                
    $result '<p> error updating new author: ' mysql_error() . '<p/>'
            } 
    And later in the page

    PHP Code:
    <?php 
    if(isset($result)){
    echo 
    $result;
    }
    PHP | MySQL | (X)HTML | CSS

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yes. Good catch! I just changed his code and pasted.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the feedback i'll give it a go


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
  •