如何为显示SQL表的行的PHP/HTML网页的每一行创建按钮或超链接?

问题描述:

我为我的web应用程序使用Linux,Apache,PHP和Postgres。我有一个返回SQL表格内容的Web应用程序。用户必须登录并键入有效的SQL表名。如何为Web界面中返回并显示的每一行创建一个按钮?它必须是动态的,因为返回的行根据用户输入的表而变化。如何为显示SQL表的行的PHP/HTML网页的每一行创建按钮或超链接?

最终我希望让用户通过Web UI更新SQL数据库中的行。现在我想要一个按钮出现在每一行返回。如果按钮或链接可用于每个单元格(不管单元格是否为空),那就行了。返回的行当前处于静态字母数字文本中。我希望有一个超链接或每个按钮的按钮。然后,我可以让用户从该链接或按钮转到新的.php网页。此页面将允许更新SQL表。

如何在网页中为返回并显示在PHP网页中的每个SQL行创建按钮或超链接?

下面的代码省略了认证网页。这是我反复调用的网页代码,因为它允许用户键入表名并查看它:

<?php 

session_start(); 

$pa = $_POST["table"]; 

    $host = $_SESSION['hostv']; 
    $port = $_SESSION['portv']; 
    $dbname = $_SESSION['dbv']; 
    $credentials = $_SESSION['cv']; 
    $comp = $credentials; 

    $db = pg_connect("$host $port $dbname $credentials" ); 
    $query = 'select * from ' . $pa . ';'; 
    $cc = pg_query($query); 


showingfunc($cc); 

function showingfunc($cc) 
{ 
/* This code below was copied and modified from razorsql.com */ 
$i = 0; 
echo '<html><body><table><tr>'; 
if ($cc) { 
    } 
else { 
echo '<td>' . "Failure." . $dbname . '</td>'; 
} 
while ($i < pg_num_fields($cc)) 
{ 
     $fieldName = pg_field_name($cc, $i); 
     echo '<td>' . $fieldName . '</td>'; 
     $i = $i + 1; 
} 
echo '</tr>'; 
$i = 0; 
while ($row = pg_fetch_row($cc)) 
{ 


     echo '<tr>'; 
     $count = count($row); 
     $y = 0; 
     while ($y < $count) 
     { 
       $c_row = current($row); 
       echo '<td>' . $c_row . '</td>'; 
       next($row); 
       $y = $y + 1; 
     } 
     echo '</tr>'; 
     $i = $i + 1; 
    } 
pg_free_result($cc); 

echo '</table></body></html>'; 


} 

?> 

<html> 
<body> 

<form action="repeater.php" method="post"> 
TableToView: <input type="text" name="table"<br> 

     <input type="submit"> 
</form> 

<a href="logout.php">Logout</a> 
</body> 
</html> 

只需添加一个包含操作按钮的单元格即可。

while ($row = pg_fetch_row($cc)) 
{ 
    echo '<tr>'; 
    $count = count($row); 
    $y = 0; 
    while ($y < $count) 
    { 
      $c_row = current($row); 
      echo '<td>' . $c_row . '</td>'; 
      next($row); 
      $y = $y + 1; 
    } 
    //New Code on the line below 
    echo '<td><a class="edit" href="[YOURLINK]">Edit</a></td>' //You Probably want to add an ID to your query string here 
    echo '</tr>'; 
    $i = $i + 1; 
} 

另外,以类似的方式在头中添加一个空单元格。

你只需要在你的代码中添加一个td标签... 我们只是通过标签的href传递了你的记录的id。 我们只是用简单的方法

while ($y < $count) 
    { 
      $c_row = current($row); 
      echo '<td>' . $c_row . '</td>'; 
      next($row); 
      $y = $y + 1; 
    } 

代替你的代码替换: -

while ($y < $count) 
    { 
      $c_row = current($row); 
      echo '<td>' . $c_row . '</td>'; 
      echo '<td><a href="new.php?cid='.$c_row.'">Update</a></td>'; 
      next($row); 
      $y = $y + 1; 
    } 

,你会做得到另一页上此ID: -

$id=$_REQUEST['cid']; 

,并通过它到你的查询中,这样你就可以得到表中所有的信息。 请让我知道如果有任何问题或如果不工作...谢谢

+0

我想用这个。在new.php中,我应该把“$ id = $ _ REQUEST ['id'];”节?我试过了。但我似乎无法让它工作。 $ id被赋值为null。我认为第一个答案就足够了。但我很想保留行ID。我认为这个答案可能有更大的潜力。如何获得new.php中的$ c_row或行号值? – Victor