Forum Moderators: coopster

Message Too Old, No Replies

delete database base on user choice

delete database base on user choice

         

xbl01234

6:32 am on Dec 18, 2006 (gmt 0)

10+ Year Member



Hello;
i want to let user delete their data from database. Note: one user may have more than one
rows of data.

table as following;

TCid UserName Topic Content
1 q1 aa aa
2 q2 aa aa
3 q3 aa aa
4 q1 aa aa
5 q2 aa aa

And i code as following steps;
1) ask user input user name
2) Display out all the data which the users got. (it is in delete.php file.)
in here, i will use the check box, to let the user choices which data they will delete.
note: i will try to make the Cid relate with the input name, so later on i can delete
the data base on Cid.
3) i will delete the data base on the user choice(it is in deleteR.php file).
but at the movement, i just want to test where i can get the value of the TCid or not
(because i will delete some datas base on the TCid which the user choiced).


The problem i go now, it is from the step 3, i could not get the TCid's value out,
that's mean, i could not delete some data base on the user choiced.

input user name (step 1)


<html>
<body>

<form action='delete.php' method="POST">
Please eneter your userName:
<input type="text" name="userName" value=0>
<br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>

</body>
</html>


delete.php (step 2)


<html>
<body>

<form action='deleteR.php' method="POST">

