简体   繁体   English

SQL查询插入错误

[英]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. 当我手动尝试将其插入phpmyadmin并插入一行时,我的查询有效。

However, when I try to load a page with the below code, it seems to hit the database 3 times. 但是,当我尝试使用下面的代码加载页面时,似乎击中数据库3次。 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. 我尝试在查询后立即使用exit()来查看是否存在任何怪异的循环,但仍将其输入数据库3次。 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:281-名称:哟wat 2016-09-03 19:18:09

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

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

id: 284 - Name: yo wat 2016-09-03 19:20:55 id:284-名称: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. 但是,不久之后,又添加了2个查询实例,并将其放入。因此,我之所以看到每个刷新3个输出,是因为获得了2个额外的插入。 (The 2 extra + the 1 correct insert) (额外2个+正确插入1个)

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? 我正在运行mariadb,并且一切看起来正确,但是也许某些东西无法正确关闭?

  • 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). 我不认为这是解决问题的方法,因为我无法直接使其正常运行,但是使用单独的html文件通过post.php提交表单,我可以得到正确的结果(1个条目)。 The reason why the page itself adds 3 entries per refresh is still unknown, but what I am doing now is something satisfactory. 页面本身每次刷新添加3个条目的原因仍然未知,但是我现在正在做的事情令人满意。

I would like to know if someone knows a reason to why this happens, but for now, the immediate problem is solved. 我想知道是否有人知道发生这种情况的原因,但是现在,眼前的问题已经解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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