2015-10-13 90 views
-2

所以我有一個留言驗證碼:刪除MYSQL一行PHP

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0"> 
<tr> 
<td><strong>View Guestbook | <a href="gbook.php">Sign Guestbook</a> </strong></td> 
</tr> 
</table> 
<br> 
<?php 
if(isset($_POST['login'])){ 

    if(empty($_POST['username'])) 
    { 
     $this->HandleError("UserName is empty!"); 
     return false; 
    } 
    if(empty($_POST['password'])) 
    { 
     $this->HandleError("Password is empty!"); 
     return false; 
    } 
    if ($_POST['username'] == "admin" && $_POST['password'] == "pietje"){ 
     echo 'Welkom'; 
     $loggedIn = true; 
     echo '<img width="500" height="375" src="http://vignette4.wikia.nocookie.net/southpark/images/9/9e/Party.gif/revision/latest?cb=20140712092024">'; 

    } else { 
     echo 'Incorrect user or pass'; 
     echo '<img width="500" height="375" src="https://media.giphy.com/media/B1TMcmoBAaSZi/giphy.gif">'; 
    } 
    $username = trim($_POST['username']); 
    $password = trim($_POST['password']); 
} 

    ?> 

<form id='login' method='post' accept-charset='UTF-8'> 
<fieldset > 
<legend>Login</legend> 
<input type='hidden' name='submitted' id='submitted' value='1'/> 

<label for='username' >UserName*:</label> 
<input type='text' name='username' id='username' maxlength="50" /> 

<label for='password' >Password*:</label> 
<input type='password' name='password' id='password' maxlength="50" /> 

<input type='submit' name='login' value='login' /> 

</fieldset> 
</form> 

<?php 

$host="mysql17.000webhost.com"; // Host name 
$username="a1126203_stan"; // Mysql username 
$password=""; // Mysql password 
$db_name="a1126203_gb"; // 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>ID</td> 
<td>:</td> 
<td><? echo $rows['id']; ?></td> 
</tr> 
<tr> 
<td width="117">Name</td> 
<td width="14">:</td> 
<td width="357"><? echo $rows['name']; ?></td> 
</tr> 
<tr> 
<td>Email</td> 
<td>:</td> 
<td><? echo $rows['email']; ?></td> 
</tr> 
<tr> 
<td valign="top">Comment</td> 
<td valign="top">:</td> 
<td><? echo $rows['comment']; ?></td> 
</tr> 
<tr> 
<td valign="top">Date/Time </td> 
<td valign="top">:</td> 
<td><? echo $rows['datetime']; ?></td> 
</tr> 
</table></td> 
</tr> 
<?php 
$id = $rows['id']; 
echo $id; 
if($loggedIn){ 
echo '<tr> 
<td width="100"> </td> 
<td> 
<input name="delete'.$id.'" type="submit" id="delete" value="Delete"> 
</td> 
</tr>'; 
} 
if(isset($_POST['delete'.$id])) 
     { 

$conn = mysql_connect($host, $username, $password); 

      $sql="DELETE FROM guestbook WHERE id='".$id."'"; 
      mysql_select_db('a1126203_gb'); 
      $retval = mysql_query($sql, $conn); 

      if(! $retval) 
      { 
       die('Could not delete data: ' . mysql_error()); 
      } 

      echo "Deleted data successfully\n"; 

      mysql_close($conn); 
     }     
?> 
</table> 
<?php 
if($loggedIn){ 

} 
} 
mysql_close(); //close database 
?> 

Iknow它很難看,但並沒有真正事情ATM,我想提出一個刪除按鈕,每一個新的行,但我想刪除與刪除按鈕具有相同ID的行。但對於一些(可能是顯而易見的)原因,它不起作用。

+5

** **停止使用廢棄的'myql_ *'API。相反,使用'mysqli_ *'或PDO和準備好的語句。調用sql命令後檢查錯誤。 – Jens

+0

我不明白這與我的問題有什麼關係 –

+1

@StanVanDerBend儘管這與您的問題無關,但使用折舊功能並不是一個好習慣,因爲您未來可能無法獲得任何幫助,PDO將有所幫助你要防止SQL注入和其他漏洞 –

回答

1

刪除按鈕不在表單內,因此點擊時不會提交。

放置一個<form>標籤內輸入字段,他們會工作,

<form method="post" action="http://some/url"> 
    <input name="delete'.$id.'" type="submit" id="delete" value="Delete"> 
    ... 
</form> 
0

試試這個 創建單獨的形式像

<form method="post" id='form' action="http://some/url"> 
    <input type="hidden" id="deleteId"> 
</form> 

代碼

<input name="delete'.$id.'" type="submit" id="delete" value="Delete"> 

更換

<input type="button" onclick=delete(.$id.) value="Delete"> 

<script> 
function delete(id){ 
    $('#deleteId').val(id); 
    $('#form').submit(); 
} 
</script>