Q: How to get MySQL database to appear on index.php I have a submission form on my website (index.php) and I have the data(user submissions) being stored into a MySQL database. Right now, I have the user submitting a post and then the page directs them to an update.php which shows what they inputed.
However, I want all of the data in the database in MySQL to be shown on the index.php. It's a lot like a comment system. User submits a post... and sees their post above the other submitted posts all on the same page. I think I'm missing AJAX... ?
Here is the code for index.php
   <div align="center">
 <p>&nbsp;</p>
 <h2 align="center" class="Title"><em><strong>REDACTED</strong></em></h2>
 <form id="form1" name="form1" method="post" action="update.php">
<hr />
<label><br />
  <form action="update.php" method="post">
 REDACTED: <input type="text" name="text" />
  <input type="submit" />
  </form>
  </label>
</form>
</div>

On update.php I have this: 
   ?php
   $text = $_POST['text'];
   $myString = "REDACTED";

   mysql_connect ("db----.net", "-----3", "------------") or die ('Error: ' . mysql_error());
   mysql_select_db ("-----------");

   $query="INSERT INTO TextArea (ID, text) VALUES ('NULL', '".$text."')";

   mysql_query($query) or die ('Error updating database');

   echo " $myString "," $text ";

    ?>

Thanks a lot!

A: I fixed your HTML a bit, mainly to get rid of the nested form tags:
<div align="center">
    <p>&nbsp;</p>
    <h2 align="center" class="Title"><em><strong>REDACTED</strong></em></h2>
    <form method="post" action="update.php">
        <label for="text">REDACTED:</label> <input type="text" name="text" />
        <input type="submit" />
    </form>
</div>

It still isn't fantastic though, you should:


*

*Change <div align="center"> to <div id="container">, and then in your CSS set #container {text-align:center}.

*Get rid of <p>&nbsp;</p>, instead #container {padding-top:24px}.

*Change <h2 align="center" class="Title"><em><strong>REDACTED</strong></em></h2> into <h2>REDACTED</h2> and add to the .Title CSS rule: text-align: center; font-weight: bold; font-style: italic.

*Your input should probably have a better name than text.


Adjusted PHP:
<?php

$text = $_POST['text'];
$myString = "REDACTED";

mysql_connect ("db----.net", "-----3", "------------") or die ('Error: ' . mysql_error());
mysql_select_db ("-----------");

$query = "INSERT INTO TextArea (ID, text) VALUES (NULL, '" . mysql_real_escape_string($text) . "')";
mysql_query($query) or die ('Error updating database: ' . mysql_error());

echo "$myString - $text<hr />";

$query = "SELECT text FROM TextArea ORDER BY ID DESC";
$result = mysql_query($query) or die ('Error querying database:' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
    echo $row['text'] . '<hr />';
}

?>



*

*AJAX makes it much more complicated, so I didn't use it.

*Now, when someone submits to update.php, the submitted text will appear, and underneath it all the previous submissions.

*I added mysql_real_escape_string() to prevent SQL injection. This is very important to do or malicious users can do things such as delete all your data. See: Google, Wikipedia, Obligatory xkcd

*I added mysql_error() so if your SQL fails, you will have a better idea why.


A: to show the data in the same page, you have to do as under:
<form method="post" action=**"index.php"**>
    <label for="text">REDACTED:</label> <input type="text" name="text" />
    <input type="submit" onsubmit="addInputedData"/>
</form>


and to show the inputted data in the same page, you have to create a function that use AJAX  to add element to your pages as p ,tag h3 tag, etc  in which you input the data you want to displa, etc,and called in the onsubmit event of the :
(look above)
