0

I am writing a website and I have this code that normally should insert just one entry but in my case is duplicating the entries.

    require_once 'login.php';
    $conn = new mysqli($hn, $un, $pw, $db);
    if ($conn->connect_error) die("Fatal Error");

    $query  = "INSERT INTO cats(family, name, age) VALUES('Caracal', 'Panou', 4)";
    $result = $conn->query($query);
    if (!$result) {
      die("Database access failed");
    }

Here is the code of login.php

<?php
$hn = 'localhost';
$db = 'publications';
$un = 'sandro';
$pw = 'mypasswd';
?>

I don't use a form. I run the script just by refreshing the page.

Can somebody help to understand what is going on?

Thank you

  • If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. – tadman Feb 29 '20 at 18:29
  • It's worth checking your server logs to see if one request is made, or if two are actually made regardless of your intent. – tadman Feb 29 '20 at 18:29
  • @tadman OP => *"I run the script just by refreshing the page."*. – Funk Forty Niner Feb 29 '20 at 18:32
  • @FunkFortyNiner That doesn't mean the browser isn't doing something funny. Some plugins can make it do things like load it, translate it by loading it again, etc. – tadman Feb 29 '20 at 19:11
  • @tadam Thank you for the tips. I want to focus on core PHP development. After I'm planning to move to frameworks like laravel or codeigniter. – Elom Atsou Agboka Mar 01 '20 at 00:15

0 Answers0