2014-01-14 59 views
0

我在我的留言簿在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>&nbsp;</td> 
<td>&nbsp;</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>&nbsp;&nbsp;&nbsp;<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

更新後未設置變量後.. – user1844933

+0

實際上保存不止一次到您的數據數據庫還是隻顯示那樣? – NewInTheBusiness

+0

@NewInTheBusiness它不止一次被保存。大約6次。 – pingboo23

回答

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>&nbsp;&nbsp;&nbsp;<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(); 
?> 
+0

這工作正常,但問題是,它擾亂了我的數據庫中的數據安排。隆隆的數據,而不是按ID順序。 – pingboo23

0

無法執行您的查詢6次,直到您通過重新加載或任何ajax請求重新提交表單。如果你的查詢被執行,那麼它應該在$ result中返回true。您可以使用內部條件的exit(),以檢查是否正常工作,並嘗試檢查,如果提交的表單提交這樣的按鈕:

<?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>&nbsp;&nbsp;&nbsp;<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"; 
    }