lufituaeb444 Posted January 29, 2008 Share Posted January 29, 2008 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 ??? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted January 29, 2008 Share Posted January 29, 2008 you have not mentioned what problem you are having... Quote Link to comment Share on other sites More sharing options...
lufituaeb444 Posted January 29, 2008 Author Share Posted January 29, 2008 I dont know the code to add to a form for a large text field, and how to connect it to a MYSQL table entry. If someone could show me this I'm sure I could work it out? Quote Link to comment Share on other sites More sharing options...
priti Posted January 29, 2008 Share Posted January 29, 2008 As i am not aware of your text size i would suggest to go for TEXT datatype to store large text files. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted January 29, 2008 Share Posted January 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
lufituaeb444 Posted January 29, 2008 Author Share Posted January 29, 2008 thanks thats brilliant! Ill give it a shot now. if I have any problems I know where to post em Quote Link to comment Share on other sites More sharing options...
lufituaeb444 Posted January 29, 2008 Author Share Posted January 29, 2008 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' ? Quote Link to comment Share on other sites More sharing options...
lufituaeb444 Posted January 29, 2008 Author Share Posted January 29, 2008 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'? Quote Link to comment Share on other sites More sharing options...
priti Posted January 29, 2008 Share Posted January 29, 2008 mysql field name should be message and select the datatype as TEXT. Quote Link to comment Share on other sites More sharing options...
lufituaeb444 Posted January 29, 2008 Author Share Posted January 29, 2008 mysql field name should be message and select the datatype as TEXT. thank you all very much Quote Link to comment Share on other sites More sharing options...
priti Posted January 30, 2008 Share Posted January 30, 2008 Always welcome Quote Link to comment Share on other sites More sharing options...
lufituaeb444 Posted January 31, 2008 Author Share Posted January 31, 2008 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 Quote Link to comment Share on other sites More sharing options...
uwannadonkey Posted January 31, 2008 Share Posted January 31, 2008 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); Quote Link to comment Share on other sites More sharing options...
lufituaeb444 Posted February 1, 2008 Author Share Posted February 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
priti Posted February 1, 2008 Share Posted February 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
lufituaeb444 Posted February 1, 2008 Author Share Posted February 1, 2008 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? Quote Link to comment Share on other sites More sharing options...
blueman378 Posted February 1, 2008 Share Posted February 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
lufituaeb444 Posted February 1, 2008 Author Share Posted February 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
Wolphie Posted February 1, 2008 Share Posted February 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
lufituaeb444 Posted February 1, 2008 Author Share Posted February 1, 2008 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.