从php更新sql表中的字段

时间:2017-04-07 18:06:47

标签: php mysql

我有一个包含更多列的表。在一列中,每行有3个按钮,并且关联的查询不同。当我单击其中一个按钮时,我的数据库中的表中的字段应该更新。问题是,当我单击表中任何一行上的按钮时,它只更新第一行中的字段。例如,我有一个包含10行的表。如果我点击第10行的其中一个按钮,IT将更新我的第一行,而不是我想要的第10行。有没有可能解决这个问题? 这是代码。对不起它太长了:

    <?php
$query = "SELECT * FROM masculin ORDER BY id_concurent";
            $result = mysqli_query($link ,$query);
    $row = mysqli_fetch_assoc($result); 

    if(isset($_POST['locul1']))
    {
        $sql = "UPDATE masculin SET Premiu=1 WHERE CNP='".$row['CNP']."'";      
        mysqli_query($link,$sql);
        header("Location:administrare.php");
    }
    else if(isset($_POST['locul2']))
    {
        $sql = "UPDATE masculin SET Premiu=2 WHERE CNP='".$row['CNP']."'";

         mysqli_query($link,$sql);
        header("Location:administrare.php");
    }
    else  if(isset($_POST['locul3']))
    {
        $sql = "UPDATE masculin SET Premiu=3 WHERE CNP='".$row['CNP']."'";      
        mysqli_query($link,$sql);
        header("Location:administrare.php");
    }   ?>  

<u><i><h1 align="center">Administrare concurenti</h1></i></u>
<u><i><h2>MASCULIN</h2></i></u>

<?php
            $query = "SELECT * FROM masculin ORDER BY id_concurent";
            $result = mysqli_query($link ,$query);
            if (mysqli_num_rows($result) == 0) {
                    echo 'Inca nu s-a inscris niciun concurent.';
            } else {
        ?>
<table width="100%">
    <tr>
        <th>Nr.<br />concurs</th>
        <th>Nume</th>
        <th>Prenume</th>
        <th>CNP</th>
        <th>Categoria</th>
        <th>Varsta</th>
        <th>Premiu</th>
        <th>Modifica<br />rezultat</th>
        <th>Descalifica</th>
    </tr>   
      <?php while($row = mysqli_fetch_assoc($result)){ ?>
        <tr>
            <?php 
            $query1="SELECT id_concurent  FROM concurenti WHERE CNP='".$row['CNP']."'";
                $result1=mysqli_query($link,$query1);
                $nr_conc=mysqli_fetch_assoc($result1);
            ?>
            <td> <?php echo $nr_conc['id_concurent'] ?> </td>
            <td> <?php echo $row['Nume'] ?> </td>
            <td> <?php echo $row['Prenume'] ?> </td>
            <td> <?php echo $row['CNP'] ?> </td>
            <td> <?php echo $row['Categorie'] ?> </td>
            <td> <?php echo $row['Varsta'] ?> </td>
            <td> <?php echo $row['Premiu'] ?> </td>         
            <td>
            <form action="administrare.php" method="post">          
                <input type="submit" name="locul1" value="Premiul 1"> 
                <input type="submit" name="locul2" value="Premiul 2"> 
                <input type="submit" name="locul3" value="Premiul 3">       
            </form>         
            </td>
        </tr>
            <?php } ?>
    </table>     <?php  } ?>

1 个答案:

答案 0 :(得分:0)

用于更新da tbale行,当您提交与html表格行相关的值时,您应该添加和隐藏输入,其值可以让您到达正确的行,例如:

  <form action="administrare.php" method="post">          
      <input type="submit" name="locul1" value="Premiul 1"> 
      <input type="submit" name="locul2" value="Premiul 2"> 
      <input type="submit" name="locul3" value="Premiul 3">       
      <input type="hidden" name="CNP" value="<?php echo $row['CNP'] ?>">
  </form>

并在您的sql更新查询中添加与行

相关的POST值
  $sql = "UPDATE masculin SET Premiu=1 WHERE CNP='".$_POST['CNP']. "'";