Forum Moderators: open

Message Too Old, No Replies

MySQL complex delete (matching row contents)

         

catche

1:45 am on Dec 27, 2006 (gmt 0)

10+ Year Member



I'm using PHP but, I think this is an sql problem. I have a task assignment table that lists the following columns: TASK_ID, USERNAME, and PROJECT_ID. The table is used to assign users to particular tasks (tasks are associated with particular projects). Multiple users can be assigned to a task and a single user can be assigned to multiple tasks. There is no primary index or unique id columns in the table.

What I am trying to do is remove rows matching on selected users and particular task assignments. The only way to avoid deleting the wrong rows is to delete a row only if the information matches on all three columns.

I've got it working ~sometimes~ in a very buggy way with the code below. I'm hoping someone might be able to tell me how I can make it work reliably.

CODE:

$sql="SELECT * FROM $TASK_ASSIGNMENTS_TABLE";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

<table width="100%" border="0" cellpadding="0" cellspacing="0">
<form name="frm" method="post" action="">
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr><INPUT TYPE="hidden" name="PROJECT_ID[]" value="<? echo $rows['PROJECT_ID'];?>"><INPUT TYPE="hidden" name="TASK_ID[]" value="<? echo $rows['TASK_ID'];?>">
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['username'];?>"></td>
</tr>
<?php
}
?>
<tr><td><input name="delete" type="submit" value="DELETE"></form></td></tr></table>

<?
if(isset ($_POST['delete'])){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $TASK_ASSIGNMENTS_TABLE WHERE TASK_ID='$TASK_ID[$i]' AND username='$del_id' AND PROJECT_ID='$PROJECT_ID[$i]'";
$result = mysql_query($sql);
}

I can get it to remove all rows for a particular user if I just match on the username but, trying to match on the above is a real problem.

jatar_k

4:35 pm on Jan 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there shouldn't be a problem with the delete unless you are getting bad data into your delete statement, which would probably make it a php problem

can you better describe what happens when it mosbehaves?
have you isolated why it misbehaves?
can you reproduce the bad behaviour?