Christoph C.
Christoph C.

Reputation: 938

Data from MySQL Database - Edit entry

I hope that someone can help me with a little issue. I have no idea how to solve that. For someone who works with MySQL and PHP it should be easy to explain I guess. Anyway here is what I need:

I created an admin interface with PHP. Inside the admin interface I have a table with entries from my database. So inside the table I can see the ID, username, first name, last name and so on from every single user.

enter image description here

Now on the right side you can see the text "Bearbeiten" which means "Edit" in English. If I click on edit I am redirected to my edit page. Problem ist, that the edit.php ist the same for every user. What I want now is, that if I click on "Bearbeiten" that I will be forwarded to the edit.php BUT that I am able to edit the specific user there.

For example if I click on "Bearbeiten" in the row where the user is with User ID 1 than I want to be redirected to edit.php?id=1 so that I am able to edit only the selected profile.

Can someone help me? What do I need to do inside my code? I would really appreciate your help!

EDIT:

Here is my table code at the user overview site:

<table class="table table-striped responsive-utilities jambo_table bulk_action">
                                        <thead>
                                            <tr class="headings">

                                                <th class="column-title">ID </th>
                                                <th class="column-title">Schule</th>
                                                <th class="column-title">Vorname </th>
                                                <th class="column-title">Nachname </th>
                                                <th class="column-title">Registriert am </th>
                                                <th class="column-title">Aktiv </th>
                                                <th class="column-title no-link last"><span class="nobr">Profil</span>
                                                </th>
                                                <th class="bulk-actions" colspan="7">
                                                    <a class="antoo" style="color:#fff; font-weight:500;">Mehrfachauswahl ( <span class="action-cnt"> </span> ) <i class="fa fa-chevron-down"></i></a>
                                            </th>
                                </tr>
                            </thead>
                            <?php while ($row = $erg->fetch_assoc()) { ?> 
                            <tbody>
                                <tr class="even pointer">
                                    <td class=" "><?php echo $row['id']; ?></td>
                                    <td class=" "><?php echo $row['schule']; ?></td>
                                    <td class=" "><?php echo $row['firstname'];?></td>
                                    <td class=" "><?php echo $row['lastname']; ?></td>
                                    <td class=" "><?php echo $row['registration']; ?></td>
                                    <td class="a-right a-right "><?php echo $row['active']; ?></td>
                                    <td class=" last"><a href="edit_profil_teacher.php?id=<?php echo $id; ?>">Bearbeiten</a>
                                                    </td>
                                            </tr>           
                            <?php } ?>
              
                             </tbody>
                            <?php $erg->close(); ?> 
                                    </table>

Here is what I entered at the top of my edit page:

<?php
$user_id = (int)$_GET['id'];
?>

Problem right now is that URL at user overview page changed to edit.php?id= but that´s it. It does not select the correct ID. What am I missing?

Thanks, Chris

Upvotes: 0

Views: 127

Answers (3)

Jon Lauters
Jon Lauters

Reputation: 56

Assuming in the listing page you are looping around a mysql result to generate the HTML, you need to add the id to the anchor tags href tag.

Code has been changed to use the $row['id'] from provided coded.

On the link for "Bearbeiten" change the:

<a href="edit.php">Bearbeiten</a>

to:

<a href="edit.php?id=<?php echo $row['id']; ?>">Bearbeiten</a>

Change $id to whatever you're calling the PHP variable for the ID column, on your edit.php page you can then pull user details from the database for that user ID.

Upvotes: 1

Mike Wells
Mike Wells

Reputation: 425

<a href="edit.php?id=<?php echo $id; ?>">Bearbeiten</a>

And then in the edit.php page at the top:

$user_id = (int)$_GET['id'];

Hope that helps.

Upvotes: 0

DirtyBit
DirtyBit

Reputation: 16772

Like members have already suggested, put this in a loop something like:

<?php
 while($row=$stmt->fetch(PDO::FETCH_ASSOC))
 { 
    $id = $row['ID'];
    $name = $row['Name Eleternteil'];

?>
<td><?php echo $id; ?></td>
<td><?php echo $name; ?></td>
<td><a href="edit.php?id=<?php echo $id; ?>">Bearbeiten</a></td>
<?php } ?>

This shoulkd take you to the next page with the unique ID and then on the edit.php get the id and do whatever you want by simply using:

<?php
    $edit = $_GET['id'];
?>

Upvotes: 0

Related Questions