php - 使用php以html形式更新mysql

标签 php html mysql

我创建了一个表单,当输入框的内容更改并提交时,应该更新输入框中的表格内容,数据库应该更新。 这是我的第一页:

<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>

这是我的编辑页面: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>

但是数据库中的数据没有更新。谁能帮我解决这个问题吗?

最佳答案

首先,mysqli_query需要连接参数。

其次,您的 UPDATE 查询存在语法错误,不需要逗号。

所以,应该是:

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

$query1=mysqli_query($conn, "select * from disease where Disease_id='$id'");
<小时/>

此外,if($query3) 不会检查该行是否已成功插入,请使用 mysqli_affected_rows相反。

关于php - 使用php以html形式更新mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37359368/

相关文章:

php - 如何测试 mySQL 查询的速度?

php - 在php中执行require_once时如何处理解析错误或编译错误

html - 无法添加背景图像 css

MySQL获取每个用户的图书数量

mysql - 需要创建自定义查询

mysql - id 的含义 = LAST_INSERT_ID(id)

php - 添加多个 SQL 结果

php - Twitch API redirect_mismatch

javascript - 如何避免 Laravel 中服务器发送事件的单一路由

javascript - 无法读取未定义/TypeError : event is undefined 的属性 'target'