I’m making a button which deletes an record from a mysql database.
I have made a database with 3 tables, 1: user, 2: activity, 3:user_activity.
In the user_activity you can find the UserID (from user) and ActivityID (from activity).
I have alreade made a script that can add both UserID and ActivityID to the user_activity table. But now i also want a button that remove that “row”.
so i made this javascript code with an onclick function:
$(".deleteActivity").click( function() {
var user_activityId = $(this).data('activity');
$.ajax({
type: "POST",
url: "delete_activity.php",
data: {
activity: user_activityId
},
dataType: 'json',
success: function (data) {
}
});
$(this).hide();
});
And I made this delete_activity.php file:
<?php
include "connection.php";
$sql = "SELECT * FROM user WHERE Username = '".$_SESSION['Username']."' ";
$stm = $db->prepare($sql);
$result = $stm->execute(array());
while($row = $stm->fetch(PDO::FETCH_ASSOC)) {
$userID = $row['UserID'];
echo $userID;
}
$sql = "SELECT * FROM user_activity WHERE UserID = '".$userID."' ";
$stm = $db->prepare($sql);
$result = $stm->execute(array());
while($row = $stm->fetch(PDO::FETCH_ASSOC)) {
echo $row['ActivityID'];
}
?>
And this is the button:
echo '<input type="button" class="deleteActivity" value="Activiteit verwijderen" data-activity="' . $row['ActivityID'] . '">';
As you can see, don’t delete anything yet, i only “echo” the ID’s so I can see the ID’s and check if those are the right ones.
I want to get the ActivityID from the “activity” on which I puch the button. With this ActivityID an can finish my query:
$sql = "SELECT * FROM user_activity WHERE UserID = '".$userID."' AND ActivityID = ??? ";
I hope everything is clear and that there is someone that can help me.
Use your posted user_activity_id on your query 😀 I don’t know the column name of your
user_activity_id, so edit it in your case.