doushi8599 2017-04-07 18:06
浏览 66
已采纳

从php更新sql表中的字段

I have a table with more columns. On one column I have 3 buttons for each row with different queries associated. When I click one of the buttons, field in the table from my database should update.The problem is that when I click on a button on any row in the table, it updates only the field from the first row.For example, I have a table with 10 rows.If I click one of the buttons on the 10th row IT will update my first row, not the 10th one as I want. Is there any possibility to solve this? This is the code..I'm sorry it is too long:

    <?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条回答 默认 最新

  • dscw1223 2017-04-07 18:22
    关注

    for updated the da tbale row when you submit the values related to a html table row you should add and hidden input with the value that let you reach the correct row eg:

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

    and in your sql updated query add the POST value related to the row

      $sql = "UPDATE masculin SET Premiu=1 WHERE CNP='".$_POST['CNP']. "'";   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入