单击按钮时如何仅更新1行而不是所有行 [英] How to only update 1 row instead of all the rows when clicking a button

查看:66
本文介绍了单击按钮时如何仅更新1行而不是所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多行的表.每行后面都有一个按钮,您可以单击以将数据库中的一列(baatinn)从0更新为1.但是,当您单击该按钮时,它将把所有行从0更新为1,而不是您要单击的按钮上的行. .我该怎么做,以便它只会更新您单击的行

I have a table containing multiple rows. Behind every row is a button you can click to update a column (baatinn) in the database from 0 to 1. However when you click the button it will update all the rows from 0 to 1 instead of the row you are clicking the button on. How do i make it so it will only update the row you are clicking on

数据库图片:

HTML:

<tr>
    <th>Båt ut</th>
    <th>Båt inn</th>
    <th>Båtnr</th>
    <th>Fornavn</th>
    <th>Etternavn</th>
    <th>Tid</th>
    <th>Kr</th>
    <th>Edit</th>
  </tr>

PHP:

$sql = "SELECT utleid, inntid, baatnr, fornavn, etternavn, tid, kr, baatinn     FROM utleie WHERE baatnr LIKE '%$sok%' or fornavn LIKE '%$sok%' or etternavn     LIKE '%$sok%' or tid LIKE '%$sok%' ORDER BY id desc";
$result = $conn-> query($sql);

if ($result-> num_rows > 0) {
    while ($row = $result-> fetch_assoc()) {
        ?>
        <tr>
            <td><?php echo $row["utleid"]; ?></td>
            <td><?php echo $row["inntid"]; ?></td>
            <td><?php echo $row["baatnr"]; ?></td>
            <td><?php echo $row["fornavn"]; ?></td>
            <td><?php echo $row["etternavn"]; ?></td>
            <td><?php echo $row["tid"]; ?></td>
            <td><?php echo $row["kr"]; ?></td>
            <td><form method="post" action="innlevering.php">
        <button name="edit" value="1">Edit</button>
</form></td>
        </tr>
        <?php 
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn-> close();

innlevering.php:

innlevering.php:

<?php
include_once 'dbconnect.php';

if ($_POST['edit']) {
   $conn->query("UPDATE utleie SET baatinn=1");
}
?>

推荐答案

要解决注射问题,请进行参数设置.可能是这样(我使用PDO,所以您需要仔细检查):

To help your injection problem, parameterize. It would be something like this (I use PDO, so you will want to double check):

/functions/getUtleie.php

function getUtleie($so, $conn)
{
    $query = $conn->prepare("SELECT utleid, inntid, baatnr, fornavn, etternavn, tid, kr, baatinn FROM utleie WHERE baatnr LIKE ? or fornavn LIKE ? or etternavn LIKE ? or tid LIKE ? ORDER BY id desc");
    $so = "%{$so}%";
    $query->bind_param('ssss',$so, $so, $so, $so);
    $result = $query->execute();
    if($result->num_rows == 0)
        return [];

    while($row = $result->fetch_assoc()) {
        $data[] = $row;
    }

    return $data;
}

现在,当您要使用它时,请包含该函数,然后表单上的关键就是在一个隐藏字段中创建id:

Now, when you go to use it, include the function, then the key on the form is to make the id in a hidden field:

# Fetch the data
$result = getUtleie($so, $conn);
# If there are any results
if(!empty($result)): ?>
    <table>
    <?php foreach($result as $row): ?>

        <tr>
            <td><?php echo $row["utleid"] ?></td>
            <td><?php echo $row["inntid"] ?></td>
            <td><?php echo $row["baatnr"] ?></td>
            <td><?php echo $row["fornavn"] ?></td>
            <td><?php echo $row["etternavn"] ?></td>
            <td><?php echo $row["tid"]; ?></td>
            <td><?php echo $row["kr"]; ?></td>
            <td>
                <form method="post" action="innlevering.php">
                    <input type="hidden" name="action" value="update_utleie" />
                    <input type="hidden" name="utleid" value="<?php echo $row["utleid"] ?>" />
                    <input type="text" name="val" />
                    <input type="submit" value="Edit" />
                </form>
            </td>
        </tr>

        <?php endforeach ?>

    </table>

<?php else: ?>

0 results

<?php endif ?>

提交表单后,您将要使用WHERE子句进行更新:

After you submit the form, you will want to update using a WHERE clause:

<?php
include_once 'dbconnect.php';
# Check to make sure the form was submitted
if(!empty($_POST['action'] && $_POST['action'] == 'update_utleie') {
    # Trim these. You should also check they aren't empty (especially the id)
    $id    = trim($_POST['utleid']);
    $value = trim($_POST['val']);
    $query = $conn->prepare("UPDATE `utleie` SET `baatinn` = ? WHERE `utleid` = ?");
    $query->bind_param('si', $value, $id);
    $query->execute();
}

无论如何,我没有检查过这些脚本,但是应该非常接近.至少应该将您指向正确的方向.

Anyway, I haven't checked these scripts but it should be pretty close. Should at least point you in the right direction.

这篇关于单击按钮时如何仅更新1行而不是所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