使用PHP删除MYSQL行

时间:2015-10-13 08:11:33

标签: php mysql

所以我有一个留言簿的代码:

<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
 ?>

我知道这是难看的,但这并不重要,我正在为每个新行创建一个删除按钮,但我想删除与删除按钮具有相同ID的行。但是对于某些(可能是显而易见的)原因,它不起作用。

2 个答案:

答案 0 :(得分:1)

删除按钮不在表单中,因此点击时不会提交。

将输入字段放在<form>标记内,然后它们就可以了,

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

答案 1 :(得分: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>