Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Posts
    996
    Rep Power
    15

    Simple update text box


    Hi

    I have made a simple script which allows the user to enter information into a text box, submit and display it. However, i want the user to be able to update this information.

    I have put something together (below) but it doesn't seem to work. What i'm guessig is happening is, that it is inserting the data into the database, then updating it, NOT just updating it. I have to click the submit button twice for it to take affect.

    Any healp on this would be great!

    Here is the code:

    Form Page

    PHP Code:
    <?

         $query 
    "insert into job_story set news = \"$news\"";
        
    $result mysql_query($query);


    $sk001 "update job_story set news = \"$_POST[Newnews]\" ";
    $rsk002 mysql_query($sk001) or die(mysql_error());

    $qsk "select * from job_story ";
        
    $rsk mysql_query($qsk) or die(mysql_error());
        
    $ask mysql_fetch_array($rsk);



    ?>

    Preview Page

    PHP Code:
                 <?

    $q 
    "select * from job_story ";
    $r mysql_query($q) or die(mysql_error());
    $a mysql_fetch_array($r);

    $sk001 "update job_story set news = \"$_POST[Newnews]\" ";
    $rsk002 mysql_query($sk001) or die(mysql_error());

    $qsk "select * from job_story ";
        
    $rsk mysql_query($qsk) or die(mysql_error());
        
    $ask mysql_fetch_array($rsk);

    ?>
                  
                  
                  
                  <? echo "$a[news]"?>

    Comments on this post

    • chadsmith729 agrees : Drive by rep, you've been hit!
  2. #2
  3. No Profile Picture
    Lost in the 70's
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    Nashville TN USA
    Posts
    252
    Rep Power
    21
    Do yourself a favor and to avoid excaping quotes and all do this instead
    PHP Code:
    $query "insert into job_story set news = '$news'";
    $result mysql_query($query);

    $sk001 "update job_story set news = '{$_POST[Newnews]}'";
    $rsk002 mysql_query($sk001) or die(mysql_error());
    echo 
    $sk001;
    $qsk "select * from job_story ";
    $rsk mysql_query($qsk) or die(mysql_error());
    $ask mysql_fetch_array($rsk); 
    Note the curly braces around the $_POST array. Although the code will work what will happen is that it will put whatever value is contained in $_POST[Newnews] for all rows in that table, so you need to have a WHERE clause in there to only affect the row(s) you wish.
    PHP Code:
    $sk001 "UPDATE job_story SET news = '{$_POST[Newnews]}'WHERE user ='$user'"
    Also don't use short tags <? instead use <?php because short tags now are turned off by default and if your webserver upgrades code with short tags will cease to work and you will have to go change the code to <?php to make it work again.
    Note when troubleshooting code echo out your queries to see that what you are sending MySQL so in the above code I placed one there for you to see what is going to MySQL, then when everything works correctly then comment it out or remove the echo of the query.

    Comments on this post

    • chadsmith729 agrees : Drive by rep, you've been hit!
    Last edited by Houdini2005; May 14th, 2006 at 12:45 PM. Reason: Explain echo statement
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Posts
    996
    Rep Power
    15
    Ok thanks for the tips.

    One thing.. why did you add echo $sk001?

    When i do that, it echos this:

    update job_story set news = ''
  6. #4
  7. No Profile Picture
    Lost in the 70's
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    Nashville TN USA
    Posts
    252
    Rep Power
    21
    The reason for echoing a query is to see what is passed to the MySQL so in this case apparently there is no value for the $_POST variable $_POST[Newnews]. What you need to do is first check is $_POST[Newnews] is set or not empty then to make coding easier if it is in fact set or not empty assign it to asimple variable like
    PHP Code:
    $news $_POST[Newnews]; 
    then you would rewrite your code like this
    PHP Code:
    $sk001 "UPDATE job_story SET news = '$news' WHERE someCondition"
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Posts
    996
    Rep Power
    15
    Thanks. it's working now, except, when i go to the page to post the text, it wipes everything. Why is it not staying there?
  10. #6
  11. No Profile Picture
    Lost in the 70's
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    Nashville TN USA
    Posts
    252
    Rep Power
    21
    Your $_POST data is no longer there so you might have to use sessions so in the page where you are updating the database start the page like this
    PHP Code:
    <?php
    session_start
    ();
    //then set a new $_SESSION variable
    $_SESSION['news'] = $_POST['Newnews'];
    //more code
    ?>
    then on the page you are posting to again you will need to use session_start(); at the beginning of the php for that page, you have to start a session on any page you want to us or assign a $_SESSION variable.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Posts
    996
    Rep Power
    15
    Ok thanks.
    It keeps the text in the text area each time i add more text, but if i click on the link to that page again, it still wipes everything. Any ideas?
  14. #8
  15. No Profile Picture
    Lost in the 70's
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    Nashville TN USA
    Posts
    252
    Rep Power
    21
    When you click a link to a page that is used with a form for posting data that page or script does not not if it is the first time it has been viewed or the hundredth time. This is why I suggested using $_SESSION variables, these variables once set remain with the user wherever they go to on your site and are availble if you use session_start() first. So like I said on the page where you are updating the database set a $_SESSION variable just like I showed in the earlier post then in your form page make sure that again that you use session_start() again then you can use the $_SESSION['somevariable'] and use that value in the form field and your problem should be solved.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Posts
    996
    Rep Power
    15
    I don't really understand sessions. I want the text to stay in the text area permanantly, not just when the user logs in and then is reset. I want it to act like a profile where the user logs in and can modify their information.
  18. #10
  19. No Profile Picture
    Lost in the 70's
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    Nashville TN USA
    Posts
    252
    Rep Power
    21
    Well not seeing your actual form it is hard to tell you how to deal with it, generally speacking when you click submit on a form and lets say the
    Code:
    <form action ="process.php" method="post">
    <!--More form stuff -->
    <input type = "submit" value = "Update">
    </form>
    Now the $_POST data is good on the process.php page but if you go anywhere else even back to the original page guess where the $_POST variables you just used went...they are now gone. This is why you need to have a variable that will last the lifetime of a users visit this is where sessions are important because you can assign $_POST or $_GET data to them and they are good anywhere you want to make use of them, they are not that hard to learn how to use and will enable you to make your code do what you are wanting it to do.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Posts
    996
    Rep Power
    15
    Ok, I think i understand. I have no idea how to use them, but i guess i have to do something like:


    $_SESSION['news'] = $_GET['Newnews'];

    or

    $news = $_GET[Newnews];

    am i close?
  22. #12
  23. No Profile Picture
    Lost in the 70's
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    Nashville TN USA
    Posts
    252
    Rep Power
    21
    Just a quick example so you can learn them quickly.
    Call this one posting.php
    PHP Code:
    <?php 
    session_start
    ();
    //Now you can use the session data
     
    ?>
     <form action='pagetwo.php' method='post'>
     <textarea name='input'><?php echo$_SESSION['test']; ?></textarea>
     <input type = 'submit' value='Update'>
     </form>
    Call this one pagetwo.php
    PHP Code:
    <?php 
    session_start
    ();
    $_SESSION['test']= $_POST['input'];
    echo 
    $_POST['input']."<br />";
    echo 
    $_SESSION['test'];
    echo
    "<br />Go to next page<a href='thirdpage.php'>click here<a/>";
     
    ?>
    and call this one thirdpage.php
    PHP Code:
    <?php 
    session_start
    ();
    echo 
    $_SESSION['test'];
    echo 
    "<br />See how this works? ";
    echo 
    "<a href='posting.php'>Back to the form</a>";
     
    ?>
    Now open the posting.php with a browser and enter something into the textarea then hit Update and it will go to the next page where it will echo the $_POST andd the $_SESSION variable then click the link and it will send you back to postin.php and now using the $_SESSION variable then what you entered originally will be there using the $_SESSION variable, it is as easy as that.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Posts
    996
    Rep Power
    15
    Ok,
    I have had a go at using them, but weird things happen..

    I enter text, then hit update and it adds lots of breaks in the text area box. Also, wheni click back on the page, it still looses everythign.

    This is what i have, not sure if its right:

    Form page

    PHP Code:

    <?php  
    session_start
    (); 
    //Now you can use the session data 


    $news $_POST[Newnews]; 
    $query "insert into job_story set news = '$news' where  news = \"$_SESSION[news]\" "
    $result mysql_query($query); 

    $sk001 "UPDATE job_story SET news = '$news' where  news = \"$_SESSION[news]\" ";  
    $rsk002 mysql_query($sk001) or die(mysql_error()); 

    $qsk "select * from job_story where  news = \"$_SESSION[news]\" "
    $rsk mysql_query($qsk) or die(mysql_error()); 
    $ask mysql_fetch_array($rsk); 

     
    ?>



    <form action="addnews.php" method=post name=f1>
    <textarea rows=6 cols=40 name="Newnews">
    <?php echo$_SESSION['news']; ?>      
    </textarea>
          
    <input type=submit name=s1 value="Add or Update News" align=left> </form>
    preview page
    PHP Code:
    <?php  
    session_start
    (); 
    //Now you can use the session data 


    $news $_POST[Newnews]; 
    $query "insert into job_story set news = '$news' where  news = \"$_SESSION[news]\" "
    $result mysql_query($query); 

    $sk001 "UPDATE job_story SET news = '$news' where  news = \"$_SESSION[news]\" ";  
    $rsk002 mysql_query($sk001) or die(mysql_error()); 

    $qsk "select * from job_story where  news = \"$_SESSION[news]\" "
    $rsk mysql_query($qsk) or die(mysql_error()); 
    $ask mysql_fetch_array($rsk); 

     
    ?>


    <?php  
    session_start
    (); 
    $_SESSION['news']= $_POST['Newnews']; 
    echo 
    $_POST['Newnews']; 
    echo 
    $_SESSION['news']; 
     
    ?>
  26. #14
  27. No Profile Picture
    Lost in the 70's
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    Nashville TN USA
    Posts
    252
    Rep Power
    21
    Let me work with your code and see what I can do with what you have, there are some problems with it but they can be fixed so give me a bit of time.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Posts
    996
    Rep Power
    15
    ok thank you
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo