-
as i said i did this in localhost
i manage to convert my radio button delete form to check boxes..how and where do i put insert query in delete.php ?
PHP Code:
<?php
include "connect.php";
$result = mysql_query("SELECT * FROM person");
echo '<form action="delete.php" method="post">';
echo '<table border="1">
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>';
$count = 1;
while($row = mysql_fetch_array($result))
{
$id=$row['id']; echo "<tr>";
echo "<td>";
echo $count++;
echo "<input type='checkbox' name='id[]' value='$id'></td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
}
echo '</table>
<br />
<input type="submit" value="Delete" />
</form>';
include "menu.php";
mysql_close($con);
?>
delete.php
PHP Code:
<?php
include "connect.php";
$id=$_POST[id];
for($i=0;$i<count($_POST['id']);$i++)
mysql_query("DELETE FROM person WHERE id={$id[$i]}");
echo "$i records deleted";
include "menu.php";
mysql_close($con);
?>
-
As I see it your problem is that you are not passing the value $id as a POST variable to delete.php.
Try changing this :
PHP Code:
echo "<input type='checkbox' name='id[]' value='$id'></td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
to this :
PHP Code:
echo "<input type='checkbox' name='id[]' value='$id'></td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td input type='hidden' name='id' value='".$row[id]."'></td>";
echo "</tr>";
Or change the checkbox line to this:
PHP Code:
echo "<input type='checkbox' name='id[]' value='".$id."'></td>";
The input type="hidden" is a useful way of passing variables to PHP that you don't want to be visible on the input screen
-
thanks..
i did it like this and it worked
delete.php
PHP Code:
<?php
include "connect.php";
$id=$_POST[id];
for($i=0;$i<count($_POST['id']);$i++)
{
$result = mysql_query("SELECT * FROM person WHERE id={$id[$i]}") or die(mysql_error());
$row = mysql_fetch_array( $result ) or die(mysql_error());
mysql_query("INSERT INTO bin (id, firstname, lastname, age)
VALUES
(". $row['id'] . ",". $row['firstname'] . ",". $row['lastname'] . ",". $row['age'] . ")");
mysql_query("DELETE FROM person WHERE id={$id[$i]}");
}
echo "$i records deleted";
include "menu.php";
mysql_close($con);
?>
SOLVED..
-
Of course! I can't believe I didn't see that. I guess some problems are simpler than they look. :)
-
can someone help me to check this...what did i do wrong
i add the ability to restore back the data in the 'recycle bin' (restore.php)
last time i didn't input/check it properly
the problem if i input first name,last name as text it is not inserted to the bin table...
but if i put first name,last name as numbers it is ok it is inserted to the bin table and i can restore it back to person table
and how do i put add new ability to delete in restore.php...
newbie here
please help...thanks
Person
http://img690.imageshack.us/i/person.jpg/
Bin
http://img215.imageshack.us/i/bint.jpg/
mylist.php
PHP Code:
<?php
include "connect.php";
$result = mysql_query("SELECT * FROM person");
echo '<form action="mydelete.php" method="post">';
echo '<table border="1">
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>';
$count = 1;
while($row = mysql_fetch_array($result))
{
$id=$row['id']; echo "<tr>";
echo "<td>";
echo $count++;
echo "<input type='checkbox' name='id[]' value='$id'></td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
}
echo '</table>
<br />
<input type="submit" value="Delete" />
</form>';
include "menu.php";
mysql_close($con);
?>
mydelete.php
PHP Code:
<?php
include "connect.php";
$id=$_POST[id];
for($i=0;$i<count($_POST['id']);$i++)
{
$result = mysql_query("SELECT * FROM person WHERE id={$id[$i]}") or die(mysql_error());
$row = mysql_fetch_array( $result ) or die(mysql_error());
mysql_query("INSERT INTO bin (id, firstname, lastname, age)
VALUES
(". $row['id'] . ",". $row['firstname'] . ",". $row['lastname'] . ",". $row['age'] . ")");
mysql_query("DELETE FROM person WHERE id={$id[$i]}");
}
echo "$i records deleted";
include "menu.php";
mysql_close($con);
?>
restore.php
PHP Code:
<?php
include "connect.php";
$result = mysql_query("SELECT * FROM bin");
echo '<form action="restore-success.php" method="post">';
echo "<H4>Deleted Items</H4>";
echo "Select To Restore";
echo '<table border="1">
<tr>
<th>No</th>
<th>Deleted ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>';
$count = 1;
while($row = mysql_fetch_array($result))
{
$bin_id=$row['bin_id'];
echo "<tr>";
echo "<td>";
echo $count++;
echo "<input type='checkbox' name='bin_id[]' value='$bin_id'></td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "</tr>";
}
echo '</table>
<br />
<input type="submit" value="Restore" />
</form>';
include "menu.php";
mysql_close($con);
?>
restore-success.php
PHP Code:
<?php
include "connect.php";
$bin_id=$_POST[bin_id];
for($i=0;$i<count($_POST['bin_id']);$i++)
{
$result = mysql_query("SELECT * FROM bin WHERE bin_id={$bin_id[$i]}") or die(mysql_error());
$row = mysql_fetch_array( $result ) or die(mysql_error());
mysql_query("INSERT INTO person (id, firstname, lastname, age)
VALUES
(". $row['id'] . ",". $row['firstname'] . ",". $row['lastname'] . ",". $row['age'] . ")");
mysql_query("DELETE FROM bin WHERE bin_id={$bin_id[$i]}");
}
echo "$i records restored";
include "menu.php";
mysql_close($con);
?>
-
also tried below code but not working....this code is working perfectly if i input numbers for all fields i can delete and restore - but as text its failed to be inserted to bin table
PHP Code:
<?php
include "connect.php";
$id_array=$_POST[id];
foreach($id_array as $id)
{
$result = mysql_query("SELECT * FROM person WHERE id=$id") or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$age=$row['age'];
}
echo $firstname;
mysql_query("INSERT INTO bin (id, firstname, lastname, age)
VALUES ($id,$firstname,$lastname,$age)");
mysql_query("DELETE FROM person WHERE id=$id");
echo "Record deleted and inserted to recycle bin";
}
mysql_close($con);
?>