简体   繁体   English

href中的PHP传递ID

[英]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. 所以我需要链接到另一个php文件,同时从我的数据库Fetches中传递ID。 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. 到目前为止一切正常,但是当我进入edit.php页面时,'a href'不会显示任何ID。

  <?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. 您正在将ID添加到属性之外 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. 这意味着href没有该值,并且有一个错误的123alt属性,在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 在您的情况下,它会由于php引号和单引号混合而中断

You can also do like this also and this works fine: 您也可以这样做,并且效果很好:

' alt='edit'>Edit 'alt ='edit'>编辑

This passes the id value to next page as url 这会将id值作为url传递到下一页

instead of using this id =".$row["id"]." 而不是使用此id =“。$ row [” id“]。” ; ; you can also use this id=<?=$row["id"]?> 您还可以使用此id=<?=$row["id"]?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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