943,925 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 924
  • PHP RSS
Oct 30th, 2007
0

Super newbie needs help

Expand Post »
I have written lots of php dealing with html strings, but it's my first time trying to get a very simple page to function. It has a textarea, and I guess that all I need to do is get the text into the database on the server. I really have no idea how to go about this, and I'm so frustrated that I can't find what I'm looking for. It's simple (again, guessing) as all there is, for now, is the text area. The client (user) types in some text, sends it, after previewing it through my nifty string work :-) and it goes to the database. It would be very nice of someone to walk me through this. I'm the type of learner who needs guidance over "solutions". Sorry if I seem dumb, but I just need help. I'm only a hobbyist programmer.
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Oct 30th, 2007
0

Re: Super newbie needs help

My html:
PHP Syntax (Toggle Plain Text)
  1. <form action="insert.php" method="post" name="form"> <br/><span style="position:relative: left: -20%; font-size: small;"></span><br/>
  2. <textarea name="text" id="DynamicText" style="font-family: arial, helvetica, sans-serif; font-size: large;" cols="100" rows="5" wrap="soft" onclick="document.form.text.value='';">
  3. Lorem ipsum . . .
  4. </textarea>
  5. <br/>
  6. <br/>
  7. <input type="button" value="Preview" onclick="reloadTextDiv();" />
  8. </form>
PHP:
PHP Syntax (Toggle Plain Text)
  1. $username = "-------";
  2. $password = "-------";
  3. $database = "----";
  4.  
  5. $text = $_POST['text'];
  6.  
  7. mysql_connect("mysql.gravityway.com", $username, $password);
  8.  
  9. @mysql_select_db($database) or die("Unable to select database");
  10.  
  11. $query = "INSERT INTO ---- VALUES ('','$first')";
  12.  
  13. mysql_query($query);
  14.  
  15. mysql_close();
Last edited by tefflox; Oct 30th, 2007 at 5:14 am.
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Oct 30th, 2007
0

Re: Super newbie needs help

Here is my progress. I have connected to the localhost database and inserted text.

Insertion code:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $username = "root";
  4. $password = "-------";
  5. $database = "grav";
  6. $host = "localhost";
  7.  
  8. $text = $_POST['text'];
  9.  
  10. $cid = mysql_connect($host, $username, $password);
  11.  
  12. if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
  13.  
  14. if ($_SERVER['REQUEST_METHOD'] == "POST") {
  15. /*
  16.   // the following 4 lines are needed if your server has register_globals set to Off
  17.   $category = $_POST['category'];
  18.   $sitename = $_POST['sitename'];
  19.   $siteurl = $_POST['siteurl'];
  20.   $description = $_POST['description'];
  21. */
  22. $SQL = " INSERT INTO posts VALUES ('', '$text')";
  23.  
  24. $result = mysql_db_query($database, $SQL, $cid);
  25.  
  26. if (!$result) {
  27. echo("ERROR: " . mysql_error() . "\n".$SQL."\n"); }
  28. else
  29. echo ("New Post Added\n");
  30.  
  31. }
  32.  
  33. mysql_close($cid);
  34.  
  35.  
  36. ?>
I need help with retrieving the data. This is mainly copynpaste code, but it's a start. I get an unknown category error. The table has two columns, the int key and a text field.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $username = "root";
  4. $password = "-------";
  5. $database = "grav";
  6. $host = "localhost";
  7.  
  8. # connect to database
  9. $cid = mysql_connect($host,$username,$password);
  10. if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
  11. ?>
  12. <html>
  13. <head>
  14. <title>Display Posts</title>
  15. </head>
  16. <body>
  17.  
  18. <?php
  19. $category = "Posts";
  20.  
  21. # setup SQL statement
  22. $SQL = " SELECT * FROM posts WHERE category = '$category' ";
  23.  
  24. # execute SQL statement
  25. $retid = mysql_db_query($database, $SQL, $cid);
  26.  
  27. # check for errors
  28. if (!$retid) { echo( mysql_error()); }
  29. else {
  30.  
  31. # display results
  32. echo ("<h3><b>$category</b></h3>\n");
  33. while ($row = mysql_fetch_array($retid)) {
  34. $post = $row["post"];
  35. echo ($post."<br/>\n");
  36. }
  37. }
  38. ?>
  39.  
  40. </body>
  41. </html>
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Oct 30th, 2007
1

