使用单选按钮编辑和删除数据库中的记录

时间:2013-12-29 14:59:07

标签: php html mysql

<?php

$user_name = "root";
$password = "";
$database = "my_db";
$server = "127.0.0.1";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if(isset ($_POST['name']))
{
$name = $_POST['name'];

if(mysql_query("INSERT INTO persons VALUES(' ' , '$name') "))

     echo "Successful Insertion!";

else

     echo "Please try again!";
}

$result = mysql_query("SELECT * FROM persons");     

?>

<html>

<head>

    <style type = "text/css">

    li { list-style-type: none;  display: inline;   padding: 10px;  text-align: center;}

    </style>                    


</head>

    <body>

    <form action = " . "        method = "POST">

    Name:   <input type = "text"        name = "name"><br>
            <input type = "submit"  value = "Enter">

    </form>             

    <form name = "delete_form"   method = "POST"   action = "delete.php" >

        <input type = "submit"  name = "deleteRecord"   value = "Delete Record" />

        </form>

    <h1>List of Names</h1>

    <table border = "1"   width = "100%"   cellpadding = "5"   cellspacing = "2">

    <tr>
    <td><strong></strong></td>
    <td><strong>ID</strong></td>
    <td><strong>Company</strong></td>
    <td><strong>Edit</strong></td>
    <td><strong>Delete</strong></td>
    </tr>

    <?php while ($row = mysql_fetch_array($result)) { ?>

    <tr>
    <td><input type="radio"   Name="id"   value="<?php echo $row['id']; ?>" ></td>
    <td><?php echo $row['id']; ?></td>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo "<a href = 'edit.php?id=$row[id]'>edit</a>" ?></td>
    <td><?php echo "<a href = 'delete.php?id=$row[id]'>delete</a>" ?></td>
    </tr>
    <?php } ?>


    <form name = "edit_form"   method = "POST"   action = " edit.php?edit=  "<?php echo $row['id'] ?> >

    <input type = "submit"  name = "editRecord" value = "Edit Record" />

    </form> 


                </table>                

    <?php

            while($row = mysql_fetch_array($result))
        echo "<li>$row[id]</li> . <li>$row[name]</li> <li> <a href = 'edit.php?edit=$row[id]'>edit</a> </li> <li> <a href = 'delete.php?del=$row[id]'>delete</a></li> <br>";
        ?>

    </body>

     </html>

    edit.php



  <?php

   $user_name = "root";
   $password = "";
   $database = "my_db";
   $server = "127.0.0.1";

    $db_handle = mysql_connect($server, $user_name, $password);
     $db_found = mysql_select_db($database, $db_handle);

     $row = " ";

       if (isset($_POST['id']))
      {
     // if there is an id sent through POST and it isn't null/empty, use that
     $id = $_POST['id'];

     $SQL = "SELECT * FROM persons WHERE id = '$id' ";

     $result = mysql_query($SQL);

     $row = mysql_fetch_array($result);
  }
  else
  {
    // otherwise use id sent through GET links
    $id = $_GET['id'];

   $SQL = "SELECT * FROM persons WHERE id = '$id' ";

   $result = mysql_query($SQL);

   $row = mysql_fetch_array($result);
  }               

  if(isset($_POST['newName']))
   {
       $id = $_POST['id'];
       $newName = $_POST['newName'];
       $SQL = "UPDATE persons SET name = '$newName' WHERE id = '$id' ";
       $result = mysql_query($SQL) or die("Could not update database" . mysql_error());
          echo "<meta http-equiv = 'refresh' content = '0 ; url = index.php'>";
   }
 ?>


<form action = " edit.php"  method = "POST">

ID: <input type = "text"    name = "id" value = "<?php echo $row[0] ?>"<br><br>

Name:   <input type = "text"    name = "newName"   value = "<?php echo $row[1] ?>"<br><br>
       <input type = "submit"  value = "Update">

</form>

您好,

上面的代码显示了如何编辑和删除数据库中的记录。最初,编辑和删除选项的形式是指向执行所需操作的php脚本的链接。所选行的ID号传递给编辑或删除php文件,然后执行用户选择的操作(请参阅上面代码中的注释)我现在正在尝试修改此代码以便我可以使用收音机按钮选择一个记录,然后使用单选按钮编辑或删除记录。我知道这听起来微不足道,但我遇到了一些困难。任何帮助将不胜感激。谢谢。

你好汤姆。我已经做了你建议的更改,但我仍然提出同样的问题。我已经包含了edit.php文件,以防你想看看。

1 个答案:

答案 0 :(得分:0)

单选按钮的值需要包含要编辑的记录的ID。

<td><INPUT TYPE="Radio" Name="radio" value="<?php echo $row['id']; ?>"></td>

然后,当您提交表单时,您将知道您正在编辑的记录的ID为$_POST['radio']

虽然您已经使用GET方法传递ID(通过编辑和删除链接)。我建议保持一致性,并使用参数id传递所有ID。所以

使用此

    <td><?php echo "<a href = 'edit.php?id=$row[id]'>edit</a>"; ?></td>
    <td><?php echo "<a href = 'delete.php?id=$row[id]'>delete</a>"; ?></td>

这个

<td><input type="radio" name="id" value="<?php echo $row[id]; ?>"></td>

然后在 edit.php delete.php 中,检查ID是否通过POST(如果有人提交了表单)或通过GET(他们点击了一个链接),然后使用任何一个值。

<?php
    if (!empty($_POST['id']))
    {
        // if there is an id sent through POST and it isn't null/empty, use that
        $id = $_POST['id'];
    }
    else
    {
        // otherwise use id sent through GET
        $id = $_GET['id'];
    }

我还应该提到mysql_fetch_array已被弃用,您应该使用PDO或MySQLi。在此处阅读更多内容:http://www.php.net/mysql_fetch_array