简体   繁体   中英

How can i retrieve data from a textarea using PHP?

Given the following HTML form:

<form id="form1" name="form1" method="post" action="comments.php">
    <textarea name="text" id="textarea" cols="45" rows="5"></textarea><br/>
    <input type="submit" name="button" id="button" value="Update" />
</form>

...and the following PHP code ( comments.php ):

<?php
require("includes/config.php");
$fromtextarea = $_POST['text'];
$con = mysql_connect($dbserver, $dbusername, $dbpassword);
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname , $con);

$sql = "INSERT INTO textarea (comment) VALUES ('$fromtextarea')";
if (mysql_query($sql)) {
    header("Location: home.php");

}
else
    echo "no no no";
mysql_close($con);

?>

How can I get the data and display all user comments on a page?

Take a look at SELECT sql statement. Your query should look like something like this:

SELECT comment FROM textarea;

Then see how to manipulate the result with mysql_fetch_* functions in PHP ( http://www.php.net/manual/fr/function.mysql-fetch-assoc.php ).

By the way, mysql_* functions are deprecated (and will be deleted soon). I advise you using mysqli_* functions ( http://www.php.net/manual/fr/book.mysqli.php ) or (better) PDO ( http://php.net/manual/fr/book.pdo.php ).

Do like this

$sql = "INSERT INTO textarea (comment) VALUES ('". $_POST["text"] . "')";

Make sure you sanitize it before using it in your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM