Jump to content

Text area ~ MYSQL question


lufituaeb444

Recommended Posts

Hi,

 

I hope this isnt too basic a question, but I have this problem. I want to add a large text area with line breaks to a user registration form, and allow them to update it in their settings. I think I could work with a code snippet of a text area such as this, and also pointers on how to connect it to the table entry. This is a code question really. Please help.

 

Michael  ???

Link to comment
Share on other sites

For mysql datatype use TEXT for that field.

For your form like this:

<textarea name='message' cols='10' rows='25'></textarea>

 

Now in php code:

 

if(isset($_POST['submit'])){
$message=mysql_real_escape_string($_POST['message']);
}

Now to insert new entry to db:

 

$sql="insert into table(message)values('$message');"
$q=mysql_query($sql);

 

Now to update that existing field:

 

$sql="update table set message='$message' where username='$username' OR message_id='$id';"
$q=mysql_query($sql);

 

Hope this will help a bit.

Link to comment
Share on other sites

Mmarif4u,

 

can you give me one more bit of advice here? can you explain how in the code you gave, it connects to the field in the MYSQL database. This may be a basic question, but I am not so familiar with PHP syntax. I should name the MYSQL field 'TEXT' ?

I meant should I name it 'message'?

Link to comment
Share on other sites

Always welcome

Hi!

 

I have another question. Would this return different data depending on the user logged in. ie. depending on the cookies they have, 'message' will bring back their own comments?

 

Also, separately, is the code given above applicable on the page where you view the comments in the user profile. Or should there be different code for this? I suspect the code would be differnet?

 

Please assist  :D

Link to comment
Share on other sites

quick fix: theres an error on the update line that the other guy gave

 

$sql="update table set message='$message' where username='$username' OR message_id='$id';"
$q=mysql_query($sql);

 

it should be

 

$sql="update table set message='$message' where username='$username' OR message_id='$id'";
$q=mysql_query($sql);

Link to comment
Share on other sites

quick fix: theres an error on the update line that the other guy gave

 

$sql="update table set message='$message' where username='$username' OR message_id='$id';"
$q=mysql_query($sql);

 

it should be

 

$sql="update table set message='$message' where username='$username' OR message_id='$id'";
$q=mysql_query($sql);

 

Thanks for the fix, it all has helped me out allot. Can you or anyone give me a pointer on my question about whether anything needs to be done to the user cp view page.

Link to comment
Share on other sites

Hi,

 

If understanding you right then make a guess that the code here is doing update or insert where as when you would be showing you comment in user control panel you would be using "select..." statment to pull the comments from table.

 

i amnot too clear with you view which code exactly you trying to reuse?

 

Regards

Link to comment
Share on other sites

Hi,

 

If understanding you right then make a guess that the code here is doing update or insert where as when you would be showing you comment in user control panel you would be using "select..." statment to pull the comments from table.

 

i amnot too clear with you view which code exactly you trying to reuse?

 

Regards

 

It's the code mmarif4u provided above. I would add it to the registration form, with 'message' being the SQL field, but I wondered how I would retrive it for viewing on the user control panel page.

So if the code above updates the 'message' field, then what would be the code to simply view it on another page?

Link to comment
Share on other sites

jsut an idea, but you may well want to have something like

$sql="select $message where username='$username' OR message_id='$id';"
$q=mysql_query($sql);
while($row = mysql_fetch_array($q, MYSQL_ASSOC))
{
    echo "<textarea name='message' cols='10' rows='25' value='{$row['name']}'></textarea>" .
} 

 

that way when the user loads the page the box will be filled with what is already contained in the database

Link to comment
Share on other sites

jsut an idea, but you may well want to have something like

$sql="select $message where username='$username' OR message_id='$id';"
$q=mysql_query($sql);
while($row = mysql_fetch_array($q, MYSQL_ASSOC))
{
    echo "<textarea name='message' cols='10' rows='25' value='{$row['name']}'></textarea>" .
} 

 

that way when the user loads the page the box will be filled with what is already contained in the database

 

Thanks again.

 

I hate to sound like the prince of all nubes, but can someone sum this up for me? I'd like to cut and past two sets of code, one to the registration page, one to the profile view page. Both need to view this text field.

 

This code has already taught me allot so thanks for the help  :D

Link to comment
Share on other sites

If i'm correct you want to be able to add, change, display and update code in the database?

 

If so it's not too difficult.

 

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'dbname';

$con = mysql_connect($host, $user, $pass);
if($con) {
  mysql_select_db($dbname);
} else {
  echo 'Error: ' . mysql_error();
}

switch($_GET['do'])
{
  default: 
    $sql = sprintf("SELECT `content` FROM `main_page` LIMIT 1");
    $sql = mysql_query($sql) or die('Error: ' . mysql_error());
    $obj = mysql_fetch_object($sql);
    echo '<form action="?do=update" method="post">';
    echo '<textarea name="content">' . $obj->content . '</textarea>';
    echo '<input type="submit" value="Submit" />';
    echo '</form>';
  break;

  case 'update':
    $content = $_POST['content'];
    $content = mysql_real_escape_string($content);
    $sql = sprintf("UPDATE `main_page` SET `content` = '%s'", $message);
    $sql = mysql_query($sql) or die('Error: ' . mysql_error());
    echo 'Updated Successfully.';
  break;
}
?>

 

 

I think thats what you're looking for, although i haven't tested it myself to make sure it works. But i'm confident.

Link to comment
Share on other sites

If i'm correct you want to be able to add, change, display and update code in the database?

 

If so it's not too difficult.

 

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$dbname = 'dbname';

$con = mysql_connect($host, $user, $pass);
if($con) {
  mysql_select_db($dbname);
} else {
  echo 'Error: ' . mysql_error();
}

switch($_GET['do'])
{
  default: 
    $sql = sprintf("SELECT `content` FROM `main_page` LIMIT 1");
    $sql = mysql_query($sql) or die('Error: ' . mysql_error());
    $obj = mysql_fetch_object($sql);
    echo '<form action="?do=update" method="post">';
    echo '<textarea name="content">' . $obj->content . '</textarea>';
    echo '<input type="submit" value="Submit" />';
    echo '</form>';
  break;

  case 'update':
    $content = $_POST['content'];
    $content = mysql_real_escape_string($content);
    $sql = sprintf("UPDATE `main_page` SET `content` = '%s'", $message);
    $sql = mysql_query($sql) or die('Error: ' . mysql_error());
    echo 'Updated Successfully.';
  break;
}
?>

 

 

I think thats what you're looking for, although i haven't tested it myself to make sure it works. But i'm confident.

 

Thanks again. Ill test it all out. If I have any problems I know where to ask!  :D

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.