Run PHP code once [closed]

0

I want the code to be executed only once, because everytime I refresh it, it returns to the sqlite database. Not run from a button. code:

if(!file_exists($dir.$base)) {  
$base_hndl  =   new SQLite3($dir.$base);
$requete = "CREATE TABLE 'contact' (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
nom TEXT NULL,
prenom TEXT NULL,
categorie TEXT NULL
)";             
$result = $base_hndl->exec($requete);} else {
$row = 0;
$delemiter = ";" ;

$data = array();

    if (($handle = fopen("contacts.csv", "r")) !== FALSE) 
    {
        while (($data = fgetcsv($handle, 2000, $delemiter)) !== FALSE)
        { 
            $num = count($data);
            $row++;
            $base_hndl  =   new SQLite3($dir.$base);
            $query_value = "";
            $query = "INSERT INTO contact (nom,prenom,categorie) VALUES ( ";

            for ($c = 0; $c < $num; $c++) 
            {

                if($data[$c] == "")
                { 
                    $query_value .= "\"0\"";
                }
                else
                {
                    //$query .= "'" . $data[$c] . "'";
                    $query_value .= "\"$data[$c]\"";
                }
                if ($c+1 < $num) 
                {
                   $query_value .= ",";
                }
            }
            $query_value .= ")\n";

            $result = $base_hndl->exec($query.$query_value);    
        }
        fclose($handle);
    }

}
    
asked by anonymous 12.11.2014 / 10:52

3 answers

2

Just before the insertion code, you can check if POST was done, that is, if it was through submit :

if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') === 'POST')
{
    //código de inserção após clicar no botão
}
    
12.11.2014 / 10:56
-1

I do not understand if you want the code to be executed only once, so make sure that the page is executed once, too

@Edit
Make a select and then if to check if the record already exists, if it already exists it does not insert, if it does not exist it inserts.

if($registro > 0){
// não faz nada
}else{
//aqui vai o código se inserção
}
    
12.11.2014 / 13:21
-3

Make a SELECT before the INSERT to see if the record that is being inserted with each page refresh already exists.

    
20.10.2018 / 19:36