4 Replies - 358 Views - Last Post: 14 November 2012 - 08:08 AM Rate Topic: -----

#1 Bestford  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-December 11

TASK: delete a record in a mysql database if your id is used in it

Posted 14 November 2012 - 07:26 AM

hello guys,

i want to be able to delete a record on a MySQL database. However the user has to be assigned to that record, I'm currently using a user_id in the table call module, which will link to another table called users using the user ID and if that user is logged in to the system it will only allow them to delete the modules they are linked to.

Can anyone please provide any assistance to this problem, I have the deletion working for the module to deleted I just can't think of a way to limit it to a specific user.

Thanks
Bestford

Is This A Good Question/Topic? 0
  • +

Replies To: TASK: delete a record in a mysql database if your id is used in it

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: TASK: delete a record in a mysql database if your id is used in it

Posted 14 November 2012 - 07:29 AM

Hello there,

First of all make sure they you only show them a list of the records that they can delete. Something simple like SELECT * FROM module WHERE user_id = useridhere is a basic solution.

Then to make sure that nobody meddles with your links and such to delete something that they are not a user of, delete with a check for the user as well. DELETE FROM module WHERE ID = moduleid AND user_id = useridhere.

You obviously need to change the queries for your application and I merely guessed at the ID column for modules.

Edited note: As you provided no code or anything, I assume you have a working application and have a strong understanding of both PHP and MySQL.

This post has been edited by Kruithne: 14 November 2012 - 07:30 AM

Was This Post Helpful? 1
  • +
  • -

#3 Bestford  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-December 11

Re: TASK: delete a record in a mysql database if your id is used in it

Posted 14 November 2012 - 07:54 AM

hello thanks for the quick reply

so yeah basically i want the module to show whos linked to the modules and they have to be logged on that account to see them, in your SQL statements, how would I go about to check the session ID is equal to the id on the database entry of the module that is being deleted

thanks and regards
Bestford
P.S This is currently what I have
<?php

include('core/init.php');
protect_page();
admin_protect();
include('includes/overall/header.php');
?>

<aside>
	<?php
if (logged_in() === TRUE ) {
	include 'includes/widgets/loggedin.php';
} else {
	include 'includes/widgets/login.php';
}
include('includes/widgets/user_count.php');
?>
</aside>


<form action = "deletedModule.php" id = "delete" method = "get">
	<select name = "module_name" id = "module_name">
	<?php
include 'database_conn.php';

$sql = "SELECT module_ID, module_name FROM lr_module";
$queryresult = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_assoc($queryresult)) {
	$module_ID = $row['module_ID'];
	$module_name = $row['module_name'];

	echo "<option value = \"$module_ID\">$module_name</option>\n";//give options to the user thirough a drop down list
}

mysql_free_result($queryresult);//close connection
mysql_close($conn);

?>
<input type="submit" value="Delete Module Details" /><!--submit button-->
</select>
</form>



This post has been edited by Bestford: 14 November 2012 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: TASK: delete a record in a mysql database if your id is used in it

Posted 14 November 2012 - 07:57 AM

Hello again,

I don't see any session on that code, you will need to begin one (first line in your script) with session_start(); and include that on every page you need to use the session on.

When the user logs in for the first time, store their ID in the session with $_SESSION['user'] = $theUserID;.

The first step then would be changing your module listing query to something along the lines of...
$sql = "SELECT module_ID, module_name FROM lr_module WHERE user_id = " . $_SESSION['user'];

Was This Post Helpful? 1
  • +
  • -

#5 Bestford  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 05-December 11

Re: TASK: delete a record in a mysql database if your id is used in it

Posted 14 November 2012 - 08:08 AM

Thanks so much for your help I have managed to fix the problem and works great

thanks again
Bestford

This post has been edited by Bestford: 14 November 2012 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1