Reputation: 1326
I've put together the code below that lists and allows me to delete members.
Solution
<?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>
To make it a little easier for me, I'd like to be able to insert the outputted list into a table on my form with the first column containing the bullet point, radio button and name of the member and the last column to show the 'Subscription Expiration Date'.
I've tried to simply move the php script into the table columns but this simply doesn't work because I can't, or should I say, don't know how to incorporate the PHP and HTML elements together.
Upvotes: 0
Views: 8659
Reputation: 3805
I would leave the HTML like:
<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>
And for my 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';
}
}
?>
I took out the exit()
because that's what was causing the page to stop loading (white screen). I also took out your onclick
event for the submit button because I wasn't sure what you were using that for, and it's not needed to perform this task. If you were using it for something else, it can be put back in.
At the top, I just have it checking to see if the submit button was pressed. Let me know if this works for you.
Upvotes: 0
Reputation: 1629
I eddited your code and replaced the list items (ul and il) with table tags (table,tr, and 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>';
}
Upvotes: 2
Reputation: 6709
You can modify your code as follows, just switch out the list elements with table elements:
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}' /> <small>{$forename} {$surname}</small></td>";
echo "<td><small>{$subscriptionexpiration}</small></td>";
echo "</tr>";
}
echo '</table>';
Upvotes: 0