DELIMITER $$
DROP FUNCTION IF EXISTS `sead`.`php__seadgen__insert`$$
CREATE FUNCTION `sead`.`php__seadgen__insert`(in_schema VARCHAR(64), in_table VARCHAR(64))
RETURNS TEXT
READS SQL DATA
BEGIN
DECLARE currentcolumn varchar(64);
DECLARE currenttype varchar(64);
DECLARE instatement text;
DECLARE setstatement text;
DECLARE outputstatement text;
DECLARE querystatement text;
DECLARE anymatches tinyint(1);
DECLARE errorcount INT;
DECLARE lastrow tinyint(1);
DECLARE rowcount INT;
DECLARE cursortables CURSOR FOR
SELECT column_name, column_type
FROM information_schema.columns
WHERE table_schema = in_schema and table_name = in_table;
DECLARE CONTINUE HANDLER FOR 1329 SET lastrow=1;
insertfieldgetsprocedures:BEGIN
SET errorcount = 0;
IF in_schema is NULL or
in_schema <= " "
THEN
SET errorcount = errorcount + 1;
ELSE
SET anymatches = 0;
SELECT count(*) from information_schema.schemata
WHERE schema_name=in_schema
INTO anymatches;
IF anymatches = 0
THEN
SET errorcount = errorcount + 1;
END IF;
END IF;
IF in_table is NULL or
in_table <= " " or
errorcount > 0
THEN
SET errorcount = errorcount + 1;
ELSE
SET anymatches = 0;
SELECT count(*) from information_schema.tables
WHERE table_schema=in_schema AND table_name=in_table
INTO anymatches;
IF anymatches = 0
THEN
SET errorcount = errorcount + 1;
END IF;
END IF;
IF errorcount > 0 THEN
LEAVE insertfieldgetsprocedures;
END IF;
IF anymatches > 0 THEN
SET lastrow = 0;
SET instatement = " ";
SET setstatement = " ";
SET rowcount = 0;
OPEN cursortables;
preparestatementloop: LOOP
FETCH cursortables INTO currentcolumn, currenttype;
IF lastrow = 1 THEN
leave preparestatementloop;
END IF;
IF currentcolumn NOT IN ("id","inserted","inserted_by","updated","updated_by","updated_reason") THEN
SET rowcount = rowcount + 1;
SET setstatement = concat_ws("",
setstatement,
"
<?=get_fields('",in_table,"','",currentcolumn,"','",rowcount,"',NULL);?>
"
);
SET instatement=CONCAT_WS("",instatement,"\n",
"$checked['",currentcolumn,"']=word_and_number_check('",currentcolumn,"');");
IF rowcount = 1 THEN
SET querystatement = concat_ws("","'.(empty($checked['",currentcolumn,"'])?'NULL':'\"'.$checked['",currentcolumn,"'].'\"').'");
ELSE
SET querystatement = concat_ws("",querystatement,",'.(empty($checked['",currentcolumn,"'])?'NULL':'\"'.$checked['",currentcolumn,"'].'\"').'");
END IF;
END IF;
END LOOP preparestatementloop;
close cursortables;
SET setstatement = concat_ws("",setstatement,"\n");
LEAVE insertfieldgetsprocedures;
ELSE
SET errorcount = errorcount + 1;
LEAVE insertfieldgetsprocedures;
END IF;
END;
SET outputstatement = concat_ws("","<?
",instatement,"
include_once($_SERVER['DOCUMENT_ROOT'].'inc/inc_navigation_links.php');
?>
<div id=\"cbs\">
<?
if(empty($checked['inserted_reason'])&&empty($_POST['inserted_button'])){
echo ' <h2>Insert a ",in_table,"</h2>
';
$showform=TRUE;
}else{
if(empty($checked['inserted_reason'])&&!empty($_POST['inserted_button'])){
$showform=TRUE;
?>
<h2>Required fields are missing</h2>
<div class=\"alert center\">
<p class=\"center\">The following fields are emtpy and required:</p>
<ul class=\"nonli\">
<?=(empty($checked['inserted_reason'])?'<li>The Insert Comment</li>':'');?>
</ul>
</div>
<?
}else{
if ($result=$mysqli->query('call `sejamp`,`",in_table,"_insert`(",querystatement,")')) {
while ($row = $result->fetch_assoc()) {
$id=$row['last_insert_id'];
if($id==FALSE){
echo '<h2 class=\"alert\">Failure! Something wrong with the ID</h2>';
$showform=TRUE;
}else{
echo '<h2>Success!</h2><p>The ",in_table," data that was transfered has been a success.</p>';
$showform=FALSE;
//header('Location: /transfer?pg=",in_table,"&id='.$id.'&procedure=insert');
//header('Location: /sead/",in_table,"-get/id-'.$id.'/');
}
}
$result->close();
}else{
echo '<h2 class=\"alert\">Failure!</h2>';
echo '<p>The ",in_table," data has <strong>not</strong> been entered.</p>';
echo '<p>The reason is: '.$error.'</p>';
$showform=TRUE;
}
}
}
if($showform==TRUE){
?>
<form action=\"<?=$_SERVER['REQUEST_URI']?>\" method=\"post\" id=\"qrs\">
<fieldset>
",setstatement,"
</fieldset>
<?=make_input('submit','Insert','inserted_button','inserted_button','Insert '.ucwords($c['currentpage']),'accesskey=\"i\"',TRUE);?>
</form>
</div>
<?
}
?>
");
RETURN(outputstatement);
END$$
DELIMITER ;