PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 31, 2022

[FIXED] Phpmyadmin Function Delete row if exists in other table

 January 31, 2022     mysql, phpmyadmin     No comments   

Issue

Need help on creating MySQL Function in phpmyadmin So I have 2 tables:

-Customer_Table CustomerID|CustomerName

-Project_Table ProjectID|CustomerID

My aim is to create a function called delete_customer(parameter_CustomerID). It'll take the CustomerID passed in the input parameter, then it checks whether this Customer ID exists in Project Table. If it is, then

return 0

Otherwise,

DELETE FROM Customer_Table WHERE CustomerID=parameter_CustomerID

I've tried to search the tutorial on how to create routine-function in phpmyadmin especially with if-else function, but turned out no luck.

Thank you in advance.


Solution

Hello Please check following Mysql Function:

DROP Function IF EXISTS delete_customer;

Delimiter //
CREATE FUNCTION delete_customer(customer_id INT(11))

RETURNS INT(1) 

BEGIN

DECLARE NAME_FOUND INT DEFAULT 0;
DECLARE USER_ACCEPT INT DEFAULT 0;

SELECT COUNT(iCustomerId) INTO NAME_FOUND FROM Customer_Table WHERE iCustomerId = customer_id;
IF NAME_FOUND > 0 
    THEN
    DELETE FROM Customer_Table WHERE iCustomerId = customer_id;
    SET USER_ACCEPT = 1;

ELSE 
     SET USER_ACCEPT = 0;

    END IF;
 RETURN USER_ACCEPT;
END//

Delimiter ;



Answered By - Jyoti Sharma
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing