[英]Passing ID through href inside while php loop
I have this while loop working fine except for the last line in the table where i can't get the id to pass through correctly. 我有这个while循环工作正常,除了表中的最后一行(我无法正确获取ID)。 It does not give me any error, but when i hover over the "Modify" link it shows the correct ID and right after comes all the html code of the table.
它没有给我任何错误,但是当我将鼠标悬停在“修改”链接上时,它会显示正确的ID,并且紧接着是表格的所有html代码。 It looks like it's not concatenating correctly.
看起来好像没有正确连接。
There's the code: 有代码:
while ($row = mysqli_fetch_assoc($result)){
echo "<table><tr><td>" . $row["id"] . "</td>" .
"<td>" . $row["product_name"] . "</td>" .
"<td>" . $row["visible"] .
"<td><a href=\"modify.php?id=" . $row["id"] . ">Modify</a>" .
"</td></tr></table>";
}
You are using echo
for each record, which is fine, but each time you echo a brand new table. 您对每个记录都使用
echo
,这很好,但是每次您都回显一个全新的表时。 What you probably mean is to echo only a new row. 您可能的意思是仅回显新行。 As others have suggested, your href is also not closed properly
正如其他人建议的那样,您的href也未正确关闭
Do something like: 做类似的事情:
echo '<table>';
while($row = mysqli_fetch_assoc($result)){
echo "<tr>
<td> $row[id] </td>
<td> $row[product_name] </td>
<td> $row[visible] </td>
<td><a href='modify.php?id=$row[id]'>Modify</a></td>
</tr>";
}
echo '</table>';
Note that because you're writing HTML, you don't need to concatenate many strings. 请注意,因为您正在编写HTML,所以不需要连接许多字符串。 You can have one long multi-line string.
您可以使用一个长的多行字符串。 The browser will render the table just fine even if you have line breaks in your string.
即使您的字符串中有换行符,浏览器也会很好地呈现表格。 In addition, PHP can parse double-quoted strings and substitute variables for their string values, so you don't need to do
"<td>" . $row["id"]
另外,PHP可以解析双引号字符串并用变量替换其字符串值,因此您无需执行
"<td>" . $row["id"]
"<td>" . $row["id"]
; "<td>" . $row["id"]
; this works just fine: "<td>$row[id]"
这很好用:
"<td>$row[id]"
It looks like you were missing a "
and not closing your href
. Below I have fixed it by adding the missing "
. 看起来您好像缺少了
"
并且没有关闭href
。下面我通过添加缺少的内容来修复它"
。 Also, I swapped out most of the double quotes for single quotes. 另外,我将大多数双引号替换为单引号。 That was my preference because it made it easier to read;
那是我的偏爱,因为它使阅读更容易。 I suspect you had trouble identifying the missing quote because of the use of only doubles.
我怀疑您由于仅使用双精度字而无法确定缺少的报价。
while ($row = mysqli_fetch_assoc($result)){
echo '<table><tr><td>' . $row["id"] . '</td>' .
'<td>' . $row["product_name"] . '</td>' .
'<td>' . $row["visible"] .
'<td><a href="modify.php?id=' . $row["id"] . '">Modify</a>' .
'</td></tr></table>';
}
You seem to have forgotten to close the href
attribute in your while loop. 您似乎忘记了在while循环中关闭
href
属性。
Also, as the other answer notes, you are using echo | print
另外,作为其他答案,您正在使用
echo | print
echo | print
to print a new table every time you run this loop, which might cause some wierd results. echo | print
打印新的表格每次运行这个循环中,这可能会导致一些奇怪的结果的时间。
Here is an example where you append everything to a string names $table
and prints it out when you are done with the loop: 这是一个示例,其中将所有内容附加到名为
$table
的字符串$table
并在完成循环后将其打印出来:
<?php
//Create start of table:
$table = '<table><tbody>';
//Fetch rows:
while ($row = mysqli_fetch_assoc($result)) {
//append to the table:
$table .= '<tr><td>' . $row['id'] . '</td>' .
'<td>' . $row['product_name'] . '</td>' .
'<td>' . $row['visible'] . //You forgot the " under here
'<td><a href="modify.php?id=' . $row['id'] . '">Modify</a>' .
'</td></tr>';
}
//Append end of table:
$table .= '</tbody></table>';
//print table:
print $table;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.