如何让MySQL数据库出现在index.php上

时间:2022-09-25 15:47:25

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.

我在我的网站上有一个提交表单(index.php),我将数据(用户提交)存储到MySQL数据库中。现在,我让用户提交帖子,然后页面将他们引导到update.php,显示他们输入的内容。

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... ?

但是,我希望MySQL中数据库中的所有数据都显示在index.php上。这很像评论系统。用户提交帖子...并在同一页面上看到他们的帖子高于其他提交的帖子。我想我错过了AJAX ......?

Here is the code for index.php

这是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:

在update.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', '".$text."')";

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

   echo " $myString "," $text ";

    ?>

Thanks a lot!

非常感谢!

2 个解决方案

#1


3  

I fixed your HTML a bit, mainly to get rid of the nested form tags:

我修改了你的HTML,主要是为了摆脱嵌套的表单标签:

<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}.
  • 更改为
    ,然后在CSS集#container {text-align:center}中。

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

      ,而不是#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.
  • 删除 更改为

    删除 并添加到.Title CSS规则: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.
  • AJAX使它变得更加复杂,所以我没有使用它。

  • Now, when someone submits to update.php, the submitted text will appear, and underneath it all the previous submissions.
  • 现在,当有人提交update.php时,将显示提交的文本,并在其下面显示所有先前的提交。

  • 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
  • 我添加了mysql_real_escape_string()来防止SQL注入。这非常重要,或者恶意用户可以执行删除所有数据等操作。请参阅:Google,Wikipedia,Obligatory xkcd

  • I added mysql_error() so if your SQL fails, you will have a better idea why.
  • 我添加了mysql_error(),所以如果你的SQL失败了,你会更好地了解原因。

#2


0  

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)

要在同一页面中显示输入的数据,您必须创建一个函数,使用AJAX将页面中的元素添加为p,标记h3标记等,在其中输入要显示的数据等,并调用onsubmit事件:(见上)

#1


3  

I fixed your HTML a bit, mainly to get rid of the nested form tags:

我修改了你的HTML,主要是为了摆脱嵌套的表单标签:

<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}.
  • 更改为
    ,然后在CSS集#container {text-align:center}中。

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

      ,而不是#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.
  • 删除 更改为

    删除 并添加到.Title CSS规则: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.
  • AJAX使它变得更加复杂,所以我没有使用它。

  • Now, when someone submits to update.php, the submitted text will appear, and underneath it all the previous submissions.
  • 现在,当有人提交update.php时,将显示提交的文本,并在其下面显示所有先前的提交。

  • 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
  • 我添加了mysql_real_escape_string()来防止SQL注入。这非常重要,或者恶意用户可以执行删除所有数据等操作。请参阅:Google,Wikipedia,Obligatory xkcd

  • I added mysql_error() so if your SQL fails, you will have a better idea why.
  • 我添加了mysql_error(),所以如果你的SQL失败了,你会更好地了解原因。

#2


0  

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)

要在同一页面中显示输入的数据,您必须创建一个函数,使用AJAX将页面中的元素添加为p,标记h3标记等,在其中输入要显示的数据等,并调用onsubmit事件:(见上)