Saving text area into mySQL database field PHP

66,797

Try something like the following:

<?php
    if ($_POST['submit']) {
        mysql_connect ("localhost", "user", "password") or die ('Error: ' . mysql_error());
        mysql_select_db("databasename") or die ('Data error:' . mysql_error());
        $text = mysql_real_escape_string($_POST['comments']); 
        $query="INSERT INTO KeepData (player_data) VALUES ('$text')";
        mysql_query($query) or die ('Error updating database' . mysql_error());
    }
?>


<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <textarea name="comments">Example Comment</textarea>
    <input name="submit" type="submit" value="submit" />
</form>
Share:
66,797
DIM3NSION
Author by

DIM3NSION

Updated on October 21, 2020

Comments

  • DIM3NSION
    DIM3NSION over 3 years

    Hi i am using openWYSIWYG as a text editor for a text area. I then am trying to post the contents of the text area to a field in my database.

    This is the code i have so far -

    <?php
    $text = $_GET['Comments']; 
    
    
    
    mysql_connect ("localhost", "user", "password") or die ('Error: ' . mysql_error());
    mysql_select_db("databasename") or die ('Data error:' . mysql_error());
    
    $query="INSERT INTO KeepData (player_data)VALUES ('$text')";
    
    mysql_query($query) or die ('Error updating database' . mysql_error());
    
    
    
    
    ?> 
    

    I can connect to the database, and when i click submit it adds a blank entry into the field? how would i get it so it keeps all the formatted data?

    Many thanks

    update

     <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
     <textarea id="Comments" name="Comments">
     example text
     </textarea>
    <input type="submit" name="mysubmit" value="Save Post" />
     </form>
    

    DIM3NSION

  • DIM3NSION
    DIM3NSION over 12 years
    Thanks for your reply i have tried this but again it adds a blank field. Ive updated my post with my form information. Any help would be greatly appreciated
  • Kasia Gogolek
    Kasia Gogolek over 12 years
    use this and change $text = mysql_real_escape_string($_POST['comments']);
  • Mat
    Mat over 12 years
    What output do you get if you echo $query?
  • devasia2112
    devasia2112 over 7 years
    Check the data type in your database table.
  • devasia2112
    devasia2112 over 7 years
    Or just use REQUEST