简体   繁体   中英

Updating mysql using php in html form

I've created a form that is supposed to update table contents in input boxes, when the content of the input boxes are changed and submitted the database is supposed to update. This is my first page:

<body>
 <form action="qa1.php" 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>Select</th>
                <th>Edit</th>
                <th>Disease ID</th>
                <th>Disease</th>
                <th>Sub Disease</th>
                <th>Associated Disease</th>
                <th>Ethinicity</th>
                <th>Source</th>
                </tr>";
                while($row = mysqli_fetch_assoc($result)) {
                    echo "<tr>";
echo "<td><input type='radio' name='select' value=".$row['Disease_id']."/>&nbsp;</td>";

echo "<td>".$row{'Disease_id'}."</td>";
echo "<td><a href='edit.php?Disease_id=".$row['Disease_id']."'>edit</a></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 "</tr>";}

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

  ?>

 <div>


    <table border="0" align="center" style="border-spacing: 40px 30px;">
        <caption><strong>QualityAnalysis:</br></br></strong></caption></br></br>

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



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


        </center></div>  </TABLE>
        </body>
</html>

This is my editing page: edit.php

    <body>
     <form action="edit.php" 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>


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

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

    if(isset($_GET['Disease_id']))
{
    if(isset($_POST['Update']))
    {
$id=$_GET['Disease_id'];
$name=$_POST['Ethinicity'];
$query3=mysqli_query("update disease set Ethinicity='$name', where Disease_id='$id'");
if($query3)
{
header('location:qa1.php');
}
}
$query1=mysqli_query("select * from disease where Disease_id='$id'");
$query2=mysqli_fetch_array($query1);

?>

    <table border="2" align="center" style="border-spacing: 40px 30px;">
        <caption><strong>Edit</br></br></strong></caption></br></br>
        <tr>   

            <td>Ethinicity<input type="text" list="Ethinicity"        name="Ethinicity" value="<?php echo $row['Ethinicity'];?>"/>
            <input type="hidden" name="Disease_id" value="<?php echo   $row['Disease_id']; ?>"/>        

        <input type="submit" name="Update" value ="Update">
<?php
}
?>      

        </center></div></div>
</form>

But Data is not getting updated in the database. Can anyone help me with this?

Firstly, mysqli_query requires the connection parameter.

Secondly, there's a syntax error with your UPDATE query, there isn't a need for a comma.

So, it should be:

$query3=mysqli_query($conn, "update disease set Ethinicity='$name' where Disease_id='$id'");

$query1=mysqli_query($conn, "select * from disease where Disease_id='$id'");

Also, if($query3) won't check if the row is successfully inserted, use mysqli_affected_rows instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM