Jump to content

[SOLVED] Variable problem in script to delete from MySQL


Chappers

Recommended Posts

Hi, I've started writing my own PHP script to allow me to modify information in my MySQL database from a web page. I can't work out one thing though, so if someone could please help I'd appreciate it. You'll see I'm in testing mode still, so the delete query is not present, I'm just trying to echo the selection made for deletion to prove the concept works:
[code]<?php
require("connect.php");

echo "
<h1>Choose user to delete</h1>
<form action='{$_SERVER['PHP_SELF']}' method='post'>
<table align='center' border='1' cellspacing='0' cellpadding='3'>";

$result = mysql_query("SELECT username FROM example ORDER BY username");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$username = $row["username"];
echo "
<tr>
<td><span class='n'>{$username}</span></td>
<td><input type='checkbox' name='userid'></td>
<td><input type='hidden' name='{$username}'></td>
</tr>";
}

echo "
<tr><td colspan='3'>
<input type='submit' name='submit' value='Delete'>
</td></tr>
</table>
</form>";

if (isset($_POST['submit'])) {
if (!$_POST['userid']) {
echo "You did not select a user to delete.";
}
else {
echo "Selected {$_POST[HELP HERE!]} from example for deletion";
}
}

mysql_free_result($result);
mysql_close();
?>[/code]
The line that states [b]echo "Selected {$_POST[HELP HERE!]} from example for deletion";[/b] is where I need help. I wish for this to happen:

There will be an array of usernames from the table printed on the screen, each having a checkbox. The data posted must be unique to each username so that the next step of deletion knows which user to delete, so I'm posting the username as a variable.

*EDIT*
I shouldn't code into the early hours, or I tend to do dumb things, like forget that I was only setting the checkbox [b]name[/b], and not the [b]value[/b], and was confusing the name for the value, otherwise I'd have realised a static name can be used for the variable with the [i]value[/i] data containing the $username variable... D'Oh!
*EDIT*

James
[code=php:0]
<?php

include('connect.php');


if (isset($_POST['submit'])) {
$delete = $_POST['delete'];
if(count($delete) > 0) {
foreach($delete as $v) {
if(is_numeric($v) && $v > 0) {
$del[] = $v;
}
}
}
if(count($del) > 0) {
$delin = implode(",",$del);
$q = mysql_query("DELETE FROM example WHERE id IN ({$delin})");
printf("Records deleted: %d\n", mysql_affected_rows());
}
else {
echo "No users selected, or incorrect values were selected";
}
}


echo "
<h1>Choose user to delete</h1>
<form action='{$_SERVER['PHP_SELF']}' method='post'>
<table align='center' border='1' cellspacing='0' cellpadding='3'>";

$result = mysql_query("SELECT username,id FROM example ORDER BY `username`") or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$username = $row["username"];
echo "
<tr>
<td><span class='n'>{$username}</span></td>
<td><input type='checkbox' name='delete[]' VALUE=\"{$row['id']}\"></td>
</tr>";
}

echo "
<tr><td colspan='3'>
<input type='submit' name='submit' value='Delete'>
</td></tr>
</table>
</form>";


mysql_free_result($result);
mysql_close();

?>
[/code]

This makes <option name="delete[]" value="id"> where id is the usernames' ids.  This creates an array that is later manipulated in php and made into a DELETE FROM table WHERE field IN (value1,value2) so on thing...
Many thanks for your help, I'm not sure I'd ever have realised the fact I'd left out the value from the checkbox and was trying to stick my variable into the name field, meaning I had no static name to grab from the $_POST variable.

This is my new code:
[code]<?php
require("connect.php");

if (isset($_POST['submit'])) {
$userid = $_POST['userid'];
if (!$userid) {
echo "<center><span class='n'>You did not select a username to delete.</span></center>";
}

else {
foreach ($userid as $b) {
mysql_query("DELETE FROM members WHERE username='$b'");
echo "{$b} username deleted";
}
}
}

echo "
<center><h2>Choose user to delete</h2></center>
<form action='{$_SERVER['PHP_SELF']}' method='post'>
<table align='center' border='1' cellspacing='0' cellpadding='3'>";

$result = mysql_query("SELECT username FROM members ORDER BY username");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$username = $row["username"];

echo "
<tr><td><input type='checkbox' name='userid[]' value='{$username}'></td>
<td><span class='n'>$username</span></td></tr>";
}

echo "
<tr><td colspan='2' align='center'>
<input type='submit' name='submit' value='Delete'></td></tr>
</table>
</form>";

mysql_free_result($result);
mysql_close();

?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.