gpt4 book ai didi

php - 单击按钮时如何仅更新 1 行而不是所有行

转载 作者:行者123 更新时间:2023-11-29 09:49:24 25 4
gpt4 key购买 nike

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

数据库图片:

Database

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:

<?php
include_once 'dbconnect.php';

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

最佳答案

为了解决注入(inject)问题,请进行参数化。它会是这样的(我使用 PDO,所以你需要仔细检查):

/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放在隐藏字段中:

# 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 子句进行更新:

<?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();
}

无论如何,我还没有检查这些脚本,但应该非常接近。至少应该为您指明正确的方向。

关于php - 单击按钮时如何仅更新 1 行而不是所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55079210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com