1

I have this HTML table with data from MySQL database and form for adding/editing rows in this table. If I add new row, everything works fine, but when I want to edit, I dont know how to pre-fill input text fields in this form. I have id of specific row saved as id of each editing button, but I dont know how to get know the ID from this clicked button. And should I have to do it with PHP or JS?

<table class="table table-striped">
                    <thead>
                    <tr>
                        <th> Poradi</th>
                        <th> Jmeno</th>
                        <th> Prijmeni</th>
                        <th> Adresa</th>
                        <th> Mesto</th>
                        <th> Telefon</th>
                        <th> Email</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    $db = new PDO(
                        "mysql:host=localhost;dbname=xyz",
                        "xyz",
                        "xy"
                    );

                    $stmp = $db->prepare("SELECT * FROM user");
                    $stmp->execute();
                    $data = array();
                    while ($row = $stmp->fetch()) {
                        $data[]=($row);
                        echo "<tr>
                                <td>" . $row['id'] . "</td>
                                <td>" . $row['login'] . "</td>
                                <td>" . $row['password'] . "</td>
                                <td>" . $row['name'] . "</td>
                                <td>" . $row['surname'] . "</td>
                                <td>" . $row['address'] . "</td>
                                <td>" . $row['city'] . "</td>
                                <td>" . $row['phone'] . "</td>
                                <td>" . $row['email'] . "</td><td>";
                        $id = $row['id'];
                        echo "<a><i class='glyphicon glyphicon-pencil' id=$id></i></a></td>
                        </tr>";
                    }

                    ?>

                    </tbody>
                </table>


            </div>

            <div class="sideMenu">
                <form method="post" class="form-group" action="add.php">
                    <h1>Pridat servis</h1>
                    <input type="text" name="name" placeholder="Jmeno" required autofocus>
                    <input type="text" name="surname" placeholder="Prijmeni" required>
                    <input type="text" name="address" placeholder="Adresa" required>
                    <input type="text" name="city" placeholder="Mesto" required>
                    <input type="tel" name="phone" placeholder="Telefon" required>
                    <input type="email" name="email" placeholder="Email" required>

                    <div class="btn-toolbar">
                        <button class="btn" type="submit">Ulozit</button>
                        <button class="btn" type="button">Zrusit</button>
                    </div>
                </form>

                <div class="icon-close">
                    <img src="http://s3.amazonaws.com/codecademy-content/courses/ltp2/img/uber/close.png">
                </div>
            </div>

3 Answers 3

0

I was on the same page when I have started.

You should use

echo "<a href=\"edit.php?id=".$row['id']."\"><i class='glyphicon glyphicon-pencil' id=$id></i></a>

On edit.php you will use $_GET['id'] to get the id of clicked element.

Sign up to request clarification or add additional context in comments.

1 Comment

Your solution would be helpful, but I dont want to redirect or refresh website, I wanted to do that just dynamically. I've found different solution which works but anyway, thanks a lot for inspiration :)
0

You need to tell you script whichrecord you want to edit.

Best way will be to add href to your edit anchor.

echo "<a href="/url/to/this/script?id=$id"><i class='glyphicon glyphicon-pencil' id=$id></i></a></td>

Now after refresh you can get id of record you want to edit by:

// this way provide some base security
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);

// Now get record from database (only sample call - need to build prepare statement)
$record = $database->getRecordById($id);

// After that fill form with record data
<input type="text" name="name" placeholder="Jmeno" value="<?php $record->name ?>" required autofocus>

Comments

0

I found a solution I was looking for. Advices from guys here are right as well in case it never mind you redirect from your site.

I wanted to do it dynamically with jQuery if it would be possible and I've found this solution which works for me perfect, so I share it here.

jQuery: Get the contents of a table row with a button click

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.