无法在mysql中更新表循环

时间:2018-05-22 12:13:09

标签: php

我有循环表包含文本区域,当循环提交最后一行相对于循环时,循环结果有7行,只有我可以更新它

            $id = $_SESSION['id'];
            $sql = "select * from sp where team_id ='$id' "; 
            $result = $connt->query($sql);

            if($result->num_rows > 0 ){
            ?>
            <?php
            while ($row = $result->fetch_assoc()){ 
        ?>
<form action="update.php" method="post">
        <tr>
    <td> <?php echo $row['name']; ?> </td>
    <td><input type="hidden" value="<?php echo $row['id'];?> "> <?php echo $row['id']; ?> </td>
    <td> <textarea name="sun"> <?php echo $row['sunday']; ?> </textarea></td> 
    <td> <textarea name="mon"> <?php echo $row['monday']; ?> </textarea></td> 
    <td> <textarea name="tues" > <?php echo $row['tuesday']; ?> </textarea></td> 
    <td> <textarea name="wednes" > <?php echo $row['wednesday']; ?></textarea></td> 
</tr>
    <?php
    }
    }
?>
<input type="submit" name="submit" >
</form>

file update.php

  include'../../../config/connect.php';

     $sun = $_POST['sun'];
$mon = $_POST['mon'];
$tues = $_POST['tues'];
$wednes = $_POST['wednes'];
$thurs = $_POST['thurs'];
$fri = $_POST['fri'];
$satur = $_POST['satur'];

$id = $_POST['id'];

if($_POST['submit']){
$sql = "UPDATE sp SET sunday='$sun', monday='$mon', tuesday='$tues', 
wednesday='$wednes', thursday='$thurs', friday='$fri', saturday='$satur' 
WHERE id='$id' ";
};

if ($connt->query($sql) === true){
?>


<?php
}else{


echo 'notdone';

}

循环中的最后一行仅受影响,因为最后一行属性会覆盖其他行

2 个答案:

答案 0 :(得分:2)

您没有正确关闭表单,您应该将ID保存在隐藏字段中,以便在更新查询中使用它

 while ($row = $result->fetch_assoc()){ 
        ?>
<form action="update.php" method="post">
        <tr>
    <td> <?php echo $row['name']; ?> </td>
    <td> <?php echo $row['id']; ?> <input type="hidden" name="id" value="<?php echo $row['id']; ?> "></td>
    <td> <textarea name="sun"> <?php echo $row['sunday']; ?> </textarea></td> 
    <td> <textarea name="mon"> <?php echo $row['monday']; ?> </textarea></td> 
    <td> <textarea name="tues" > <?php echo $row['tuesday']; ?> </textarea></td> 
    <td> <textarea name="wednes" > <?php echo $row['wednesday']; ?></textarea></td> 
</tr>
<input type="submit" name="submit" >
</form>
    <?php
    }
    }
?>

答案 1 :(得分:0)

请在此处找到我的表单设计。请在while循环后使用并提交操作,否则它将针对所有实际错误的行运行。

您的代码中没有更新查询。请写下代码,然后让我回答。

<form action="update.php" method="post">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<?php 
 $id = $_SESSION['id'];
 $sql = "select * from sp where team_id ='$id' "; 
 $result = $connt->query($sql);

  if($result->num_rows > 0 ){
 ?>
 <?php
  while ($row = $result->fetch_assoc()){ 
 ?>
        <tr>
    <td> <?php echo $row['name']; ?> </td>
    <td> <?php echo $row['id']; ?> <input type="hidden" name="id" value="<?php echo $row['id']; ?> "></td>
    <td> <textarea name="sun"> <?php echo $row['sunday']; ?> </textarea></td> 
    <td> <textarea name="mon"> <?php echo $row['monday']; ?> </textarea></td> 
    <td> <textarea name="tues" > <?php echo $row['tuesday']; ?> </textarea></td> 
    <td> <textarea name="wednes" > <?php echo $row['wednesday']; ?></textarea></td> 
</tr>

    <?php
    }
    }
?>
<tr><td><input type="submit" name="submit" ></td></tr>
</table>
</form>