简体   繁体   中英

SQL Query insert error

So I'm currently trying to establish a database connection to my server and my insertion is having some problems. My query works when I manually try to plug it into phpmyadmin and a single row gets inserted.

However, when I try to load a page with the below code, it seems to hit the database 3 times. I tried using exit() right after my query to see if there was any weird loops, but it still got entered 3 times into the database. Is there anything that seems weird here?

//timestamp
$t = time();
$stamp = date("Y-m-d", $t);


//create connection
$conn = new mysqli($servername, $username, $password, $dbname);

//check connection
if($conn->connect_error){
  die("connection failed: ".$conn->connect_error);
}
echo "Connection Successful"."<br>";

$name = "hi";
$affiliation = "yo";
$note = "wat";

$sql = "INSERT INTO `$table` (`name`, `affiliation`, `notes`, `timestamp`) VALUES ('$name', '$affiliation', '$note', '$stamp')";

$result = $conn->query($sql);

if($result){
  echo "Data entered successfully";
  echo "<br>".$sql;
} else{
  echo "Error: " .$sql."<br>".$conn->error;
}


$conn->close();

MORE INFO*****

So here is a sample output for the whole table during one session

id: 281 - Name: yo wat 2016-09-03 19:18:09

id: 282 - Name: yo wat 2016-09-03 19:18:11

id: 283 - Name: yo wat 2016-09-03 19:18:11

id: 284 - Name: yo wat 2016-09-03 19:20:55

As you can tell by the timestamp, one gets added in one session and it is correct. However, shortly after that one is added 2 more instances of the query are run and put in. So the reason I was seeing 3 outputs per refresh is because of the 2 extra inserts that get through. (The 2 extra + the 1 correct insert)

I'm mystified as to why it does this, because it seems to be a server-related issue. I'm running mariadb and things look correct, but maybe something isn't closing correctly?

  • I don't think this is a fix to the problem since I wasn't able to directly get it working, but using a separate html file to submit a form with post.php I was able to get correct results (1 entry). The reason why the page itself adds 3 entries per refresh is still unknown, but what I am doing now is something satisfactory.

I would like to know if someone knows a reason to why this happens, but for now, the immediate problem is solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM