Jump to content


Photo

Text Area Data into Mysql


  • Please log in to reply
25 replies to this topic

#1 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 12:25 PM

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:

$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

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

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

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.

#2 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 01:14 PM

Please?

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

#3 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 02:13 PM

sanitise all input for mysql with mysql_real_escape_string();

#4 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 03:40 PM

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?

#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 03:45 PM

The form:

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

The php for page.php:
<?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.

?>


#6 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 04:15 PM

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.



#7 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 04 September 2006 - 04:17 PM

What is shown after $sql is echoed?

#8 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 04:18 PM

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.

#9 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 04:20 PM

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 Pre here.

ALso follow GingerRobots suggestion of echoing $sql, to whether the SQL query is actually correct.

#10 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 04 September 2006 - 04:22 PM

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.

#11 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 04:28 PM

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?



#12 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 04 September 2006 - 04:30 PM

It doesn't matter. We just need to see what the query actually is doing.

#13 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 04:34 PM

I added it, submitted the form and the page went blank after the form was submitted like before.

#14 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 04:36 PM

dude.. echo the value of $sql and paste it here.. for the love of god..

#15 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 04:38 PM

ive given you all the code that is required right?

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

echo '$sql';

That correct?

#16 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 04:50 PM

Well your not performing the query if you are just echo'ing $sql. All what
echo $sql
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.

#17 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 04:51 PM

I am connected, could someone just give me this code to run this query.

#18 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 05:04 PM

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

#19 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 05:06 PM

Now I'm confused. You are running the query, see:
if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }


#20 superhoops

superhoops
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 04 September 2006 - 05:12 PM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users