Jump to content

delete data from database base on user choice


xbl1

Recommended Posts

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 Content1 q1 xx xxx2 q2 xx xxx3 q3 xx xxx4 q1 xx xxx5 q2 xx xxxAnd i code as following steps;1) ask user input user name2) 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 deletethe 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", "xxxxx","xxxxx");  if (! $con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db("xxxx", $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++;  }?>

Link to comment
Share on other sites

in deleteR.php you can like this:

<?php		$arrdelete = $_REQUEST['delete_id'];	if(is_array($arrdelete))	{		while(list($key, $value) = each($arrdelete))		{			$sql = "delete xxx where id=".$value;			mysql_query($sql);		}	}?>

Link to comment
Share on other sites

in deleteR.php you can like this:
<?php		$arrdelete = $_REQUEST['delete_id'];	if(is_array($arrdelete))	{		while(list($key, $value) = each($arrdelete))		{			$sql = "delete xxx where id=".$value;			mysql_query($sql);		}	}?>

Thanks for your help, it does work now. :):)
Link to comment
Share on other sites

Thanks for your help, it does work now. :):)
:blink::) 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 }
Link to comment
Share on other sites

You did not set $num. Also, since you have $num is single quotes, it is looking for the string "$num", not the value of the variable $num.

$arrdelete = $_REQUEST['id_delete']; if(is_array($arrdelete)){   $numId=count($arrdelete);   echo $numId; // i can get this value   echo "<br>";   foreach ($arrdelete as $key => $val)	echo "key: {$key}; value: {$val} <br>";}

Link to comment
Share on other sites

You did not set $num. Also, since you have $num is single quotes, it is looking for the string "$num", not the value of the variable $num.
$arrdelete = $_REQUEST['id_delete']; if(is_array($arrdelete)){   $numId=count($arrdelete);   echo $numId; // i can get this value   echo "<br>";   foreach ($arrdelete as $key => $val)	echo "key: {$key}; value: {$val} <br>";}

Thanks a lot, it is nice code, i like it :)
Link to comment
Share on other sites

There's one other problem, when you get all the CIDs:

$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'] ?> " >

This is the problem:$CArray['$i']=$row['TCid'];The problem is that you are using the array index '$i'. This is using the string '$i' for the key, not the value of the variable $i. Single quotes do not do variable replacement, so if you put a variable inside single quotes it does not get replaced with the variable's value. If you want to use the value of $i, just don't even quote it at all:$CArray[$i]=$row['TCid'];I've seen a lot of people put quotes around a single variable like that on here, if you are only using one variable, you do not need to quote it! Quotes are for strings only.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...