Take input from user and display on website

Discussion in 'PHP' started by rizwanspirit, Feb 13, 2010.

  1. #1
    Hi.

    I am new to php, i have created a mysql database in my website and now want a script by which i could take some text string from user and display it on my site, the new content is automatically on the top of the page and the older content goes below it,

    Thanks in advance.
     
    rizwanspirit, Feb 13, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Follow this example:

    <?php
    //...mysql connection...
    
    //get the user you want to extract the text from
    $user = $_REQUEST['user'];
    
    //replace table_name, id and user with yours...
    //notice the ORDER BY? - thats to order the text from new too old...
    $sql = mysql_query("SELECT * FROM table_name WHERE user=$user ORDER BY id");
    
    while($row = mysql_fetch_array($sql)){
    
    //echo the text - replace text with the column name of which row you want to echo...
    echo $row['text'];
    
    }
    
    ?>
    PHP:
    If you need anymore help, reply with your mysql table structure... (ie. table name, column names).
     
    danx10, Feb 13, 2010 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I haven't tested so there could be typos, but this ought to do the trick:

    create table posts (post_id int not null auto_increment, content text, primary key(post_id)) character set utf8;
    Code (markup):
    <?php
    
    $database_user = '123';
    $database_password = 'xyz';
    $database_name = 'abc';
    
    if (false === mysql_connect('localhost', $database_user, $database_password))
       die("could not connect to database: " . mysql_error());
    if (false === mysql_select_db($database_name))
       die("could not connect to database: " . mysql_error());
    
    echo <<<EOF
    <html><head><title>sample<title></head><body>
    <form method="post" action="{$_SERVER['PHP_SELF']}">
    <p>Enter your message:</p>
    <textarea name="content"></textarea>
    <p>
       <input type="hidden" name="submitted" value="1">
       <input type="submit" name="whatever" value="Save">
    </p>
    </form>
    EOF;
    
    if ($_POST['submitted'])
    {
       $message = mysql_real_escape_string(strip_tags(trim($_POST['comment'])));
       if (strlen($message))
       {
          $sql = "insert into posts (content) values ('{$message}')";
          $st = mysql_query($sql) or die("Could not insert message: " . mysql_error());
       }
    }
    
    $sql = "select content from posts order by post_id desc";
    $st = mysql_query($sql) or die("Could not retrieve messages: " . mysql_error());
    while ($row = mysql_fetch_row($st))
    {
       echo "<div style=\"padding: 10px; border: solid 1px black; margin: 3px 0;\">{$row[0]}</div>\n";
    }
    
    echo "</body></html>";
    
    ?>
    PHP:
     
    SmallPotatoes, Feb 13, 2010 IP
  4. TakyDine

    TakyDine Peon

    Messages:
    86
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    man , i need this trick but i only read html
    i have to learn php and stuff
     
    TakyDine, Feb 13, 2010 IP