douwu5009 2016-11-08 05:11
浏览 104
已采纳

如何在我的php行中添加删除按钮

I am new to php coding.

I am adding each row delete button but it should not working please help me.

This is my html code:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
    $connection = mysql_connect('localhost', 'root','');
if (!$connection)
{
    die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "emp",$connection);
if (!$select_db)
{
    die("Database Selection Failed" . mysql_error());
}
    $sql = "SELECT * FROM venu ";     
    $result = mysql_query($sql) or die(mysql_error());
    ?>
    <table border="2" style= " margin: 0 auto;" id="myTable">
      <thead>
        <tr>
          <th>name</th>
          <th>id</th>
          <th>rollnumber</th>
          <th>address</th>
          <th>phonenumber</th>
        </tr>
      </thead>
      <tbody>
      <?php
            while($row = mysql_fetch_array($result))
            {
              echo "<tr>";
                echo "<td>" . $row['name'] . "</td>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['rollnumber'] . "</td>";
                echo "<td>" . $row['address'] . "</td>";
                echo "<td>" . $row['phonenumber'] . "</td>";
               echo "<td><form action='delete.php' method='POST'><input type='hidden'  value='".$row["address"]."'/><input type='submit' name='submit-btn' value='delete' /></form></td></tr>";
                echo "</tr>";


            }
            ?>
            </tbody>
            </table>
    </body>
    </html>

This is my delete php code:

<?php
$connection = mysql_connect('localhost', 'root','');
if (!$connection)
{
    die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "emp",$connection);
if (!$select_db)
{
    die("Database Selection Failed" . mysql_error());
}
    error_reporting(0);
    session_start();
    $name = $_POST['name'];
    $id = $_POST['id'];
    $rollnumber = $_POST['rollnumber'];
    $address = $_POST['address'];
    $phonenumber = $_POST['phonenumber'];
    if($name!='' and $id!='')
    {
        $sql = mysql_query("DELETE FROM 'venu' WHERE name='balaji'AND id='93'AND rollnumber='93'AND address='bangalore'AND phonenumber='1234567890'");
    echo "<br/><br/><span>deleted successfully...!!</span>";
}
else{
echo "<p>ERROR</p>";
}
mysql_close($connection); 
?>

I am trying to delete each row using button but should not working please help me.

  • 写回答

3条回答 默认 最新

  • douhuiqi3855 2016-11-08 05:24
    关注

    In your html view page some change echo "<td><a href='delete.php?did=".$row['id']."'>Delete</a></td>"; like bellow:

    <?php
    while($row = mysql_fetch_array($result))
    {
        echo "<tr>";
            echo "<td>" . $row['name'] . "</td>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . $row['rollnumber'] . "</td>";
            echo "<td>" . $row['address'] . "</td>";
            echo "<td>" . $row['phonenumber'] . "</td>";
            echo "<td><a href='delete.php?did=".$row['id']."'>Delete</a></td>";
        echo "</tr>";
    }
    ?>
    

    PHP delete code :

    <?php
    if(isset($_GET['did'])) {
        $delete_id = mysql_real_escape_string($_GET['did']);
        $sql = mysql_query("DELETE FROM venu WHERE id = '".$delete_id."'");
        if($sql) {
            echo "<br/><br/><span>deleted successfully...!!</span>";
        } else {
            echo "ERROR";
        }
    }
    ?>
    

    Note : Please avoid mysql_* because mysql_* has beed removed from PHP 7. Please use mysqli or PDO.

    More details about of PDO connection http://php.net/manual/en/pdo.connections.php

    And more details about of mysqli http://php.net/manual/en/mysqli.query.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私