将PHP输出插入表中

时间:2012-01-03 16:16:43

标签: php html

我将下面的代码放在一起列出并允许我删除成员。

解决方案

    <?php
    $db_host = 'hostname';
    $db_user = 'username';
    $db_pwd = 'password';

    $database = 'databasename';
    $table = 'tablename';
    // use the same name as SQL table

    if (!mysql_connect($db_host, $db_user, $db_pwd)) 
    die("Can't connect to database"); 

    if (!mysql_select_db($database)) 
    die("Can't select database"); 

    function sql_safe($s)  
{  
    if (get_magic_quotes_gpc())  
        $s = stripslashes($s);  

    return mysql_real_escape_string($s);  
}  

if ($_SERVER['REQUEST_METHOD'] == 'POST')  
{  
    if (isset($_POST["submit"]))  
    {  
        if (isset($_POST['del'])) 
            $userid = intval($_POST['del']); 

        if (mysql_query("DELETE FROM {$table} WHERE userid={$userid}")) 
            $msg = 'The member who you selected has now been deleted!';  
        else 
            $msg = 'Could not delete the selected member'; 
    } 
}  
?> 
<html><head> 
<title>Save Photo(s) to Find</title>
<style type="text/css">
<!--
.style1 {
    color: #FFFFFF;
    font-size: 18px;
    font-family: Calibri;
}
.style2 {
    font-size: 18px;
    font-weight: bold;
    font-family: Calibri;
}
.style3 {font-family: Calibri}
.style6 {font-size: 16px}
.style7 {font-family: Calibri; font-size: 16px; }
-->
</style>
</head> 
<body>
<p class="style7">
<?php 
if (isset($msg)) // this is special section for 
// outputing message 
{ 
?></p>
<p class="style7">
  <?=$msg?>
</p>
<?php 
} 
?>
<form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data"> 
<?php 
$result = mysql_query("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} ORDER BY userid DESC"); 
if (mysql_num_rows($result) == 0) // table is empty 
echo 'There are no members to view'; 
else 
{ 
echo "<table>\n"; 
while(list($userid, $forename, $surname, $subscriptionexpiration) = mysql_fetch_row($result)) 
{ 
echo "<tr>\n" 
."<td><input type='radio' name='del' forename, surname value='{$userid}' /></td>\n" 
."<td><small>{$forename} {$surname}</small><td>\n" 
."<td><small>{$subscriptionexpiration}</small><td>\n" 
."</tr>\n"; 
} 
echo "<tr><td colspan=\"3\">"; 
echo '<input type="submit" value="Delete Selected Member" name="submit"/>'; 
echo "</td></tr>"; 
echo '</table>'; 
} 
?> 
<input type="hidden" name="action" id="action" /> 
</form> 
</body>
</html>

为了让我更容易一点,我希望能够将输出的列表插入到表格中的表格中,第一列包含项目符号点,单选按钮和成员名称以及最后一列显示“订阅到期日期”。

我试图简单地将php脚本移动到表列中,但这根本不起作用,因为我不能,或者我应该说,不知道如何将PHP和HTML元素合并在一起。 / p>

3 个答案:

答案 0 :(得分:2)

我编写了代码并用表标签(table,tr和td)替换了列表项(ul和il)。

$result = mysql_query("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} ORDER BY userid DESC"); 
if (mysql_num_rows($result) == 0) // table is empty 
echo 'There are no members to view'; 
else 
{ 
    echo "<table>\n";
    while(list($userid, $forename, $surname, $subscriptionexpiration) = mysql_fetch_row($result)) 
    { 
        echo "<tr>\n"
        ."<td><input type='radio' name='del' forename, surname value='{$userid}' /></td>\n"
        ."<td><small>{$forename} {$surname}</small><td>\n"
        ."<td><small>{$subscriptionexpiration}</small><td>\n"
        ."<td><input type=\"submit\" value=\"Delete Selected Member\" onclick=\"document.getElementById('action').value='delete'\" /><td>\n"
        ."</tr>\n";
    } 
    echo '</table>';  
} 

答案 1 :(得分:0)

您可以按如下方式修改代码,只需使用表格元素切换列表元素:

echo '<table>'; 
while(list($userid, $forename, $surname, $subscriptionexpiration) = mysql_fetch_row($result)) 
{ 
// outputing list 
echo "<tr>";
echo "<td><input type='radio' name='del' value='{$userid}' /> &nbsp;<small>{$forename} {$surname}</small></td>";
echo "<td><small>{$subscriptionexpiration}</small></td>";
echo "</tr>";
} 
echo '</table>'; 

答案 2 :(得分:0)

我会留下HTML:

<form action="<?=$PHP_SELF?>" method="POST" enctype="multipart/form-data"> 
<?php  
$result = mysql_query("SELECT userid, forename, surname, subscriptionexpiration FROM {$table} ORDER BY userid DESC");  
if (mysql_num_rows($result) == 0) // table is empty  
echo 'There are no members to view';  
else  
{  
    echo "<table>\n"; 
    while(list($userid, $forename, $surname, $subscriptionexpiration) = mysql_fetch_row($result))  
    {  
        echo "<tr>\n" 
        ."<td><input type='radio' name='del' forename, surname value='{$userid}' /></td>\n" 
        ."<td><small>{$forename} {$surname}</small><td>\n" 
        ."<td><small>{$subscriptionexpiration}</small><td>\n" 
        ."</tr>\n"; 
    }  
    echo "<tr><td colspan=\"3\">";
    echo '<input type="submit" value="Delete Selected Member" name="submit"/>';
    echo "</td></tr>";    
    echo '</table>';   
}  
?>
<input type="hidden" name="action" id="action" /> 
</form>

对于我的PHP:

<?php
$db_host = 'hostname';
$db_user = 'username';
$db_pwd = 'password';

$database = 'databasename';
$table = 'tablename';
// use the same name as SQL table

if (!mysql_connect($db_host, $db_user, $db_pwd)) 
    die("Can't connect to database"); 

if (!mysql_select_db($database)) 
    die("Can't select database"); 

function sql_safe($s) 
{ 
    if (get_magic_quotes_gpc()) 
        $s = stripslashes($s); 

    return mysql_real_escape_string($s); 
} 

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{ 
    if (isset($_POST["submit"])) 
    { 
        if (isset($_POST['del']))
            $userid = intval($_POST['del']);

        if (mysql_query("DELETE FROM {$table} WHERE userid={$userid}"))
            $msg = 'The member who you selected has now been deleted!'; 
        else
            $msg = 'Could not delete the selected member';
    }
} 
?> 

我拿出exit(),因为这是导致页面停止加载的原因(白屏)。我还为提交按钮取出了onclick事件,因为我不确定您使用的是什么,并且不需要执行此任务。如果您将其用于其他内容,则可以将其重新放入。

在顶部,我只是检查是否按下了提交按钮。如果这对您有用,请告诉我。