使用php将信息更新到mysql

时间:2016-05-23 06:15:43

标签: php mysql mysqli sql-update

我正在尝试通过获取用户选择的ID来更新我的数据库。

<form action="edit1.php" method="post">
<?php
                $query = "SELECT * FROM disease1;";
                $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>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><input type='radio' name='id' value='".$row[id]."'></td>";
    echo "</tr>";}              
    echo "</table>";                         
      ?>     <div> 
    <input type = 'submit' value = 'Update' name = 'submitupdate'>

我的编辑页面edit1.php

<?php
    $conn = mysqli_connect('localhost','root','','tool')  
    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);
    echo $count;
    ?>
    <form action="update.php" method="post">
    <input type="hidden" value="<?php echo $row['id'];?>" name="id"/>
    Diease<input type="text" name="Disease" value="<?php echo $row['Disease'];?    >"/>
    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'];?>"/>
    <input type="submit" value="update">
    </form>

我的update.php

  <?php
    $conn = mysqli_connect('localhost','root','','tool');
     if (!$conn) {
    die("Connection failed: " . mysqli_error());
    } 
    $disease=$_POST['Disease'];
    $SubDisease=$_POST['SubDisease'];
    $Associated_Disease= $_POST['Associated_Disease'];
    $id = $_POST ['id'];
    $update="Update disease1 set Disease='".$disease."', SubDisease='".$SubDisease."', Associated_Disease='".$Associated_Disease."' where     id=".$_POST["id"];
    mysqli_query($conn,$update);
?>

但是id没有读取,并且id的值没有传递给update命令。任何人都可以帮助我

1 个答案:

答案 0 :(得分:2)

您需要从查询结果中fetch data

$row = mysqli_fetch_assoc($result);

然后分配给表单。

完整的代码将是

$query = "SELECT * FROM disease where id=".$_POST["id"];
$result = mysqli_query($conn, $query);
$count= mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);

<form action="update.php" method="post">
    <input type="hidden" value="<?php echo $row['id'];?>" name="id"/>
    Diease<input type="text" name="Disease" value="<?php echo $row['Disease'];?    >"/>
    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'];?>"/>
    <input type="submit" value="update">
    </form>