Re: Super newbie needs help

Quote ...
I need help with retrieving the data. This is mainly copynpaste code, but it's a start. I get an unknown category error. The table has two columns, the int key and a text field.

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $username = "root";
  4. $password = "-------";
  5. $database = "grav";
  6. $host = "localhost";
  7.  
  8. # connect to database
  9. $cid = mysql_connect($host,$username,$password);
  10. if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
  11. ?>
  12. <html>
  13. <head>
  14. <title>Display Posts</title>
  15. </head>
  16. <body>
  17.  
  18. <?php
  19. $category = "Posts";
  20.  
  21. # setup SQL statement
  22. $SQL = " SELECT * FROM posts WHERE category = '$category' ";
  23.  
  24. # execute SQL statement
  25. $retid = mysql_db_query($database, $SQL, $cid);
  26.  
  27. # check for errors
  28. if (!$retid) { echo( mysql_error()); }
  29. else {
  30.  
  31. # display results
  32. echo ("<h3><b>$category</b></h3>\n");
  33. while ($row = mysql_fetch_array($retid)) {
  34. $post = $row["post"];
  35. echo ($post."<br/>\n");
  36. }
  37. }
  38. ?>
  39.  
  40. </body>
  41. </html>

please replace this part of your code:

php Syntax (Toggle Plain Text)
  1. <?php
  2. $category = "Posts";
  3.  
  4. # setup SQL statement
  5. $SQL = " SELECT * FROM posts WHERE category = '$category' ";
  6.  
  7. # execute SQL statement
  8. $retid = mysql_db_query($database, $SQL, $cid);
  9.  
  10. # check for errors
  11. if (!$retid) { echo( mysql_error()); }
  12. else {
  13.  
  14. # display results
  15. echo ("<h3><b>$category</b></h3>\n");
  16. while ($row = mysql_fetch_array($retid)) {
  17. $post = $row["post"];
  18. echo ($post."<br/>\n");
  19. }
  20. }
  21. ?>

with this one:

php Syntax (Toggle Plain Text)
  1. $category = "Posts";
  2.  
  3. # setup SQL statement
  4. $SQL = " SELECT * FROM posts WHERE category = '$category' ";
  5.  
  6. # execute SQL statement
  7. $retid = mysql_query($query);
  8. $result=mysql_query($retid) or die (mysql_error());
  9. $num=mysql_num_rows($result);
  10.  
  11. # display results
  12. while ($i<$num)
  13. {
  14. $post=mysql_result($result,$i,"post");
  15. echo "<h3><b>'.$category.'</b></h3>\n";
  16. echo $post;
  17. $i++;
  18. }

I think that you are getting an unknown column because the field "post" is not the column name of what you are trying to display.
Look at your database, look what field do you want to display and replace "post" in
php Syntax (Toggle Plain Text)
  1. $post=mysql_result($result,$i,"post");

with that field.
Or better yet please display here all your table fields so I can guide you.
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
Oct 31st, 2007
0

Re: Super newbie needs help

Hi, thanks. Your code works with the following change: $SQl = " SELECT post FROM posts ";
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Oct 31st, 2007
0

Re: Super newbie needs help

Actually, there are problems with the code provided. I had mistakenly copied my own back in. This works ---

PHP Syntax (Toggle Plain Text)
  1. # setup SQL statement
  2. $SQL = " SELECT post FROM posts ";
  3.  
  4. # execute SQL statement
  5. $retid = mysql_db_query($database, $SQL, $cid);
  6.  
  7. # check for errors
  8. if (!$retid) { echo( mysql_error()); }
  9.  
  10. else {
  11.  
  12. # display results
  13. echo ("<h3><b>Posts</b></h3>\n");
  14.  
  15. while ($row = mysql_fetch_array($retid)) {
  16. $post = $row["post"];
  17. echo ($post."<br/>\n");
  18. }
  19. }
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Oct 31st, 2007
0

Re: Super newbie needs help

so,that's why I am wondering on where did you get that SQL statement.Actually mysql_fetch_array is another way of displaying data but for me my way of displaying data is what I have shown.But anyway,it's still the same!
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Prerequisite?
Next Thread in PHP Forum Timeline: Export RSS 1.0 or 2.0 feeds to an array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC