Jump to content

Archived

This topic is now archived and is closed to further replies.

superhoops

Text Area Data into Mysql

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.

Share this post


Link to post
Share on other sites
Please?

[b]EDITED BY WILDTEEN88: DO NOT BUMP YOUR THREAD UNLESS IT IS ATLEAST A COUPLE OF HOURS OLD (2HRS MIN). IDEALLY YOU SHOULD BUMP YOUR THREAD EVERY 12-24 HOURS, BUT AS THE PHP HELP FORUM CAN MOVE ALONG VERY FAST 2 HOURS IS FINE[/B]

Share this post


Link to post
Share on other sites
sanitise all input for mysql with mysql_real_escape_string();

Share this post


Link to post
Share on other sites
I don't understand sorry. I had a previous table in my database and the form i had submitted all the data correctly and i put the addslashes command for the fields with a textbox in. I haven't yet before this one had a text area though so i was wondering what i do?

Share this post


Link to post
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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Do you mean after the form is submitted? it goes blank but thats fine, it did that with my other page as well. Its getting the textarea info into the database.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Ah i only just noticed that you had commented this line out:
// echo $sql;  for testing purposes

remove the comments to see what the query is showing.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
dude.. echo the value of $sql and paste it here.. for the love of god..

Share this post


Link to post
Share on other sites
ive given you all the code that is required right?

This is the code i need to use to run $sql is

[code]
echo '$sql';
[/code]

That correct?

Share this post


Link to post
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.

Share this post


Link to post
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());
  }
?>

Share this post


Link to post
Share on other sites
Now I'm confused. You are running the query, see:
[code]if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }[/code]

Share this post


Link to post
Share on other sites
I always had that in the page just i didn't show that code. Do you know how to solve the problem of the textarea data not showing on the databse now?

Share this post


Link to post
Share on other sites
As many other people have requested, and you have failed to do. Post the output of [code=php:0]echo $sql;[/code] here.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
I get this message with your code:
[b]
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/www/fmprotasy.com/insertpre.php on line 23[/b]

Share this post


Link to post
Share on other sites
Add a semi-colon at the end of this line:
[nobbc]echo "Attempting to insert data into database<br /><br />"[/nobbc]

It should be this
[nobbc]echo "Attempting to insert data into database<br /><br />";[/nobbc]

Share this post


Link to post
Share on other sites

×

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.