0

I want the user to post a comment into the database through the textarea and then the database should put the information back onto the site. But this isn't working!

index.php:

<textarea rows="2" cols="71" wrap="physical" placeholder = "Post Something!" name="rant_box"></textarea>
        <input type="submit" value="Post" name="post_btn"/>

<?php
        $getquery = mysql_query("SELECT data FROM status ORDER BY id DESC");

        while($rows = mysql_fetch_array($getquery)){
            $id = $rows['id'];

            $status = $rows['status'];

            echo $status . '<br />' . '<br />';
        }


    ?>

The data is being correctly entered into the database. But calling it back and posting it back onto the index.php page isn't working.

Karthik
  • 25
  • 1
  • 5
  • [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Apr 06 '13 at 22:02
  • Your `select` statement doesn't include the `id` column. Yet you're trying to retrieve it. – hd1 Apr 06 '13 at 22:04
  • I changed SELECT data to SELECT *. But it's still not displaying the data. – Karthik Apr 06 '13 at 22:12
  • If OPs trying to access the column, as line 5 says, there is, IIRC @Sepster – hd1 Apr 06 '13 at 22:12
  • @hjpotter92 so would changing all the mysql_query to mysqli_query and mysql_fetch_array to mysqli_fetch_array do it? – Karthik Apr 06 '13 at 22:15
  • try `$status .= $rows['status'];` if `status` is the comment column! – auicsc Apr 06 '13 at 22:22
  • @hd1 yes, you're right. I missed that line, thought you were suggesting the SQL itself was invalid. – Sepster Apr 06 '13 at 22:23

2 Answers2

0

Your query is selecting a column called 'data' yet you are trying to inspect a column called 'status' in your resultset. If the column is indeed called 'data', then this line

        $status = $rows['status'];

Based on your select query, should be

        $status = $rows['data'];

But if the column is actually called 'status', then you need the replace 'data' with 'status' in your SQL statement.

You're also attempting to access $rows['id'] which is not in your select list. But since you're not using that value in your code, is this required? If it is required, you also need to update your select list to include this column, ie

        SELECT data FROM status ORDER BY id DESC

Should be

        SELECT id, data FROM status ORDER BY id DESC

NB As others have suggested, you shouldn't be using the deprecated mysql_ functions. Refer Choosing an API

Sepster
  • 4,800
  • 20
  • 38
0

First you have to connect to the database, and second you are supposed to say what you are going to use in the SQL statement so : SELECT id,status FROM status OREDER BY ID DESC Try this :

 try
{
    $bdd = new PDO('mysql:host=localhost;dbname=test', 'root', '');
}
catch(Exception $e)
{
        die('Erreur : '.$e->getMessage());
}
 id,statu
$reponse = $bdd->query('SELECT id,status FROM status ORDER BY ID DESC ');


while ($donnees = $reponse->fetch())
{
   echo $donnees['status'];
}

$reponse->closeCursor();

?>
  • THe index.php file has an include statement for my post_to_db.php file which has an include statement to db_connect.php file. – Karthik Apr 06 '13 at 23:05