Romano Schoonheim Romano Schoonheim - 9 days ago 4
MySQL Question

PDO - Query executed twice?

My solution

I was debugging my functions on index.php, i didn't notice that my browser was searching for favicon.icon. Currently my router sends the user to index.php if the requested file isn't found, this cause the seccond trigger.

You can easly find this @ you're site acces logs!

*Sorry for my grammer, i hope its readable..

Topic

I have created a little database script, there is only one thing i cannot solve. When i execute a insert query it wil execute twice. The values are diffrent(Value = openssl_random_pseudo_bytes).

private function __init($Query = '', $Params = NULL){
if($this->__getStatus() === false){
$this->__createConnection();
}
$this->__PQuery = $this->__Database->prepare($Query);
foreach($Params as $param => $value){
$dataType = PDO::PARAM_STR;
switch ($value){
case is_int($value):
$dataType = PDO::PARAM_INT;
break;
case is_bool($value):
$dataType = PDO::PARAM_BOOL;
break;
case is_null($value):
$dataType = PDO::PARAM_NULL;
break;
}
$this->__PQuery->bindValue($param, $value, $dataType);
}
$this->__PQuery->execute();
}


Function call:

$this->__database->Query("INSERT INTO `Serials` (`SerialKey`, `Active`, `ActivatedOn`, `ValidTo`, `Paid`) VALUES (:Serial, 'no', NULL, NULL, 'no');", ['Serial' => openssl_random_pseudo_bytes(10)]);


(Query send it to __init)

Can you guys give me any idea's where to look? I'm searching for about 4 hours now.

Backtrace before execution


Core\Database\Handler->__init(INSERT INTO
Serials
(
SerialKey
,
Active
,
ActivatedOn
,
ValidTo
,
Paid
) VALUES (:Serial, 'no', NULL, NULL, 'no');, Array ([Serial] => ��n�� �R)) called at [PATH_TO_PUBLIC_HTMLpublic_html/Core/Database/Handler.php:45]
Core\Database\Handler->Query(INSERT INTO
Serials
(
SerialKey
,
Active
,
ActivatedOn
,
ValidTo
,
Paid
) VALUES (:Serial, 'no', NULL, NULL, 'no');, Array ([Serial] => ��n�� �R)) called at [PATH_TO_PUBLIC_HTMLpublic_html/Core/serialManager/createSerial.php:17]
Core\serialManager\createSerial->__storeToDB() called at [PATH_TO_PUBLIC_HTMLpublic_html/Core/serialManager/createSerial.php:9]
Core\serialManager\createSerial->__construct() called at [PATH_TO_PUBLIC_HTMLpublic_html/Controllers/Website.php:6]
Website->Home()
call_user_func_array(Array ([0] => Website Object (),[1] => Home), Array ()) called at [PATH_TO_PUBLIC_HTMLpublic_html/Core/Routing/Router.php:20]
Core\Routing\Router->__construct() called at [PATH_TO_PUBLIC_HTMLpublic_html/index.php:3]


Backtrace after execution


Core\Database\Handler->__init(INSERT INTO
Serials
(
SerialKey
,
Active
,
ActivatedOn
,
ValidTo
,
Paid
) VALUES (:Serial, 'no', NULL, NULL, 'no');, Array ([Serial] => F�7�,.Kr�)) called at [PATH_TO_PUBLIC_HTMLpublic_html/Core/Database/Handler.php:45]
Core\Database\Handler->Query(INSERT INTO
Serials
(
SerialKey
,
Active
,
ActivatedOn
,
ValidTo
,
Paid
) VALUES (:Serial, 'no', NULL, NULL, 'no');, Array ([Serial] => F�7�,.Kr�)) called at [PATH_TO_PUBLIC_HTMLpublic_html/Core/serialManager/createSerial.php:17]
Core\serialManager\createSerial->__storeToDB() called at [PATH_TO_PUBLIC_HTMLpublic_html/Core/serialManager/createSerial.php:9]
Core\serialManager\createSerial->__construct() called at [PATH_TO_PUBLIC_HTMLpublic_html/Controllers/Website.php:6]
Website->Home()
call_user_func_array(Array ([0] => Website Object (),[1] => Home), Array ()) called at [PATH_TO_PUBLIC_HTMLpublic_html/Core/Routing/Router.php:20]
Core\Routing\Router->__construct() called at [PATH_TO_PUBLIC_HTMLpublic_html/index.php:3]


[NOTE] My hosting company are under attack..

*Sorry for my grammer, did my best..

Answer

Cause of the problem

My browser was looking for favicon.ico, but it didnt exist. If a file/page didnt exist the request is routed to index.php

Mistake

  • Debugging on index.php
  • Missing favicon.ico

What did i learned?

  • Never debug on the homepage, see below why.
  • If there are 2 requests are incomming(when 1 made) Probably the browser sends the second request to look for the favicon.ico (In my case if the page doenst exist return to home)

Solution

1 Don't debug on you're index.php

2 Add the following code to you're html

<link rel="shortcut icon" href="#" />

Sorry For My Broken English

Comments