我在php的留言簿中遇到了问题。填写留言簿后,它工作正常,但是当我查看我的数据库时,我输入的所有数据都会重复多次。
guestbook.php
<table border="0" width="920" bgcolor="#1d1c1b" id="round"><tr><td>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr><br>
<td><strong><h2>SEACO's Guestbook</h2> </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="name" type="text" id="name" size="40" placeholder="Enter your name here" required/></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" placeholder="Enter your email here (Optional)"/></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" rows="3" id="comment" placeholder="Your comment here" required></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" class="button"/> <input type="reset" name="Submit2" value="Reset" class="button" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
</table><br><center><strong><a href="viewguestbook.php"><button class="button">View Guestbook</button></a> </strong></center>
<br><br></table>
addguestbook.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="chess"; // Database name
$tbl_name="guestbook"; // Table name
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT into $tbl_name(name, email, comment, datetime)values('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
//check if query successful
if($result){
echo "<br><br><center><font color='white' size='5'>Successful</font> <img src='images/cmark.png' width='40px'></center>";
echo "<BR>";
// link to view guestbook page
echo "<center><a href='viewguestbook.php'><button class='button'>View guestbook</button></a></center>";
echo '<br><br>';
}
else {
echo "ERROR";
}
mysql_close();
?>
viewguestbook.php
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><font color="white">View Guestbook</font> | <a href="guestbook.php"><button class="button">Sign Guestbook</button></a> </strong></td>
</tr>
</table>
<br>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="chess"; // Database name
$tbl_name="guestbook"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357" id="input"><?php echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td id="input"><?php echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td id="input"><?php echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td id="input"><?php echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<?php
}
mysql_close(); //close database
?>
</table>
答案 0 :(得分:0)
<?php
if (!isset($_POST['name'])) {
header ("Location: guestbook.php");
exit();
}
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="chess"; // Database name
$tbl_name="guestbook"; // Table name
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT into $tbl_name(name, email, comment, datetime)values('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
unset($_POST['name']);
//check if query successful
if($result){
echo "<br><br><center><font color='white' size='5'>Successful</font> <img src='images/cmark.png' width='40px'></center>";
echo "<BR>";
// link to view guestbook page
echo "<center><a href='viewguestbook.php'><button class='button'>View guestbook</button></a></center>";
echo '<br><br>';
}
else {
echo "ERROR";
}
mysql_close();
?>
答案 1 :(得分:0)
在您通过重新加载或任何ajax请求重新提交表单之前,无法执行您的查询6次。如果执行了查询,那么它应该在$ result中返回true。您可以使用exit()inside条件来检查它是否有效并尝试检查提交按钮提交的表单是否如下所示:
<?php
if(isset($_POST['Submit'])){
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
$tbl_name="users"; // Table name
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT into $tbl_name(name, email, comment, datetime) values ('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
//check if query successful
if($result){
echo "<br><br><center><font color='white' size='5'>Successful</font> <img src='images/cmark.png' width='40px'></center>";
echo "<BR>";
// link to view guestbook page
echo "<center><a href='viewguestbook.php'><button class='button'>View guestbook</button></a></center>";
echo '<br><br>';
exit();
}
else {
echo "ERROR";
}
mysql_close();
}
else{
echo "not set";
}