Jump to content

Delete MySql row using php.


Teng

Recommended Posts

Hey guys, I'm having a little trouble using the delete function.

 

I have a number of activites a user is assigned too. This is pretty much how my db structure is.

 

Activities

ActivityID (PK)

ActivityName

 

Assignments

AssignmentID (PK)

ActivityID

UserID

Location

 

Users

UserID (PK)

Firstname

Lastname

Address

 

So what i have done is at the bottom of my page is an add feature which accesses the Activities table and with a drop down menu lists all the activities available. Next to that is the Location of the activity they can select. Then next to these is an Add button. This directs the user to Add.php and checks the session to see if they are logged in. Then adds the selected Activity to the Assignments table.

 

Then above the bit where it lists all the activites which you can add is a list of Activites the user has been assigned. The php accesses the Assignments table and checks the User's session and matches it against the UserID in the Assignments table. It then displays using a for loop all the Activites the user has been assigned. Next to each of these activites is a remove button. Which when clicked I want it to delete the AssignmentID, ActivityID, UserID and Location row from the Assignments table. However the problem i have is I don't know how to tell it to delete a specific row as there is nothing linking the remove buttons to the individual activites like when I add them.

 

Any help would be great thanks.

 

 

Link to comment
Share on other sites

How do i assign that? here is my code. If i were assigning hidden id attributes in a for loop wouldnt it just overwrite each time? Sorry am very new to php.

 

 

<form name="Remove" method="post" action="Remove.php">

<table width="100%">

 

<?

$query = "select *from assignments where UserID = '" . $_SESSION['usertid'] ."'";

$result = $db_conn->query($query);

$num_results = $result->num_rows;

 

for ($i=0; $i <$num_resultsEnrol; $i++)

{

$row = $result->fetch_assoc();

$ActivityID = $row["ActivityID"];

$query1 = "select *from Activities where ActivityID = '" . $ActivityID."'";

$result1 = $db_conn->query($query1);

$row = $result1->fetch_assoc();

echo '<tr>';

echo '<td>'.$row["ActivityID"].': '.$row["ActivityName"].'</td>';

echo '<td align="right"><input type="submit" value="Remove"></td>';

echo '</tr>';

}

?>

Link to comment
Share on other sites

OK i've decided on a slightly different approach to this. I want to assign to the button invidual links does that make sense? For example it will be like .....remove.php?activity=Act1

 

but when i try this it doesnt work.

 

<?       

        $query = "select *from assignments where UserID = '" . $_SESSION['usertid'] ."'";

        $result = $db_conn->query($query);

        $num_results = $result->num_rows;

 

        for ($i=0; $i <$num_resultsEnrol; $i++)

        {

            $row = $result->fetch_assoc();

            $ActivityID = $row["ActivityID"];

            $query1 = "select *from Activities where ActivityID = '" . $ActivityID."'";

            $result1 = $db_conn->query($query1);

            $row = $result1->fetch_assoc();

            echo '<form name="Remove" method="post" action="Remove.php?unit=$ActivityID=>';

            echo '<table width="100%">';

            echo '<tr>';

            echo '<td>'.$row["ActivityID"].': '.$row["ActivityName"].'</td>';

            echo '<td align="right"><input type="submit" value="Remove"></td>';

            echo '</tr>';

        }

?>

 

But it says something like requested page not found ....Remove.php?unit=$ActivityID=<table width....

 

Whats wrong with it???

Link to comment
Share on other sites

<form name="Remove" method="post" action="Remove.php">
		<table width="100%">
<?			
		$query = "select assignments.*, Activities.ActivityName from assignments, Activities where UserID = '" . $_SESSION['usertid'] ."' and assignments.AssignmentID = Activities.AssignmentID";
		$result = $db_conn->query($query);
		$num_results = $result->num_rows;

		for ($i=0; $i <$num_resultsEnrol; $i++)
		{
			$row = $result->fetch_assoc();
			$ActivityID = $row["ActivityID"];
			//$query1 = "select *from Activities where ActivityID = '" . $ActivityID."'";
			//$result1 = $db_conn->query($query1);
			//$row = $result1->fetch_assoc();
			echo '<tr>';
			echo '<td>'.$row["ActivityID"].': '.$row["ActivityName"].'</td>';
			echo '<td align="right"><input type="submit" value="Remove" name="',$row['AssignmentID'],'></td>';
			echo '</tr>';
		} 
                        echo '</table></form>';
?>
[/quote]and in Remove.php[code]$key = array_search('Remove', $_POST);
if($key) {
   $query = "DELETE FROM assignments WHERE AssignmentID=$key";
   $result = $db_conn->query($query) or die($query);
}

[/code]

Link to comment
Share on other sites

OK so i changed the code around and now using html link to remove.php

 

echo '<td align="right"><a href=remove.php?enrol='.$AssignmentID.'>Remove</a></td>';

 

So this creates a link such as remove.php?Assignment=1

 

But now having trouble in remove.php which i basically have nothing on how to get the Assignment=1 into a variable like for example $AssignmentLink = //code to identify that Assignment=1 therefore i would use that to say ok delete from assignments the row with assignment id as 1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.