Re: textarea to mysql
I got this code to work, but after 4 days of frustration! I created 3 pages instead of the two he has here because that makes it work-with a couple of lines of code inserted.
The first page is the form:
//hands of the post values to a handler
<form name="news" method="post" action="textareatexthandler.php">
<textarea name="comment" cols="90" rows="25" wrap="hard"></textarea><br />
<input type="submit" value="Submit" />
//The handler page only contains the insert function instead of the insert and select function
<?php
$host="serverName";
$username="rainhider";
$password="yeah_right";
$conn = mysql_connect($host, $username, $password) or
die('Error connecting to mysql ' . mysql_error());
$selected = mysql_select_db("rainhid_test", $conn)or
die("Could not select db rainhid_test");
$mysql = array(); // create array of mysql escaped values
$mysql['comment'] = mysql_real_escape_string( $_POST['comment'] );
$sql = "INSERT INTO comments ( comment_text ) VALUES ( '{$mysql['comment']}' )"; //insert your comment to db
mysql_query( $sql );
//I needed to retrieve the last mysql id inserted to the database and this code //retrieves the id number of the last insert and then stores it in the session array so that I can retrieve it on the third page.
session_start();
$_SESSION['$sqlID'] = mysql_insert_id(); // store last id in session array
echo $_SESSION['$sqlID']; //just to make sure it got the number
echo "Success! Your newsletter has been saved and posted!"
?>
//The third page retrieves the last inserted comment(by the last id) and displays it
$host="serverName";
$username="rainhider";
$password="yeah_right";
$conn = mysql_connect($host, $username, $password) or
die('Error connecting to mysql ' . mysql_error());
$selected = mysql_select_db("rainhid_test", $conn)or
die("Could not select db rainhid_test");
session_start();
$sqlID = $_SESSION['$sqlID'];
$sql = "SELECT * FROM comments where comment_id='$sqlID'";
$result = mysql_query( $sql );
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {
$html = array(); //create an array of html formated values.
$html['comment'] = nl2br( stripslashes( htmlentities( $row['comment_text'], ENT_QUOTES, 'UTF-8' ) ) );
echo "<br />{$html['comment']}<hr />";
}
?>
TaDa!
|