This topic is locked
[SOLVED]

  Duplicate entry '1' for key 'PRIMARY'

6/11/2010 9:51:22 PM
PHPRunner General questions
S
staiaccapi author

Hi, i have this problem: <<< Il record non è stato aggiunto >>> Duplicate entry '1' for key 'PRIMARY'

Is not possible add new users!!!!

I have create one table called "anagrafica" with 14 fileds.

the first field is the key primary called "Codice_alunno"

The page in php created with phprunner is ok when i choose in 'advanced security settings' the voice 'Users can see and edit other users data'.

But if i choose the second voice 'Users can see and edit their own data only' the page give me the error "Duplicate entry '1' for key 'PRIMARY'"

Help me thanks

i post the code page!

}

if(!@$_SESSION["UserID"] || !CheckSecurity(@$SESSION["".$strTableName."_OwnerID"],"Add"))

{

$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];

header("Location: login.php?message=expired");

return;

}
$filename="";

$status="";

$message="";

$usermessage="";

$error_happened=false;

$readavalues=false;
$showKeys = array();

$showValues = array();

$showRawValues = array();

$showFields = array();

$showDetailKeys = array();

$IsSaved = false;

$HaveData = true;

if(@$_REQUEST["editType"]=="inline")

$inlineedit=ADD_INLINE;

elseif(@$_REQUEST["editType"]=="onthefly")

$inlineedit=ADD_ONTHEFLY;

else

$inlineedit=ADD_SIMPLE;

$keys=array();

$templatefile = "anagrafica_add.htm";
//connect database

$conn = db_connect();
// Before Process event

if(function_exists("BeforeProcessAdd"))

BeforeProcessAdd($conn);
include('libs/Smarty.class.php');

$smarty = new Smarty();
// insert new record if we have to
if(@$_POST["a"]=="added")

