如何在我的php行中添加删除按钮 [英] How to add delete button in my php rows

查看:163
本文介绍了如何在我的php行中添加删除按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php编码的新手。

I am new to php coding.

我正在添加每行删除按钮但不应该工作请帮帮我。

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

这是我的html代码:

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>

这是我的删除p​​hp代码:

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.

推荐答案

在您的html视图页面中,一些更改 echo< td>< a href ='delete.php?

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删除代码:

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

注意:请避免 mysql _ * code> mysql _ * 已从
中删除PHP 7.请使用 mysqli PDO

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

关于 PDO 连接的更多细节 http://php.net/manual/en/pdo.connections.php

有关 mysqli http://php.net/manual/en/mysqli.query.php

这篇关于如何在我的php行中添加删除按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