在线留言 - 提交您的宝贵意见
<!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> <form action="liuyan.php" method="POST">
<table width="480" border="0" align="center">
<tbody>
<tr>
<td height="50"><input name="username" type="text" placeholder="用户名" style=" width:300px; height:30px;"></td>
</tr>
<tr>
<td height="50"><input name="email" type="text" placeholder="邮箱" style=" width:300px; height:30px;"></td>
</tr>
<tr>
<td height="220"><textarea name="message" cols="" rows="" placeholder="留言内容" style=" width:400px; height:200px;"></textarea></td>
</tr>
</tbody>
</table>
<table width="480" border="0" height="100px" align="center">
<tbody>
<tr>
<td width="224"><button type="submit">提交</button></td>
<td width="566"><button type="reset">重置</button></td>
</tr>
</tbody>
</table>
</form>
</td>
</code></pre>
</tr>
<tr>
<td height="200" align="center" bgcolor="#000000">
<pre><code> <p class="foot">版权所有© 我的个人网站</p>
</td>
</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 = "INSERT INTO messages (username, email, message) VALUES ('$submittedUsername', '$email', '$message')";$sql = "INSERT INTO messages (username, email, message) VALUES ('$username', '$email', '$message')";
</code></pre>
<p>修改为:</p>
<pre><code class="language-php">$sql = "INSERT INTO messages (username, email, message) VALUES ('$submittedUsername', '$email', '$message')";
</code></pre>
<p>最后,你可能还需要将header()函数的位置移动到所有输出之前,以避免“Cannot modify header information”错误。可以将header()函数放在代码的最前面,或者使用ob_start()函数在输出之前开启输出缓冲区。</p>
<p>希望这些修改可以解决你的问题。</p>
原文地址: https://www.cveoy.top/t/topic/f3BO 著作权归作者所有。请勿转载和采集!