How to only update 1 row instead of all the rows when clicking a button

Philip

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

Picture of database:

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

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

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to update all rows with value from first row

Swift How to update url of video in AVPlayer when clicking on a button?

Selecting table rows with jquery, but also selecting rows when clicking button, how do I prevent this?

How to delete rows from table when clicking button

MySQL query only fetching the first row instead of all the rows in the table

SQL Update only updating 1 row, when 2 in query

Button inside table row tr, when clicking button only do button action

Get the model when clicking a button on a table row

button in fragmentlist only responds once, then "releases" when clicking on the row

only delete relation row when all rows not exist

How to update 1 column only but to all rows with a calculation?

MySQL return a single row only when all rows are the value of the WHERE

How to update rows based only on ROW_NUMBER()?

How to close a popup window, when clicking onto a button in html only

How to update the value of the spinner when clicking a "reset" button

How can I automatically disable other rows using jquery after clicking a button from another row?

Update multiple rows using a case statement only updates 1 row

Update only 1 component instead of all in ReactJS

Why not all of the action button functionality work when using Enter key instead of clicking?

Gridview, Update function affecting all rows instead of single row

How to show modal by clicking on the whole row instead of "Edit" button

Update row value only when all other rows meet the condition

How to apply styles and text to only one button when clicked instead of all buttons in Angular 10

how to get specific row text haviing same class in a table column when clicking on a button in one row

Google Spreadsheet: Hide/Unhide any row that meets x condition in a group of rows when clicking button?

How to pass all user's data when clicking edit button?

Update Text only once after clicking button

How to use innerHTML to format all the rows like row 1 when adding rows?

how to not update all rows and only restrict to the where clause