2012-01-03 61 views
0

我已將下面的代碼放在一起,並允許我刪除成員。將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'; 
    } 
} 
?> 
<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元素結合在一起。

+1

'不知道如何將PHP和HTML元素結合在一起。「你是什麼意思?你已經在你發佈的代碼中做了它... – 2012-01-03 16:19:33

+0

也許他問如何使它成爲一張桌子而不是列表? – Rooster 2012-01-03 16:24:08

回答

2

我eddited你的代碼和替換列表項(UL和IL)與表標籤(表,TR ,和td)。

$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>'; 
} 
+0

嗨,非常感謝您花時間回覆我的帖子,這基本上是我在尋找的內容,但是請您告訴我,我需要更改哪些內容以便只有一個「刪除所選內容」按鈕,通過單選按鈕進行選擇。親切的問候。 – IRHM 2012-01-03 16:43:18

+1

@IRHM只需將該行刪除,然後將其添加到自己的tr標記中,然後加上'echo'';'行(循環外部)。 – James 2012-01-03 17:19:12

+0

嗨@詹姆斯,非常感謝您花時間幫助解決這個問題。我已經完成了我以爲你在說什麼,但是我明顯做錯了什麼,因爲我收到了錯誤: 解析錯誤:語法錯誤,第83行中出現意外的T_CONSTANT_ENCAPSED_STRING whch是這一行:「」; 我修改了原始文章中的代碼。你可能請看看這個,讓我知道我做錯了什麼。親切的問候 – IRHM 2012-01-03 18:24:12

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>'; 
+0

嗨,非常感謝您花時間回覆我的帖子。非常感謝。親切的問候。 – IRHM 2012-01-03 16:50:39

+0

傳遞鬥! – 2012-01-06 15:52:36

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事件,因爲我不確定你使用的是什麼,也不需要執行這個任務。如果你使用它作爲別的東西,它可以放回去。

在頂部,我只是讓它檢查是否按下提交按鈕。讓我知道這是否適合你。

+0

嗨,非常感謝您的回覆我的文章,這是真正的讚賞。親切的問候。 – IRHM 2012-01-03 16:51:18

+0

@IRHM這是否回答您的問題,還是仍然存在問題? – James 2012-01-03 22:22:33

+0

嗨@詹姆斯,不幸的是這不起作用。只要我添加,點擊'刪除選定的成員'按鈕,整個屏幕刷新,併成爲一個空白的網頁瀏覽器頁面。然後,當我進行手動刷新時,我選擇刪除的成員未被刪除。親切的問候 – IRHM 2012-01-04 13:52:31