<?
$con = mysql_connect("localhost", "#*$!xx","#*$!xx");
if (! $con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("#*$!x", $con);

$result = mysql_query("select * from Customer2 where UserName='$_POST[userName]'");

$CArray = array();
$i=0;
while($row = mysql_fetch_array($result)){
$CArray['$i']=$row['TCid'];
?>

<input type="checkbox" name=" <? $CArray['$i']?> " >

<?
echo $row['Topic'];
$i++;
?>
<br>
<?
}
?>

<input type="submit" value="submit">
<input type="reset" value="reset">
</form>

</body>
</html>


deleteR.php (step 3)


<?

$i=0;

while(isset($_POST['$CArray[$i]'])){
echo "The customer will delete their data from the TCid= ";
echo $CArray[$i];
echo "<br>";
$i++;
}

?>

omoutop

7:06 am on Dec 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Perhaps you should try a different approach...

<form>
<?
while($row = mysql_fetch_array($result))
{
?>

<input type="checkbox" name="id_delete[]" value="<? echo $row['TCid'];?>">

<?
}
?>
</form>

This way all your checked ids will be at the $id_delete array.
On post, all you have to do is make a loop in all array values and echo the ids easily

xbl01234

8:01 am on Dec 18, 2006 (gmt 0)

10+ Year Member



><input type="checkbox" name="id_delete[]" value="<? echo $row['TCid'];?>">

after i change to same as above, that's not problem for that.


but after change for the third step as following. I could not received value and print out.

<?

if(isset($_POST['$id_delete'])){
$numId=count($_POST['$id_delete']);
echo $numId; // there is not value in the $numId
while($numId>0){
echo "The TCid is ";
echo $id_delete[$numId];
$numId--;
echo "<br>";
}
}

?>

xbl01234

8:23 am on Dec 18, 2006 (gmt 0)

10+ Year Member



Thanks a lot, i got the solution now.

mcibor

8:47 am on Dec 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Was the problem in

if(isset($_POST['$id_delete'])){
$numId=count($_POST['$id_delete']);

it should be
if(isset($_POST['id_delete'])){
$numId=count($_POST['id_delete']);

?
Regards
Michal

xbl01234

8:51 am on Dec 18, 2006 (gmt 0)

10+ Year Member



i want to ask you again, why i could not print out the value.

in the deleteR.php

$arrdelete = $_REQUEST['id_delete'];

if(is_array($arrdelete)){
$arrdelete = $_REQUEST['id_delete'];
$numId=count($arrdelete);
echo $numId; // i can get this value
echo "<br>";
echo $arrdelete['$num']; // but i could not get this value

mcibor

9:27 am on Dec 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The same mistake... Stop using $ in $array['variable']

$arrdelete = $_REQUEST['id_delete'];

if(is_array($arrdelete)){
$arrdelete = $_REQUEST['id_delete'];
$numId=count($arrdelete);
echo $numId; // i can get this value
echo "<br>";
echo $arrdelete['num']; // but i could not get this value

xbl01234

9:45 am on Dec 18, 2006 (gmt 0)

10+ Year Member



>The same mistake... Stop using $ in $array['variable']

even i change to the following, it does print out anything.

echo $arrdelete['numId']; // but i could not get this value

xbl01234

10:06 am on Dec 18, 2006 (gmt 0)

10+ Year Member



>The same mistake... Stop using $ in $array['variable']
even i change to the following, it does notprint out anything.

echo $arrdelete['numId']; // but i could not get this value

omoutop

11:07 am on Dec 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



try this...

$arrdelete = $_POST['id_delete'];
echo "<pre>";
print_r($arrdelete);
echo "</pre>";

and this way you see is any values are in your array...
so you could check if the error is in the submit form or after

mcibor

11:12 am on Dec 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I reread your posts, and got lost...

$arrdelete = $_POST['id_delete'];// here you pass an array of ids

if(is_array($arrdelete)){//if it's an array, then move further
//$arrdelete = $_REQUEST['id_delete']; delete this line - it's defined above
$numId=count($arrdelete);//here you count how many ids there are for deletion
echo $numId; // now you print it out to the browser
echo "<br>";
echo $arrdelete['num']; //Here you are declaring a new field in array $arrdelete, which is of course empty

So I really don't know what you want to accomplish by using $arrdelete['num']? when the number of rows you have in $numId and the ids you have in $arrdelete[0], $arrdelete[1], etc...

Michal

henry0

12:27 pm on Dec 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do not want make the matter more complicated
However allowing user to delete from a DB could be a risky proposition
You need to strongly ID the user and strongly filter its input

In my mind a better solution consists in adding two columns:
published char(1) NOT NULL default 'y'
and a timestamp

To display the user content on your site your original query should include:
whatever from whatever WHERE user_id=’$user_id’ AND published=’y’ ”;

Use the check box system (for the user)
Default=Published
Check=unpublished (which triggers the timestamp)
If unpublished is checked then the user content does not show

By doing so you have a better DB protection
Then after a while you could query for example for:
Timestamp plus a month and delete them all
It will be your delete action and not a user action.

xbl01234

11:50 pm on Dec 18, 2006 (gmt 0)

10+ Year Member



>try this...
>$arrdelete = $_POST['id_delete'];
>echo "<pre>";
>print_r($arrdelete);
>echo "</pre>";

>and this way you see is any values are in your array...
>so you could check if the error is in the submit form or after

yes, it can print out the value of Tid.

i known the reason now, because i use the wrong index for array.

the $numId is used to find out how many Tcid in the array. And in that case, it just has one Tid in the array, so the value of the numId is 1.

Basing on the above case, i need to print out the Tid which the array hold shold like that
$index=$numId-1;
echo $arrdelete[$index];

before i wrote like that
//$numId=1, and the array just got one element at the movement, so the index should be 0.
echo $arrdelete[$numId]

Thanks a lot

xbl01234

12:03 am on Dec 19, 2006 (gmt 0)

10+ Year Member



>So I really don't know what you want to accomplish by using >$arrdelete['num']? when the number of rows you have in $numId and >the ids you have in $arrdelete[0], $arrdelete[1], etc...

Hi Michal;
i am very sorry that i make mistake with the typing. it should be $arrdelete[$numId].

and also i make mistake with the index of the array, because the $numId just represent the numId in the arry, and at that time, it just got one element in the array,

so if do like
echo $arrdelete[1] // it does not work.

it should like this
echo $arrdelete[0] //so i can print out the value.


I am appreciate your helping, sorry to bother so much.

xbl01234

12:18 am on Dec 19, 2006 (gmt 0)

10+ Year Member



Hi henry0;

Thanks for your advice.

at the movement, i just try to do some pratical,not for real.

mcibor

12:02 pm on Dec 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>I am appreciate your helping, sorry to bother so much.

No bother

Keep doing that - that way you will learn faster ;)

Regards and Merry Christmas!

Michal