简体   繁体   English

使用html格式的php更新mysql

[英]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 这是我的编辑页面: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. 首先, mysqli_query需要连接参数。

Secondly, there's a syntax error with your UPDATE query, there isn't a need for a comma. 其次,您的UPDATE查询存在语法错误,不需要逗号。

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. 另外, if($query3)不会检查是否成功插入了该行,请改用mysqli_affected_rows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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