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.

Recommended Answers

All 6 Replies

My html:

<form action="insert.php" method="post" name="form"> <br/><span style="position:relative: left: -20%; font-size: small;"></span><br/>
<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='';">
Lorem ipsum . . .
  </textarea>
  <br/>
  <br/>
<input type="button" value="Preview" onclick="reloadTextDiv();" />
  </form>

PHP:

$username = "-------";
  $password = "-------";
  $database = "----";
  
  $text = $_POST['text']; 
  
  mysql_connect("mysql.gravityway.com", $username, $password); 
  
  @mysql_select_db($database) or die("Unable to select database"); 
  
  $query = "INSERT INTO ---- VALUES ('','$first')";
  
  mysql_query($query);

  mysql_close();

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

Insertion code:

<?php

  $username = "root";
  $password = "-------";
  $database = "grav";
  $host     = "localhost";
  
  $text = $_POST['text']; 
  
  $cid  = mysql_connect($host, $username, $password); 
  
  if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
  
  if ($_SERVER['REQUEST_METHOD'] == "POST") {
/*
  // the following 4 lines are needed if your server has register_globals set to Off
  $category = $_POST['category'];
  $sitename = $_POST['sitename'];
  $siteurl = $_POST['siteurl'];
  $description = $_POST['description'];
*/
  $SQL = " INSERT INTO posts VALUES ('', '$text')";
  
  $result = mysql_db_query($database, $SQL, $cid);

  if (!$result) {
      echo("ERROR: " . mysql_error() . "\n".$SQL."\n"); }
  else
      echo ("New Post Added\n");

  }

  mysql_close($cid);  
  
     
?>

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

  $username = "root";
  $password = "-------";
  $database = "grav";
  $host     = "localhost";

    # connect to database
    $cid = mysql_connect($host,$username,$password);
    if (!$cid) { echo("ERROR: " . mysql_error() . "\n");    }
?>
<html>
 <head>
  <title>Display Posts</title>
 </head>
 <body>
 
<?php
    $category = "Posts";

    # setup SQL statement
    $SQL = " SELECT * FROM posts WHERE category = '$category' ";

    # execute SQL statement
    $retid = mysql_db_query($database, $SQL, $cid);

    # check for errors
    if (!$retid) { echo( mysql_error()); }
      else {

          # display results
          echo ("<h3><b>$category</b></h3>\n");
           while ($row = mysql_fetch_array($retid)) {
              $post = $row["post"];
              echo ($post."<br/>\n");
          }
    }
?>

</body>
</html>

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

  $username = "root";
  $password = "-------";
  $database = "grav";
  $host     = "localhost";

    # connect to database
    $cid = mysql_connect($host,$username,$password);
    if (!$cid) { echo("ERROR: " . mysql_error() . "\n");    }
?>
<html>
 <head>
  <title>Display Posts</title>
 </head>
 <body>
 
<?php
    $category = "Posts";

    # setup SQL statement
    $SQL = " SELECT * FROM posts WHERE category = '$category' ";

    # execute SQL statement
    $retid = mysql_db_query($database, $SQL, $cid);

    # check for errors
    if (!$retid) { echo( mysql_error()); }
      else {

          # display results
          echo ("<h3><b>$category</b></h3>\n");
           while ($row = mysql_fetch_array($retid)) {
              $post = $row["post"];
              echo ($post."<br/>\n");
          }
    }
?>

</body>
</html>

please replace this part of your code:

<?php
    $category = "Posts";

    # setup SQL statement
    $SQL = " SELECT * FROM posts WHERE category = '$category' ";

    # execute SQL statement
    $retid = mysql_db_query($database, $SQL, $cid);

    # check for errors
    if (!$retid) { echo( mysql_error()); }
      else {

          # display results
          echo ("<h3><b>$category</b></h3>\n");
           while ($row = mysql_fetch_array($retid)) {
              $post = $row["post"];
              echo ($post."<br/>\n");
          }
    }
?>

with this one:

$category = "Posts";

    # setup SQL statement
    $SQL = " SELECT * FROM posts WHERE category = '$category' ";

    # execute SQL statement
    $retid = mysql_query($query);
  $result=mysql_query($retid) or die (mysql_error());
   $num=mysql_num_rows($result);
 
# display results
while ($i<$num)
{
  $post=mysql_result($result,$i,"post");
echo "<h3><b>'.$category.'</b></h3>\n";
echo $post;          
$i++;
}

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

$post=mysql_result($result,$i,"post");

with that field.
Or better yet please display here all your table fields so I can guide you.

commented: thanks +2

Hi, thanks. Your code works with the following change: $SQl = " SELECT post FROM posts ";

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

# setup SQL statement
  $SQL = " SELECT post FROM posts ";

  # execute SQL statement
  $retid = mysql_db_query($database, $SQL, $cid);
 
  # check for errors
  if (!$retid) { echo( mysql_error()); }

  else {

    # display results
    echo ("<h3><b>Posts</b></h3>\n");

    while ($row = mysql_fetch_array($retid)) {
      $post = $row["post"];
      echo ($post."<br/>\n");
    }
  }

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.