Reputation: 53
So i want to add a button at the end of my table where once you click it, it will update "baatinn" in the database from 0 to 1. I have figured out how to add the button etc but when i click it, it will update all the rows and not the row you click the button on from 0 to 1. So all i need is to know how do it only update "baatinn" for the row you click the button for. I have a primary key for ID. and the default value for "baatinn" is 0
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";
Innlevering.php
<?php
include_once 'dbconnect.php';
$id = $_POST['id'];
if ($_POST['edit']) {
$conn->query("UPDATE utleie SET baatinn=1 WHERE id={$id}");
}
?>
Upvotes: 0
Views: 481
Reputation: 473
You can edit you php code generating the table. You have to set the name of the button to id and set the value to the id field of the database table ( so you have to also edit your mysql query as below
PHP
$sql = "SELECT id, 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="id" value="<?php echo $row["id"]; ?>">Edit</button>
</form></td>
</tr>
<?php
}
echo "</table>";
} else {
echo "0 results";
Upvotes: 1
Reputation: 614
Set all your rows in a single form element, then use a button with value = the primary key of the current row.
<button name="id" value="<?php echo $row["primary_key_of_this_row"]; ?>">Edit</button>
Upvotes: 0