{

$afilename_values=array();

$avalues=array();

$files_move=array();

// processing classe - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_classe");

$type=postvalue("type_classe");

if (in_assoc_array("type_classe",$_POST) || in_assoc_array("value_classe",$_POST) || in_assoc_array("value_classe",$_FILES))

{

$value=prepare_for_db("classe",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["classe"]=$value;

}

}

// processibng classe - end

// processing Cognome - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Cognome");

$type=postvalue("type_Cognome");

if (in_assoc_array("type_Cognome",$_POST) || in_assoc_array("value_Cognome",$_POST) || in_assoc_array("value_Cognome",$_FILES))

{

$value=prepare_for_db("Cognome",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Cognome"]=$value;

}

}

// processibng Cognome - end

// processing Nome - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Nome");

$type=postvalue("type_Nome");

if (in_assoc_array("type_Nome",$_POST) || in_assoc_array("value_Nome",$_POST) || in_assoc_array("value_Nome",$_FILES))

{

$value=prepare_for_db("Nome",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Nome"]=$value;

}

}

// processibng Nome - end

// processing Data_Nascita - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Data_Nascita");

$type=postvalue("type_Data_Nascita");

if (in_assoc_array("type_Data_Nascita",$_POST) || in_assoc_array("value_Data_Nascita",$_POST) || in_assoc_array("value_Data_Nascita",$_FILES))

{

$value=prepare_for_db("Data_Nascita",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Data_Nascita"]=$value;

}

}

// processibng Data_Nascita - end

// processing Indirizzo - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Indirizzo");

$type=postvalue("type_Indirizzo");

if (in_assoc_array("type_Indirizzo",$_POST) || in_assoc_array("value_Indirizzo",$_POST) || in_assoc_array("value_Indirizzo",$_FILES))

{

$value=prepare_for_db("Indirizzo",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Indirizzo"]=$value;

}

}

// processibng Indirizzo - end

// processing Residenza - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Residenza");

$type=postvalue("type_Residenza");

if (in_assoc_array("type_Residenza",$_POST) || in_assoc_array("value_Residenza",$_POST) || in_assoc_array("value_Residenza",$_FILES))

{

$value=prepare_for_db("Residenza",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Residenza"]=$value;

}

}

// processibng Residenza - end

// processing Telefono1 - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Telefono1");

$type=postvalue("type_Telefono1");

if (in_assoc_array("type_Telefono1",$_POST) || in_assoc_array("value_Telefono1",$_POST) || in_assoc_array("value_Telefono1",$_FILES))

{

$value=prepare_for_db("Telefono1",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Telefono1"]=$value;

}

}

// processibng Telefono1 - end

// processing Telefono2 - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Telefono2");

$type=postvalue("type_Telefono2");

if (in_assoc_array("type_Telefono2",$_POST) || in_assoc_array("value_Telefono2",$_POST) || in_assoc_array("value_Telefono2",$_FILES))

{

$value=prepare_for_db("Telefono2",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Telefono2"]=$value;

}

}

// processibng Telefono2 - end

// processing Email - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Email");

$type=postvalue("type_Email");

if (in_assoc_array("type_Email",$_POST) || in_assoc_array("value_Email",$_POST) || in_assoc_array("value_Email",$_FILES))

{

$value=prepare_for_db("Email",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Email"]=$value;

}

}

// processibng Email - end

// processing Foto - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Foto");

$type=postvalue("type_Foto");

if (in_assoc_array("type_Foto",$_POST) || in_assoc_array("value_Foto",$_POST) || in_assoc_array("value_Foto",$_FILES))

{

$value=prepare_for_db("Foto",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Foto"]=$value;

}

}

// processibng Foto - end

// processing Username - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Username");

$type=postvalue("type_Username");

if (in_assoc_array("type_Username",$_POST) || in_assoc_array("value_Username",$_POST) || in_assoc_array("value_Username",$_FILES))

{

$value=prepare_for_db("Username",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Username"]=$value;

}

}

// processibng Username - end

// processing Password - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Password");

$type=postvalue("type_Password");

if (in_assoc_array("type_Password",$_POST) || in_assoc_array("value_Password",$_POST) || in_assoc_array("value_Password",$_FILES))

{

$value=prepare_for_db("Password",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Password"]=$value;

}

}

// processibng Password - end

// processing Provenienza - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Provenienza");

$type=postvalue("type_Provenienza");

if (in_assoc_array("type_Provenienza",$_POST) || in_assoc_array("value_Provenienza",$_POST) || in_assoc_array("value_Provenienza",$_FILES))

{

$value=prepare_for_db("Provenienza",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Provenienza"]=$value;

}

}

// processibng Provenienza - end

// processing Giudizio_media - start

if($inlineedit!=ADD_INLINE)

{

$value = postvalue("value_Giudizio_media");

$type=postvalue("type_Giudizio_media");

if (in_assoc_array("type_Giudizio_media",$_POST) || in_assoc_array("value_Giudizio_media",$_POST) || in_assoc_array("value_Giudizio_media",$_FILES))

{

$value=prepare_for_db("Giudizio_media",$value,$type);

}

else

$value=false;

if(!($value===false))

{
$avalues["Giudizio_media"]=$value;

}

}

// processibng Giudizio_media - end
// insert ownerid value if exists

$avalues["Codice_alunno"]=prepare_for_db("Codice_alunno",$SESSION["".$strTableName."_OwnerID"]);
// insert masterkey value if exists and if not specified

if(@$_SESSION[$strTableName."_mastertable"]=="anagrafica")

{

$avalues["Codice_alunno"]=prepare_for_db("Codice_alunno",$_SESSION[$strTableName."_masterkey1"]);

}
if($inlineedit==ADD_ONTHEFLY)

{

}
// add filenames to values

foreach($afilename_values as $akey=>$value)

$avalues[$akey]=$value;

// make SQL string

$strSQL = "insert into ".AddTableWrappers($strOriginalTableName)." ";

$strFields="(";

$strValues="(";
// before Add event

$retval = true;

if(function_exists("BeforeAdd"))

$retval=BeforeAdd($avalues,$usermessage,$inlineedit);

if($retval)

{

foreach($avalues as $akey=>$value)

{

$strFields.=AddFieldWrappers($akey).", ";

$strValues.=add_db_quotes($akey,$value).", ";

}

if(substr($strFields,-2)==", ")

$strFields=substr($strFields,0,strlen($strFields)-2);

if(substr($strValues,-2)==", ")

$strValues=substr($strValues,0,strlen($strValues)-2);

$strSQL.=$strFields.") values ".$strValues.")";

LogInfo($strSQL);

set_error_handler("add_error_handler");

db_exec($strSQL,$conn);

set_error_handler("error_handler");

// move files

if(!$error_happened)

{

foreach ($files_move as $file)

{

move_uploaded_file($file[0],$file[1]);

if(strtoupper(substr(PHP_OS,0,3))!="WIN")

@chmod($file[1],0777);

}

$message="<div class=message><<< "."Il Record e stato aggiunto"." >>></div>";

if($usermessage!="")

$message = $usermessage;

if($inlineedit==ADD_INLINE || $inlineedit==ADD_ONTHEFLY || function_exists("AfterAdd"))

{
$failed_inline_add = false;

$keys["Codice_alunno"]=mysql_insert_id($conn);

}
// after edit event

if(function_exists("AfterAdd"))

{

foreach($keys as $idx=>$val)

$avalues[$idx]=$val;

AfterAdd($avalues,$keys,$inlineedit);

}

}

}

else

{

$message = $usermessage;

$status="DECLINED";

$readavalues=true;

}

}
$defvalues=array();
// copy record

if(array_key_exists("copyid1",$_REQUEST) || array_key_exists("editid1",$_REQUEST))

{

$copykeys=array();

if(array_key_exists("copyid1",$_REQUEST))

{

$copykeys["Codice_alunno"]=postvalue("copyid1");

}

else

{

$copykeys["Codice_alunno"]=postvalue("editid1");

}

$strWhere=KeyWhere($copykeys);

$strWhere=whereAdd($strWhere,SecuritySQL("Search"));

$strSQL = gSQLWhere($strWhere);
LogInfo($strSQL);

$rs=db_query($strSQL,$conn);

$defvalues=db_fetch_array($rs);

// clear key fields

$defvalues["Codice_alunno"]="";

//call CopyOnLoad event

if(function_exists("CopyOnLoad"))

CopyOnLoad($defvalues,$strWhere);

}

else if(!count($defvalues))

{

}

if($inlineedit==ADD_ONTHEFLY)

{

}

if($readavalues)

{

$defvalues["classe"]=@$avalues["classe"];

$defvalues["Cognome"]=@$avalues["Cognome"];

$defvalues["Nome"]=@$avalues["Nome"];

$defvalues["Data_Nascita"]=@$avalues["Data_Nascita"];

$defvalues["Indirizzo"]=@$avalues["Indirizzo"];

$defvalues["Residenza"]=@$avalues["Residenza"];

$defvalues["Telefono1"]=@$avalues["Telefono1"];

$defvalues["Telefono2"]=@$avalues["Telefono2"];

$defvalues["Email"]=@$avalues["Email"];

$defvalues["Username"]=@$avalues["Username"];

$defvalues["Password"]=@$avalues["Password"];

$defvalues["Provenienza"]=@$avalues["Provenienza"];

$defvalues["Giudizio_media"]=@$avalues["Giudiziomedia"];

}
foreach($defvalues as $key=>$value)

