Adding dynamical options to PHP populated Dropdown from SQLServer Database - php

need some input
i know how to populate a php dropdown list from MSSQLServer Datebase and save the selected value in the datebase, but i want to add new dynamical entrys in the dropdown menu and save them into the database. Here is the code:
some help?

I would recommend doing all this in the SQL level, try this
it is modified from a stored procedure I use in a similar fashion
Create Procedure [dbo].[MergeStatus](
#StatusID int,
#Status Varchar(8000)
)
AS
BEGIN
set nocount on;
Merge [Master.dbo.Status] as Target
using (select #StatusID As ID) As source
On
(Target.StatusID = source.ID)
When Matched Then
INSERT INTO Master.dbo.AllgemeineAngaben (StatusID) VALUES (#StatusID ) -- we have a match ID is Greater than 0 so insert into new tabke
When Not Matched Then
Insert (
#Status -- no match add new to Master.dbo.Status
)
Values(
#Status
);
IF #StatusID IS NULL or #StatusID = 0 -- our ID is not waht we expected so we need to insert
BEGIN
SET #Id = CAST(SCOPE_IDENTITY() as [int]);--getting ID od record we just inserted
INSERT INTO Master.dbo.AllgemeineAngaben (StatusID) VALUES (#StatusID )
END
END

Related

Simple Trigger Error?! PL/SQL: ORA-00933:

I am attempting to create a trigger that will insert values into an audit table for approval by an admin user. The trigger will insert new values that are added into a consultant table into this audit table.
I have re-jigged the trigger a lot but cannot seem to bypass the compilation error! I assume it's something small?
DROP TABLE MyAuditTable;
CREATE TABLE MyAuditTable (
audit_id INTEGER NOT NULL,
new_name VARCHAR2 (30),
new_postcode VARCHAR2 (20),
status VARCHAR2 (15),
CONSTRAINT pk_MyAuditTable PRIMARY KEY ( audit_id )
);
DROP sequence MySeq;
Create sequence MySeq MINVALUE 1 MAXVALUE 9999999 INCREMENT BY 1 START WITH 1;
drop trigger MyTrigger;
create trigger MyTrigger
after insert on my_consultant_table
for each row
begin
insert into MyAuditTable values (
MySeq.nextval, :new.con_name,
:new.con_postcode,
'Pending'
)
from my_consultant_table;
end;
/
ERROR: PL/SQL: ORA-00933:
So the audit table should now have the newly inputted data from the consultant table, which contains name and postcode attributes. Another trigger will fire so that when the status is changed, these changes are permitted.
Thank You!
The line
from my_consultant_table
Is unnecessary.
Should be:
insert into MyAuditTable values (MySeq.nextval, :new.con_name, :new.con_postcode, 'Pending');
This ORA-00933 denotes a syntax error in your trigger declaration :
begin
insert into MyAuditTable values (
MySeq.nextval, :new.con_name,
:new.con_postcode,
'Pending'
)
from my_consultant_table;
end;
The trailing from my_consultant_table does not make sense, just remove it and you should be fine :
begin
insert into MyAuditTable values (
MySeq.nextval, :new.con_name,
:new.con_postcode,
'Pending'
);
end;
Try this:
begin
insert into MyAuditTable
select
MySeq.nextval,
:new.con_name,
:new.con_postcode,
'Pending'
from dual;
end;
One Tip: When u create a sequence and always use from 1 to 9999999.... anc increment one by one.
Just use
create sequence owner.table nocache;

MySQL Trigger that writes to multiples tables

I have a trigger that I cannot get to work properly.
I have at table called _WorkOrderDump where data is input via a form. There is a trigger on this table that inserts some of the data into a table called _RefTable. The second part of this trigger is to insert some of the data into another table called __XClient.
I cannot get the second insert into the __XClient table to work, it does not insert any data into this table. The first part of the trigger inserting into Ref table is however working perfectly.
What is wrong with my query?
Thanks for any help provided!
CREATE TRIGGER WorkOrderTrigger
AFTER INSERT on _WorkOrderDump
BEGIN
Declare EntryDate DATETIME;
Declare RefIDDump INT;
SET EntryDate = CURDATE();
Set RefIDDump = LAST_INSERT_ID();
CASE WHEN (EntryDate) > 0
THEN INSERT INTO `_RefTable`( `WOID`, `ClientID`, `ProjectScope`, `ProjectID`, `ClientRef`, `SiteID`, `SiteName`, `RegionID`, `SiteAddressLine`, `SiteAddressSuburb`, `SiteAddressState`, `SiteAddressPostcode`, `SiteAddressCountry`, `SiteContactName`, `SiteContactMobile`, `SiteContactPhone`, `SiteContactEmail`, `RefEntryDate`, `CreatedBY`)
VALUES
( new.WOID, new.ClientID, new.ProjectScope, new.ProjectID, new.ClientRef, new.SiteID, new.SiteName, new.RegionID, new.SiteAddressLine, new.SiteAddressSuburb, new.SiteAddressState, new.SiteAddressPostcode, new.SiteAddressCountry, new.SiteContactName, new.SiteContactMobile, new.SiteContactPhone, new.SiteContactEmail, EntryDate, new.CreatedBY);
WHEN (new.ClientID) = 1
THEN INSERT INTO `__XClient`( `RefID`, `ITContactName`, `ITContactPhone`, `ITContactEmail`, `XScope32`, `XScope43`, `XScopeMediaPlayer`, `XScopeInstallDate`, `XScopeInstallTime`, `XScopeComments`)
VALUES
( RefIDDump, new.Dump_1, new.Dump_3, new.Dump_2, new.Dump_4, new.Dump_5, new.Dump_6, new.Dump_7, new.Dump_8, new.Dump_9);
END CASE;
END

Use inserted id for another column value

I have an identity column (id) that auto-increments.
id|name|image_path
I want to know if there is some way using mysql, to use the newly inserted id in the image_path.
For example if a new row is inserted and got the id 2 I want the image_path to be "/images/2.png".
Or do I have to use the traditional way, by inserting and then fetching this ID then updating the entry?
My opinion is that it is impossible to do with one query. You won't know new autoincrement value until row will be inserted. Still you can write 1 query to achieve what you want (actually 2 queries would be executed):
insert into `t`(`id`, `name`, `image_path`)
values(
(SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES
WHERE `table_name` = 't'),
'1234',
concat(
'/images/',
(SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES
WHERE `table_name` = 't'),
'.png'
)
)
Anyway much safer would be:
START TRANSACTION;
set #c = (select ifnull(max(`id`),0) + 1 from `t`);
insert into `t`(`id`, `name`, `image_path`) values (#c,'123',concat('/images/',#c,'.png'));
COMMIT;
Yes, it is possible with oracle. We have dynamic sql feature.
have tried the below.
Created a sequence and then created a procedure which takes id as input and creates an insert statement dynamically which will fulfill your requirement.
create sequence seq1 start with 1;
create table image1(id1 number,image varchar2(50));
create or replace procedure image1_insert(id1 in number)
as
sql_stmt varchar2(50);
image_path varchar2(50);
begin
sql_stmt:='insert into image1 values(:1,:2)';
image_path:='/image/'||id1||'.png';
execute immediate sql_stmt using id1,image_path;
end;
begin
image1_insert(seq1.nextval);
end;
id image
4 /image/4.png
5 /image/5.png
select *from image1;

Filter comma separated values from same table

Can anybody help me to get the sub_cat(csv) values which are not in id column from same table.
(sub_cat) comma separated values are the ids of same table, i need to get the values which are not in id column. Like 2,3,7 are present in id column whereas 20,24 are not. I need to get 20,24 only.
As I have elaborated in this post, I recommend not storing data in CSV format.
This gives trouble accessing and updating it.
I am not sure about this, but you might be able to simply use:
SELECT sub_cat FROM table_name WHERE id NOT IN (
SELECT sub_cat FROM table_name
)
However, I always prefer to store only one ID per row.
To do this purely in MySQL required writing a stored procedure; you'll need to change the database name test to the actual name of your database. It would have been a whole lot easier to do it with PHP - but where's the fun in that?
DELIMITER //
CREATE PROCEDURE test.check_subcats(
IN s_delimiter VARCHAR(30)
)
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE s_csv TEXT;
DECLARE i_subcat_index INT(10) unsigned DEFAULT 1;
DECLARE i_subcat_count INT(10) unsigned;
DECLARE l_category_done INT(10) DEFAULT FALSE;
DECLARE c_category CURSOR FOR SELECT category.sub_cat FROM category;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET l_category_done = TRUE;
-- create a temporary table to hold every csv value
CREATE TEMPORARY TABLE IF NOT EXISTS tmp_csv( cvalue VARCHAR(10) NOT NULL );
OPEN c_category;
l_category: LOOP
FETCH c_category INTO s_csv;
IF l_category_done THEN
LEAVE l_category;
ELSE
-- determine the number of sub-categories
SELECT (LENGTH(s_csv) - LENGTH(REPLACE(s_csv, s_delimiter, ''))) + 1 INTO i_subcat_count;
-- loop to store all csv values
WHILE i_subcat_index <= i_subcat_count DO
INSERT INTO tmp_csv ( cvalue ) (
SELECT REPLACE(SUBSTRING(
SUBSTRING_INDEX(s_csv, s_delimiter, i_subcat_index),
LENGTH(SUBSTRING_INDEX(s_csv, s_delimiter, i_subcat_index - 1)) + 1
), s_delimiter, '')
);
SET i_subcat_index = i_subcat_index + 1;
END WHILE;
END IF;
SET i_subcat_index = 1;
END LOOP;
CLOSE c_category;
SELECT DISTINCT tmp_csv.cvalue FROM tmp_csv WHERE tmp_csv.cvalue NOT IN ( SELECT category.id FROM category );
DROP TEMPORARY TABLE IF EXISTS tmp_csv;
END //
DELIMITER ;
I'm not 100% certain as to how robust it is but it was working with your data on my dev box.
You specify the delimiter for your CSV data when you call the procedure thus:
CALL `check_subcats`(',');
Essentially this loops through the category table to read sub_cat. It then splits the sub_cat value into chunks using the delimiter provided (much like PHPs explode() function) and writes every one of those values to a temporary table.
This then gives you a temporary table holding all your CSV data in individual bits and it's then a simple matter of selecting everything from that data that's NOT IN the category.id list.

How to Parse PHP Searilize data in Mysql Stored Procedure & Looping it

I have below data in Mysql Column (storing all data in serialize form - with comma separated into column) and i want to get/fetch this column data in Mysql stored procedure and want to loop for each data and insert into another trans table.
So if my data like below then i want to insert 7 record in trans table.
{"FormBuilderId":"5","vAnswer":"Develeop"},
{"FormBuilderId":"15","vAnswer":"Search Engine"},
{"FormBuilderId":"13","vAnswer":"10-15"},
{"FormBuilderId":"6","vAnswer":"Tester entered"},
{"FormBuilderId":"1","vAnswer":"Female"},
{"FormBuilderId":"14","vAnswer":"Moon.jpg"},
{"FormBuilderId":"12","vAnswer":"TV,dancing and modeling"}
My table structure & data is like below in table:
CREATE TABLE IF NOT EXISTS `idea_history` (
`iIdeaHistoryId` int(11) NOT NULL AUTO_INCREMENT,
`iApplicationId` tinyint(11) unsigned DEFAULT '0',
`tAnswerData` text COMMENT 'all answer data store here in json format',
`dAddedDate` datetime NOT NULL,
PRIMARY KEY (`iIdeaHistoryId`),
KEY `iApplicationId` (`iApplicationId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='history comes here' AUTO_INCREMENT=57 ;
--
-- Dumping data for table `idea_history`
--
INSERT INTO idea_history (
iIdeaHistoryId,
iApplicationId,
tAnswerData,
dAddedDate
)
VALUES
(
53,
2,
'{"FormBuilderId" : "2","vAnswer":"Environmental Group"},{"FormBuilderId" : "11","vAnswer":"Satelite"},{"FormBuilderId" : "3","vAnswer":"HB"},{"FormBuilderId" : "4","vAnswer":"Dev"},{"FormBuilderId" : "7","vAnswer":"HB"},{"FormBuilderId" : "8","vAnswer":"Balaji Satellite"},{"FormBuilderId" : "10","vAnswer":""}',
'2014-07-05 19:20:56'
),
(
54,
1,
'{"FormBuilderId":"5","vAnswer":"Hello krishna|kanth double"},{"FormBuilderId":"15","vAnswer":"Website"},{"FormBuilderId":"6","vAnswer":"need to check"},{"FormBuilderId":"13","vAnswer":"20-25"}',
'2014-07-05 19:20:56'
),
(
55,
2,
'{"FormBuilderId":"11","vAnswer":"comapnay"},{"FormBuilderId":"8","vAnswer":"here am|chw "},{"FormBuilderId" : "10","vAnswer":""},{"FormBuilderId":"9","vAnswer":"Business"}',
'2014-07-05 19:20:56'
) ;
I will pass iIdeaHistoryId in stored procedure and it will fetch value of tAnswerData field and part this value and insert into another trans table.
Could you please guide what i have to change in stored procedure ?
you can do it like this
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'{"',-1),'":"',1) INTO arg_1;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-2),'","',1) INTO arg_2;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'","',-1),'":"',1) INTO arg_3;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-1),'"}',1) INTO arg_4;
INSERT INTO trans VALUES (arg_1,arg_2,arg_3,arg_4);
or if you want to do it in store procedure add your data here
DELIMITER $$
DROP PROCEDURE IF EXISTS trans$$
CREATE PROCEDURE trans()
BEGIN
DECLARE arg_1,arg_2,arg_3,arg_4 VARCHAR(100);
DECLARE finished INTEGER DEFAULT 0;
DECLARE cursor_name CURSOR FOR YOUR_SELECT_statement;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN cursor_name;
my_loop: LOOP
FETCH cursor_name INTO your_variable;
IF finished = 1 THEN
LEAVE my_loop;
END IF;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'{"',-1),'":"',1)
INTO arg_1 FROM your_table;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-2),'","',1)
INTO arg_2 FROM your_table;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'","',-1),'":"',1)
INTO arg_3 FROM your_table;
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(your_variable,'":"',-1),'"}',1)
INTO arg_4 FROM your_table;
INSERT INTO trans VALUES (arg_1,arg_2,arg_3,arg_4);
END LOOP my_loop;
CLOSE cursor_name;
END$$
DELIMITER ;
for one column you use this
DELIMITER $$
DROP PROCEDURE IF EXISTS trans$$
CREATE PROCEDURE trans(IN in_iIdeaHistoryId INT)
BEGIN
DECLARE arg_1 VARCHAR(100);
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(`tAnswerData`,' ',''),'{"',-1),'":"',1)
INTO arg_1 FROM `idea_history` WHERE iIdeaHistoryId=in_iIdeaHistoryId;
INSERT INTO trans VALUES (arg_1);
END$$
DELIMITER ;
and call it like this
CALL `trans`(53);
This looks like JSON. Use php function json_decode() to store the json data into an array and then you can use foreach loop on that array. Inside the loop you can place condition and when its met, you could use insert query to store it in your database.
Does this help you?
If you can explain a bit further what you're trying to do exactly and what are your condition, then i Can probably give you a better answer.

Categories