Search code examples
phpmysqlihref

PHP Passing ID in href


I'm trying to make editable rows in a table. So i need to link to another php file, while passing the id from my database Fetches. I watched some tutorials but it doesnt work for me. Everything works fine so far but the 'a href' wont show me any id when i get to the edit.php page.

  <?php
$user = 'root';
$password = 'root';
$db = 'projekte_db';
$host = 'localhost';
$port = 3306;

$con = mysqli_connect($host, $user, $password, $db);
$query = "Select id,name,ort,strasse,projektleiter from Projekt";
$result = mysqli_query($con,$query);

while($row = mysqli_fetch_array($result)){
    echo "<tr>
    <td>".$row["id"]."</td>
    <td>".$row["name"]."</td>
    <td>".$row["ort"]."</td>
    <td>".$row["strasse"]."</td>
    <td>".$row["projektleiter"]."</td>

    <td><a href='edit.php?id='".$row["id"]."alt='edit'>Bearbeiten</a></td>
    </tr>";
}?>

Solution

  • You're adding the ID outside of the attribute. This:

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

    would produce something like this:

    <a href='edit.php?id='123alt='edit'>Bearbeiten</a>
    

    which means the href doesn't have the value, and there's an errant 123alt attribute that doesn't mean anything in HTML.

    Put the value inside the attribute:

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

    to produce something more like this:

    <a href='edit.php?id=123' alt='edit'>Bearbeiten</a>