douchuitang0642 2019-07-06 03:29
浏览 142

如何在表中创建一个按钮执行mysql查询

I have a simple html table that displays data from a mysql table containing a list of available tournaments.

<table>
    <tr>
        <th>Tournament Name</th>
        <th>Platform</th>
        <th>Date</th>
        <th>Venue</th>
        <th>Judge</th>
        <th>Description</th>
        <th>Limit</th>
        <th>Register</th>
    </tr>
    <?php while($row = mysqli_fetch_array($result)):?>
    <tr>
        <td><?php echo $row['nTournament'];?></td>
        <td><?php echo $row['pTournament'];?></td>
        <td><?php echo $row['dTournament'];?></td>
        <td><?php echo $row['vTournament'];?></td>
        <td><?php echo $row['jTournament'];?></td>
        <td><?php echo $row['dTournament'];?></td>
        <td><?php echo $row['lTournament']; ?></td>
        <td><form method="post" action="tournamentlist.php"><button type="submit" name="btn_register"></button></form></td>
    </tr>
    <?php endwhile; ?>
</table>

What I want to do is that once the current user clicks that button, the button executes a query which takes the current user's id and the tournament id of the selected row to add them to another mysql table. Something like a tournament inscription. The query/function I want to run is:

if (isset($_POST['btn_register'])) {
    singup();
}
function singup() {
    global $db;
    $idUser = $_POST['idUser'];
    $idTournament= $_POST['idTournament'];
    $query = "INSERT INTO registeredCompetitor(idUser, idTournament) VALUES ('$idUser', '$idTournament')";
    mysqli_query($db, $query);
    header('location: tournamentlist.php');
}

The query works just fine on it's own but I don't know if the function itself works because of the button. Is it possible to do such thing without the use of a form? If not, What other ways are there to do something like this?

EDIT1: Button now is ready but nor the function or the query executes once it's clicked.

  • 写回答

3条回答 默认 最新

  • dongliao3742 2019-07-06 03:53
    关注

    You can create a form on each row that has 2 hidden fields in it. Not sure what your DB fields are called though so you'll have to change user_id and tournament_id to something appropriate.

    ...
    <td>
        <form action="post" action="the_name_of_your_php_script.php">
            <input type="hidden" name="idUser" value="<?php echo $row['user_id']; ?>">
            <input type="hidden" name="idTournament" value="<?php echo $row['tournament_id']; ?>">
            <button type="submit" name="btn_register"></button>
        </form>
    </td>
    ...
    

    Can't wrap it because <form> is not allowed as a child of <td>

    All the documentation I see online permits forms inside of <td>. The other alternative is to use a link instead of a form.

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记