以相同的html格式更新和删除

时间:2016-05-23 11:37:48

标签: php mysql sql-update submit

我使用以下代码在同一表单中添加了更新和删除按钮。删除工作完美。但更新再次没有取“id”的价值。

        <?php
$servername = "localhost";
$username = "root";
$password = "";
$dp = "tool";

$dp= new mysqli($servername, $username, $password, $dp) or die("Unable to connect");
//echo"Great work";
?>
<!DOCTYPE html>
<html>
<head>

     <title>registration</title>
  <meta charset="UTF-8">
  <link href="site.css" rel="stylesheet">
<div align="center">

    <link rel="stylesheet" href="mine.css"/>

     <table border="0" align="center" style="border-spacing: 40px 20px;">

          <align="center">  <td>
    </head>  

 <body bgcolor=" #b3ffe0">

<style>

html {
    font-family: "Lucida Sans", sans-serif;
}
ul li {display: block;position: relative;float: left;border:1px }   
 ul li a {display: block;text-decoration: none; white-space: nowrap;color: #fff;}

   ul {
    list-style-type: none;
    padding: 2px ;
    margin-left: auto;
    background-color: #666;
}

li a, .dropbtn {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 10px 20px;
    text-decoration: none;

}
li a:hover, .dropdown:hover .dropbtn {
    background-color: #111;
}

</style>
</head>
<body>
 <form method="post">

<ul>
  <li><a class="active" href="df1.php">Disease</a></li>
  <li><a href="drug.php" >Drug</a></li>
  <li><a href="#about">Interaction</a></li>
    <a href="#" class="dropbtn">Alternate Drug</a>
     </ul>
  <?php
                $query = "SELECT * FROM disease;";
                $result = mysqli_query($dp, $query);
                echo "<table border=5>
                <tr>
                <th>Disease ID</th>
                <th>Disease</th>
                <th>Sub Disease</th>
                <th>Associated Disease</th>
                <th>Ethinicity</th>
                <th>Source</th>
                <th>Edit</th>
                </tr>";
                while($row = mysqli_fetch_assoc($result)) {
                    echo "<tr>";
echo "<td>".$row{'id'}."</td>";
echo "<td>".$row{'Disease'}."</td>";
echo "<td>".$row{'SubDisease'}."</td>";
echo "<td>".$row{'Associated_Disease'}."</td>";
echo "<td>".$row{'Ethinicity'}."</td>";
echo "<td>".$row{'Source'}."</td>";
echo "<td><input type='radio' name='id' value='".$row[id]."'></td>";
echo "</tr>";}

echo "</table>"; 
    //  $selectedRow=$_POST['id'];  

  ?>

 <div>
    <table border="0" align="center" style="border-spacing: 40px 30px;">

    <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="4" WIDTH="40%">



</br><center>
          <button style="color: red"><a href="adddf1.php" target="_self" name="Add" value="Add">Add</a></button>
          <input type = 'submit' value = 'Update' name = 'submitupdate'>
           <input type = 'submit' value = 'Delete' name = 'submitdelete'>


        </center></TABLE>

    <?php
    if(isset($_POST[submitupdate]))
    {
        header ("Location: http://localhost/card/edit3.php");
    }
    if ($_POST[submitdelete])
{
    $conn = mysqli_connect('localhost','root','','tool');

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_error());
} 

// 
     $sql="DELETE FROM disease WHERE id=".$_POST['id'];
     echo "Data deleted successfully";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
?>

</body>
</html>

Edit3.php

<?php

$conn = mysqli_connect('localhost','root','','tool');

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_error());
} 
$query = "SELECT * FROM disease where id=".$_POST['id'];
$result = mysqli_query($conn, $query);
$count= mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
echo $count;
?>
<form action="update.php" method="post">
<input type="hidden" value="<?php echo $row['id'];?>" name="id"/>
Disease (ICD10) <select id= "Disease" name="Disease">
             <option value="Certain infectious and parasitic diseases">Certain infectious and parasitic diseases</option>
                    <option value="Neoplasms">Neoplasms</option>
                    <option value="Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism ">Diseases of the blood and blood-forming organs and certain disorders involving the immune mechanism</option>
SubDisease<input type="text" name="SubDisease" value="<?php echo $row['SubDisease'];?>"/>
Associated Disease<input type="text" name="Associated_Disease" value="<?php echo $row['Associated_Disease'];?>"/>
<td>Ethinicity<input type="text" list="Ethinicity" id="color" name="Ethinicity"  value="<?php echo $row['Ethinicity'];?>" style="width:100px;">
<datalist id="Ethinicity">
<option value="Indian">
<option value="American">
<option value="Srilankan">
</datalist>
         </td>
Source<input type="text" name="Source" value="<?php echo $row['Source'];?>"/>
<input type="submit" value="update">
</form>

update.php

<?php
$conn = mysqli_connect('localhost','root','','tool');

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_error());
} 
$disease = $_POST['Disease'];
$SubDisease = $_POST['SubDisease'];
$Associated_Disease = $_POST['Associated_Disease'];
$Ethinicity = $_POST['Ethinicity'];
$Source = $_POST ['Source'];
$id = $_POST ['id'];

$update= "Update disease set Disease='".$disease."', SubDisease='".$SubDisease."', Associated_Disease='".$Associated_Disease."', Ethinicity='".$Ethinicity."', Source='".$Source."' where id=".$_POST["id"];

if(!mysqli_query($conn,$update))
    echo mysqli_error;
?>

下载疾病,也没有读取数据库值而没有在编辑页面中显示。

0 个答案:

没有答案