简体   繁体   中英

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

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>

You do not need to close double quote for variable.

Inside double quote, variables are not treated as string.

But inside single quote, variables are treated as string.

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

This is correct way:

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

Change you last td to this

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

In your case it breaks in between due to php doble quotes and single quotes mixing

You can also do like this also and this works fine:

' alt='edit'>Edit

This passes the id value to next page as url

instead of using this id =".$row["id"]." ; you can also use this id=<?=$row["id"]?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM