SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry it wont update *sigh*....

    what could be wrong

    up.php...

    <?php
    if ($submit) {
    $sql = "UPDATE news SET article=$article, title=$title, date=$date, id=$id, WHERE id=$id";
    }
    ?>

    <form method="post" action="up.php">
    <?php
    $sql = "SELECT * FROM news WHERE id=\"$id\"";
    $result = mysql_query($sql);
    $myrow = mysql_fetch_array($result);
    $title = $myrow["title"];
    $article = $myrow["article"];
    $date = $myrow["date"];
    $id = $myrow["id"];
    ?>
    <input type=hidden name="id" value="<?php echo $id ?>">
    idinput type="Text" name="id" value="<?php echo $id ?>"><br>
    titleinput type="Text" name="title" value="<?php echo $title ?>"><br>
    articleinput type="Text" name="article" value="<?php echo $article ?>"><br>
    dateinput type="Text" name="date" value="<?php echo $date ?>"><br>
    <input type="Submit" name="submit" value="Enter information">
    </form>



    anyone got any ideas why its not updating the database?
    http://redgoals.com ... my site ... nuff said

  2. #2
    SitePoint Wizard
    Join Date
    Jul 2001
    Location
    The Netherlands
    Posts
    2,617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe the exact syntax for an update query should be this way in your case:

    PHP Code:
    $sql "UPDATE news SET article='$article', title='$title', date='$date', id=$id WHERE id='$id'"
    You also had an extra comma before your WHERE-clause that might have messed things up.
    Last edited by Mark T.; Feb 13, 2002 at 16:08.

  3. #3
    I have an opinion...
    Join Date
    Sep 2001
    Location
    Barrie, Ontario
    Posts
    324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming that your ID field is an auto increment field..

    In SQL, you need apostrophes around strings, but not around numbers. You should only have one ID form field, otherwise the form will submit an array, and the WHERE clause won't work.

    The SQL should look more like this...
    PHP Code:
    <? 
        
    if( $submit ) { 
            
    $sql "UPDATE news SET article='$article', title='$title', date='$date' WHERE id=$id"
        } 
    ?>
    And the form more like this:
    PHP Code:
    <? 
        $result 
    mysql_query"SELECT * FROM news WHERE id=" $id ) or die( mysql_error() );
        
    $myrow mysql_fetch_array$result ) or die( mysql_error() );
        
    $title $myrow["title"];
        
    $article $myrow["article"]; 
        
    $date $myrow["date"]; 
        
    $id $myrow["id"]; 
    ?> 
    <form method="post" action="up.php"> 
        <input type=hidden name="id" value="<? print( $id ); ?>"> 
        title: <input type="Text" name="title" value="<? print( $title ); ?>"><br> 
        article: <input type="Text" name="article" value="<? print( $article ); ?>"><br> 
        date: <input type="Text" name="date" value="<? print( $date ); ?>"><br> 
        <input type="Submit" name="submit" value="Enter information"> 
    </form>
    Give that a shot and see how you do...
    Last edited by KodeKrash; Feb 13, 2002 at 16:06.
    Egotist: A person more interested in himself than in me.
    KodeKrash - Eidix - Barrie LUG

  4. #4
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thnx for the help guys but no result yet.

    after i pass the id in the address (up.php?id=3) i get the details presented from the row 3 in the database, i change the details and then hit submit but it returns the same form with the same details as they were in the database.

    here take a look..link
    http://redgoals.com ... my site ... nuff said

  5. #5
    I have an opinion...
    Join Date
    Sep 2001
    Location
    Barrie, Ontario
    Posts
    324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume you are populating your variables, but this should work for you...

    PHP Code:
    <? 
        
    if( $HTTP_POST_VARS["submit"] ) {
            
    $article trim$HTTP_POST_VARS["article"] );
            
    $title trim$HTTP_POST_VARS["title"] );
            
    $date trim$HTTP_POST_VARS["date"] );
            
    $id $HTTP_POST_VARS["id"];
            
    mysql_query"UPDATE news SET article='$article', title='$title', date='$date' WHERE id=$id) or die( mysql_error() ); 
        } 
    ?>
    Egotist: A person more interested in himself than in me.
    KodeKrash - Eidix - Barrie LUG

  6. #6
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: it wont update *sigh*....

    The problem is you have design error in your code. If the the form is submitted the new sql query is set, but further down in your script the select sql query is set. This means the select query will always be executed. To perform the update query use mysql_query($sql) in your "if ($submit)" statement at the top of the script.





    Cheers.

  7. #7
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    success . It seems filmfoto was correct about the query at the top, thanks KodeKrash for providing the code, much appreciated.

    code in full...


    <?
    mysql_pconnect("localhost","username","pass");
    mysql_select_db("dbname");
    ?>


    <?
    if( $HTTP_POST_VARS["submit"] ) {
    $article = trim( $HTTP_POST_VARS["article"] );
    $title = trim( $HTTP_POST_VARS["title"] );
    $date = trim( $HTTP_POST_VARS["date"] );
    $id = $HTTP_POST_VARS["id"];
    mysql_query( "UPDATE news SET article='$article', title='$title', date='$date' WHERE id=$id" ) or die( mysql_error() );
    }
    ?>


    <?
    $result = mysql_query( "SELECT * FROM news WHERE id=" . $id ) or die( mysql_error() );
    $myrow = mysql_fetch_array( $result ) or die( mysql_error() );
    $title = $myrow["title"];
    $article = $myrow["article"];
    $date = $myrow["date"];
    $id = $myrow["id"];
    ?>

    <form method="post" action="up.php">
    <input type=hidden name="id" value="<? print( $id ); ?>">
    title: <input type="Text" name="title" value="<? print( $title ); ?>"><br>
    article: <input type="Text" name="article" value="<? print( $article ); ?>"><br>
    date: <input type="Text" name="date" value="<? print( $date ); ?>"><br>
    <input type="Submit" name="submit" value="Enter information">
    </form>
    http://redgoals.com ... my site ... nuff said

  8. #8
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should run your text through addslashes before inserting into the DB, BTW.

    e.g.
    SET article='" . addslashes( $article ) . "'


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
  •