$j = new DB();
$j->SetSqlCommnd("DELETE FROM cpd_colaborator_access
WHERE col_cod = ".$_POST["colaborator"]."");
$j->Exec();
for($i = 0; $i < count($_POST["page"]); $i++)
{
$j = new DB();
$j->SetSqlCommnd("INSERT INTO cpd_colaborator_access (access_id,col_cod)
VALUES (".$_POST["page"][$i].",".$_POST["colaborator"].")");
$j->Exec();
}
I tried to represent this php code up, on this procedure below, passing an array as parameter “p_pages” and a id “p_colaborator”;
CREATE DEFINER=`root`@`localhost` PROCEDURE `delegar_paginas`(
IN p_colaborator INT,
IN p_pages INT
)
BEGIN
DECLARE A INT(200);
DECLARE I INT(0);
IF (p_colaborator <> '') THEN
DELETE FROM cpd_colaborator_access
WHERE col_cod = p_colaborator;
WHILE (I<@A) DO
IF(p_pages(0,I) <> '')THEN
INSERT INTO cpd_colaborator_acess (acess_id,col_cod)
VALUES (p_pages(0,I),p_colaborator);
ELSE
SET I = A;
END IF;
END WHILE;
END IF;
END;
I am beginner to stored procedures, someone knows what may be wrong here ?
I’m a little confused as to what datatypes p_colaborator and p_pages are supposed to be – you declare them both as INT in the parameter line, but you treat p_colaborator as a string, and p_pages as an array (of strings?), but it’s inserted into an ID field.
MySQL doesn’t support arrays in procedures, but there are lots of work arounds (http://stackoverflow.com/questions/595371/pass-array-into-a-stored-procedure).
So, this procedure makes some assumptions:
With those assumptions, this procedure should do the trick: