如何设置提交按钮以使用具有基于其数据库的不同ID的名称的post方法显示
问题描述:
This is my query. I want to display the submit button with name depending on its ID.
<?php
$sql = mysql_query("SELECT * FROM tb_topic");
while($row=mysql_fetch_assoc($sql)){
$t_id = $row['t_id'];
$t_title = $row['t_title'];
$t_desc = $row['t_desc'];
echo "<a class='list-group-item'><input type='submit' name='$t_id' class='submitLink' value='$t_title'></a>";
}
?>
Now i want to display the data using the ID from the link.
<?php
if($_POST['$t_id']){
echo "$";
$sql = mysql_query("SELECT * FROM tb_topic WHERE t_id='$t_id'");
while($row=mysql_fetch_assoc($sql)){
$t_title = $row['t_title'];
$t_desc = $row['t_desc'];
echo $t_title;
echo $t_desc;
}
?>
is this possible?
i can make it display if i use:
<?php
if($_POST['1']){
echo "1";}
if($_POST['2']){
echo "2";`*enter code here*`
}
?>
这是我的查询。 我想显示名称取决于其ID的提交按钮。 p>
&lt;?php
$ sql = mysql_query(“SELECT * FROM tb_topic”);
while($ row = mysql_fetch_assoc($ sql)){\ n $ t_id = $ row ['t_id'];
$ t_title = $ row ['t_title'];
$ t_desc = $ row ['t_desc'];
echo“&lt; a class =' list-group-item'&gt;&lt; input type ='submit'name ='$ t_id'class ='submitLink'value ='$ t_title'&gt;&lt; / a&gt;“;
}
\ n?>&n code> pre>
现在我想使用链接中的ID显示数据。 p>
&lt;?php
if($ _ POST ['$ t_id']){
echo“$”;
$ sql = mysql_query(“SELECT * FROM tb_topic WHERE t_id ='$ t_id'“);
while($ row = mysql_fetch_assoc($ sql)){
$ t_title = $ row ['t_title'];
$ t_desc = $ row [ 't_desc'];
echo $ t_title;
echo $ t_desc;
}
?&gt;
code> pre>
是这个 可能? p>
如果我使用的话,我可以显示: p>
&lt;?php
if($ _ POST ['1' ]){
echo“1”;}
if($ _ POST ['2']){
echo“2”;`*在此处输入代码*`
}
?&gt;
code> pre>
div>
答
Yes use variable variables
<?php
foreach($_POST as $key=>$value){
${$key} = $value;
// or something like
if(${$key} == "1"){
// do something
}
}