Run PHP code once

Asked

Viewed 575 times

0

I want the code to run only once, because every time I refresh it re-enter the sqlite database. Without running 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);
    }

}
  • 1

    You can create a table that only records the import day if that date already exists doesn’t matter. You can set the date field as unique key or no value of the date column can be repeated.

  • 1

    Have you tried using session? so it only executes the code when it closes and opens the browser again

  • session_start();?

  • 1

    That can help you.

3 answers

3

Just before the insertion code check if it was done POST, ie if it was through Submit:

if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') === 'POST')
{
    //código de inserção após clicar no botão
}
  • Right, but I have no Submit butao. Le of the file and insert when the page is loaded

  • Edit your question that I didn’t understand so. You want that code to run once and never run again?

  • Yes because only one file is loaded, and shows.

  • I don’t get it.

  • run the code once.

-1

I don’t understand, if you want the code to run only once, then make sure the page runs only once too

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

if($registro > 0){
// não faz nada
}else{
//aqui vai o código se inserção
}
  • This does not provide an answer to the question. To criticize or ask for clarification from an author, leave a comment below its publication - you can always comment on your own publications and when you have reputation points enough you will be able comment on any publication.

  • I made an Edit in my reply

-3

Make a SELECT before INSERT to know if there is already the record that is being inserted every page update.

  • No doubt you want to help, but a suggestion is to elaborate a little more the answer as by adding an example. Visit [tour] to see how the site works and see https://answall.com/help/how-to-answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.