0

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>";
}?>
Korken55
  • 233
  • 1
  • 3
  • 14
  • 1
    I suggest you don't echo HTML, doing it [this way](http://pastebin.com/sS1uuj5y) instead helps as you don't have lots of nested quotes to consider, your editor has an easier job syntax highlighting the code for you, and all in all just makes it easier to work with. – JimL Jun 01 '16 at 18:02

6 Answers6

4

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>
David
  • 208,112
  • 36
  • 198
  • 279
1

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>";
    }
AsgarAli
  • 2,201
  • 1
  • 20
  • 32
1

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>";
}?>
Nikola
  • 33
  • 1
  • 9
  • Developers get confused by seeing opening and closing quotes. PHP provides the way not to close quotes if you don't want to treat variable as string. http://stackoverflow.com/a/3446286/2240375 – AsgarAli Jun 01 '16 at 18:21
0

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

Passionate Coder
  • 7,154
  • 2
  • 19
  • 44
0

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

' alt='edit'>Edit

This passes the id value to next page as url

Dk S
  • 1
0

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

Dk S
  • 1