$smarty->assign("value
".GoodFieldName($key),$value);
$linkdata="";

$includes="";

$arr_includes=array();
// include files
// validation stuff

$bodyonload="";

$onsubmit="";

$needvalidate=false;
if($needvalidate)

{

if($inlineedit==ADD_ONTHEFLY)

$onsubmit="return validate_fly(this);";

else

$onsubmit="return validate();";

$bodyonload="onload=\"".$bodyonload."\"";

}
if($inlineedit!=ADD_ONTHEFLY)

{

$includes.="<script language=\"JavaScript\" src=\"include/jquery.js\"></script>\r\n";

$includes.="<script language=\"JavaScript\" src=\"include/onthefly.js\"></script>\r\n";

if ($useAJAX)

$includes.="<script language=\"JavaScript\" src=\"include/ajaxsuggest.js\"></script>\r\n";

$includes.="<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n";

}

if($inlineedit!=ADD_ONTHEFLY)

{

$includes.="<script language=\"JavaScript\">\r\n";

}

$includes.="var locale_dateformat = ".$locale_info["LOCALE_IDATE"].";\r\n".

"var locale_datedelimiter = \"".$locale_info["LOCALE_SDATE"]."\";\r\n".

"var bLoading=false;\r\n".

"var TEXT_PLEASE_SELECT='".addslashes("Prego seleziona")."';\r\n";

if ($useAJAX) {

$includes.="var SUGGEST_TABLE='anagrafica_searchsuggest.php';\r\n";

}

if($inlineedit!=ADD_ONTHEFLY)

{

$includes.="</script>\r\n";

if ($useAJAX)

$includes.="<div id=\"search_suggest\"></div>\r\n";

}

// include datepicker files

if($inlineedit!=ADD_ONTHEFLY)

$includes.="<script language=\"JavaScript\" src=\"include/calendar.js\"></script>\r\n";

else

$arr_includes[]="include/calendar.js";
if($inlineedit!=ADD_ONTHEFLY)

$smarty->assign("includes",$includes);

$smarty->assign("bodyonload",$bodyonload);

if(strlen($onsubmit))

$onsubmit="onSubmit=\"".$onsubmit."\"";

$smarty->assign("onsubmit",$onsubmit);
$smarty->assign("message",$message);

$smarty->assign("status",$status);
$readonlyfields=array();
// show readonly fields
if ($useAJAX)

{

$record_id= postvalue("recordID");

if($inlineedit==ADD_ONTHEFLY)

$record_id= postvalue("id");
if($inlineedit!=ADD_ONTHEFLY)

{

$linkdata = "<script type=\"text/javascript\">\r\n".

"$(document).ready(function(){ \r\n".

$linkdata.

"});</script>";

}

else

{

$linkdata=$includes."\r\n".$linkdata;

$includes="var s;";

foreach($arr_includes as $file)

{

$includes.="s = document.createElement('script');s.src = '".$file."';\r\n".

"document.getElementsByTagName('HEAD')[0].appendChild(s);\r\n";

}

$linkdata=$includes."\r\n".$linkdata;
if(!@$_POST["a"]=="added")

{

$linkdata = str_replace(array("\\","\r","\n"),array("\\\\","\\r","\\n"),$linkdata);

echo $linkdata;

echo "\n";

}

else if(@$_POST["a"]=="added" && ($error_happened || $status=="DECLINED"))

{

echo "<textarea id=\"data\">decli";

echo htmlspecialchars($linkdata);

echo "</textarea>";

}
}

}

else

{

}
if($inlineedit!=ADD_ONTHEFLY)

$smarty->assign("linkdata",$linkdata);
$formname="name=\"editform\"";

if($inlineedit==ADD_ONTHEFLY)

{

$formname="name=\"editform".postvalue("id")."\"";

$smarty->assign("onthefly",true);

$smarty->assign("flytable",postvalue("table"));

$smarty->assign("flyfield",postvalue("field"));

$smarty->assign("flycategory",postvalue("category"));

$smarty->assign("cancelonclick","onclick=\"RemoveFlyDiv('".substr(postvalue("id"),3)."');\"");

$smarty->assign("flyid",postvalue("id"));

}

else

$smarty->assign("onthefly",false);

$smarty->assign("formname",$formname);
if(@$_POST["a"]=="added" && $inlineedit==ADD_ONTHEFLY && !$error_happened && $status!="DECLINED")

{

$LookupSQL="";

if($LookupSQL)

$LookupSQL.=" from ".AddTableWrappers($strOriginalTableName);
$data=0;

if(count($keys) && $LookupSQL)

{

$where=KeyWhere($keys);

$LookupSQL.=" where ".$where;

$rs=db_query($LookupSQL,$conn);

$data=db_fetch_numarray($rs);

}

if(!$data)

{

$data=array(@$avalues[$linkfield],@$avalues[$dispfield]);

}

echo "<textarea id=\"data\">";

echo "added";

print_inline_array($data);

echo "</textarea>";

return;

}
if(function_exists("BeforeShowAdd"))

BeforeShowAdd($smarty,$templatefile);
$smarty->display($templatefile);

function add_error_handler($errno, $errstr, $errfile, $errline)

{

global $readavalues, $message, $status, $inlineedit, $error_happened;

$message="<div class=message><<< "."Il record non è stato aggiunto"." >>>

".$errstr."</div>";

$readavalues=true;

$error_happened=true;

}

?>

A
ann 6/15/2010

Hi,
OwnerID field is filled in automatically in the 'Users can see and edit their own data only' security option.

To add new users create custom view on the Datasource tables tab. Then proceed to the User Group Permissions dialog on the Security tab and assign rights for this view for admin only (uncheck for other user groups). Do not use 'Users can see and edit their own data only' security option and add users on this view only.

S
staiaccapi author 6/15/2010

Great!!!!

Thanks a lot!