<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>在线留言</title>
  <link href="css/css.css" rel="stylesheet" type="text/css" />
</head>
<body>
</div>
<table width="1000" height="1801" border="0" align="center" class="web">
  <tr>
    <td height="124" class=" logo"><img src="img/logo.png" width="435" height="78" align="left" /></td>
  </tr>
  <tr>
    <td height="55"><table width="1000" border="0">
      <tr class="daohang">
        <td height="40" bgcolor="#990033"><a href="index.html">首页</a></td>
        <td bgcolor="#990033"><a href="xuexiao.html">我的学校</a></td>
        <td bgcolor="#990033"><a href="zhiyuanzhe.html">个人简介</a></td>
        <td bgcolor="#990033"><a href="zuzhi.html">我的经历</a></td>
        <td bgcolor="#990033"><a href="xiangmu.html">我的爱好</a></td>
        <td bgcolor="#990033"><a href="shequ.html">我的偶像</a></td>
        <td bgcolor="#990033"><a href="liuyan.html">在线留言</a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><img src="img/banner1.jpg" width="1010" height="263" /></td>
  </tr>
  <tr>
    <td>
<pre><code>  &lt;form action=&quot;liuyan.php&quot; method=&quot;POST&quot;&gt;
    &lt;table width=&quot;480&quot; border=&quot;0&quot; align=&quot;center&quot;&gt;
      &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td height=&quot;50&quot;&gt;&lt;input name=&quot;username&quot; type=&quot;text&quot; placeholder=&quot;用户名&quot; style=&quot; width:300px; height:30px;&quot;&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td height=&quot;50&quot;&gt;&lt;input name=&quot;email&quot; type=&quot;text&quot; placeholder=&quot;邮箱&quot; style=&quot; width:300px; height:30px;&quot;&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td height=&quot;220&quot;&gt;&lt;textarea name=&quot;message&quot; cols=&quot;&quot; rows=&quot;&quot; placeholder=&quot;留言内容&quot; style=&quot; width:400px; height:200px;&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
    &lt;table width=&quot;480&quot; border=&quot;0&quot; height=&quot;100px&quot; align=&quot;center&quot;&gt;
      &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td width=&quot;224&quot;&gt;&lt;button type=&quot;submit&quot;&gt;提交&lt;/button&gt;&lt;/td&gt;
        &lt;td width=&quot;566&quot;&gt;&lt;button type=&quot;reset&quot;&gt;重置&lt;/button&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/form&gt;
&lt;/td&gt;
</code></pre>
  </tr>
  <tr>
    <td height="200" align="center" bgcolor="#000000">
<pre><code>  &lt;p class=&quot;foot&quot;&gt;版权所有© 我的个人网站&lt;/p&gt;

&lt;/td&gt;
</code></pre>
  </tr>
</table>
</body>
</html>
代码如上,怎么解决内容:根据错误提示,问题出在获取表单数据的部分。你需要确保表单中的input元素的name属性与获取数据的代码中的键名一致。
<p>在你的代码中,修改以下部分:</p>
<pre><code class="language-php">$submittedUsername = $_POST['username'];// 修改为从表单中获取用户名的值
$email = $_POST['email'];
$message = $_POST['message'];
</code></pre>
<p>为:</p>
<pre><code class="language-php">$submittedUsername = isset($_POST['username']) ? $_POST['username'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
</code></pre>
<p>这样可以避免未定义数组键的错误。</p>
<p>此外,在插入数据到数据库的代码中,你有一个重复的变量赋值,需要删除其中一个:</p>
<pre><code class="language-php">$sql = &quot;INSERT INTO messages (username, email, message) VALUES ('$submittedUsername', '$email', '$message')&quot;;$sql = &quot;INSERT INTO messages (username, email, message) VALUES ('$username', '$email', '$message')&quot;;
</code></pre>
<p>修改为:</p>
<pre><code class="language-php">$sql = &quot;INSERT INTO messages (username, email, message) VALUES ('$submittedUsername', '$email', '$message')&quot;;
</code></pre>
<p>最后,你可能还需要将header()函数的位置移动到所有输出之前,以避免“Cannot modify header information”错误。可以将header()函数放在代码的最前面,或者使用ob_start()函数在输出之前开启输出缓冲区。</p>
<p>希望这些修改可以解决你的问题。</p>
在线留言 - 提交您的宝贵意见

原文地址: https://www.cveoy.top/t/topic/f3BO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录