Jump to content

Text Area Data into Mysql


superhoops

Recommended Posts

I have a table on my database with 5 categories and when someone submits the form, all the data appears on the database correctly apart from a field called Comments where people submit data on a textarea on the form and nothing shows up on the database:

Here is the code for my insert page:

[code]
$Name = addslashes($_POST['Name']);
$Comment = addslashes($_POST['Comment']);
$sql="INSERT INTO Pre SET
        Name = '".$Name."',
        Team = '".$_POST['Team']."',
        Rating = '".$_POST['Rating']."',
        Comment = '".$Comment."'";
// echo $sql;  for testing purposes
[/code]

And here is the code for the text area part of the form:

[code]
<textarea rows="5" name="Comment" cols="34"></textarea>
[/code]

IM sure its an easy thing to fix but if anyone would be able to tell me how to do it or slightly amend the insert page code bit i would be very greatful.
Link to comment
Share on other sites

The form:

[code]<form action="page.php" method="post">
    <textarea name="mytextarea" cols="50" rows="5"></textarea>
    <br />
    <input type="submit" />
</form>[/code]

The php for page.php:
[code]<?php
$link = mysql_connect('host', 'user', 'pass');
mysql_select_db('db', $link);


if (!empty($_POST['mytextarea'])) {
    $sql = "INSERT INTO `table`(`column`) VALUES('" . mysql_real_escape_string($_POST['mytextarea']) . "'";
} else {
    echo 'Input something you numpty!';
}

//mysql_query etc down here.

?>[/code]
Link to comment
Share on other sites

I think you thought i meant that if someone submitted nothing into the textarea nothing would happen, sorry but this is not the problem.

The problem is when people submit data, they fill out all forms and all the results from the forms appear in the databse except what is submitted in the textbox.

Link to comment
Share on other sites

Do you have a comment field in the database? If theres no comment field in the database, it wont add the commnt. Post your table structure for the table called [b]Pre[/b] here.

ALso follow GingerRobots suggestion of echoing $sql, to whether the SQL query is actually correct.
Link to comment
Share on other sites

Yes i have a field called Comments in my table. here is the code for my createtable.php

mysql_select_db("fmpsite_reg", $con);
$sql = "CREATE TABLE Pre
(
id INT AUTO_INCREMENT PRIMARY KEY,
Name varchar(30),
Team varchar(30),
Rating int(2),
Comment varchar(300)
)";

I have checked the database, it definately has a field called Comments.

When I echo $sql should i delete the insert into Pre stuff?

Link to comment
Share on other sites

Well your not performing the query if you are just echo'ing $sql. All what [code=php:0]echo $sql[/code] will do is print whats in $sql to the browser. It wont perform the query.

To run the sql query you need to run it through the mysql_query function. Before you run the query you must be connected to mysql.
Link to comment
Share on other sites

The whole code of the insertpre page is:

<?
$db = mysql_connect("username", "db", "password") or die("Could not connect.");
if(!$db)
  die("no db");
if(!mysql_select_db("fmpsite_reg",$db))
    die("No database selected.");
if(!get_magic_quotes_gpc())
mysql_select_db("fmprotasy_reg", $con);
$Name = addslashes($_POST['Name']);
$Comment = addslashes($_POST['Comment']);
$sql="INSERT INTO Pre SET
        Name = '".$Name."',
        Team = '".$_POST['Team']."',
        Rating = '".$_POST['Rating']."',
        Comment = '".$Comment."'";
echo $sql;
if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }
?>
Link to comment
Share on other sites

So you get no outout what so ever. Looks like there is an error in there somewhere then. Tidied up the code a little. Try this:
[code=php:0]<?php

error_reporting("E_ALL");

echo "connecting to MySQL";

$db = mysql_connect("username", "db", "password") or die("Could not connect.<br /><br />" . mysql_error());

echo "connected to mysql<br /><br />";

echo "Selecting database";

mysql_select_db("fmpsite_reg", $db) or die("No database selected.<br /><br />" . mysql_error());

echo "Database selected<br /><br />";

//if(!get_magic_quotes_gpc())
//    mysql_select_db("fmprotasy_reg", $con);
// NOT SURE ABOUT THE ABOVE SO I COMMENTED IT OUT;

echo "Attempting to insert data into database<br /><br />"

$Name = mysql_real_escape_string($_POST['Name']);
$Comment = mysql_real_escape_string($_POST['Comment']);

$sql = "INSERT INTO `Pre` SET `Name`='".$Name."', `Team`='".$_POST['Team']."', `Rating`='".$_POST['Rating']."', `Comment`='".$Comment."'";

echo "SQL OUTPUT:<br /><code>{$sql}</code><br /><br />";

mysql_query($sql) or die('Error: ' . mysql_error());

echo "Inserted data into database";

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.