Super newbie needs help

Reply

Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Super newbie needs help

 
0
  #1
Oct 30th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: Super newbie needs help

 
0
  #2
Oct 30th, 2007
My html:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: Super newbie needs help

 
0
  #3
Oct 30th, 2007
Here is my progress. I have connected to the localhost database and inserted text.

Insertion code:

  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.

  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>
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 569
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: Super newbie needs help

 
1
  #4
Oct 30th, 2007
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.

  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:

  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:

  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: Super newbie needs help

 
0
  #5
Oct 31st, 2007
Hi, thanks. Your code works with the following change: $SQl = " SELECT post FROM posts ";
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: Super newbie needs help

 
0
  #6
Oct 31st, 2007
Actually, there are problems with the code provided. I had mistakenly copied my own back in. This works ---

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 569
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: Super newbie needs help

 
0
  #7
Oct 31st, 2007
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC