douyi7283 2015-05-01 08:58
浏览 22
已采纳

当它们不应该是多个插入的行

I'm working on an assignment from university where I need to create a task management system in PHP (users assign jobs to other users) and I keep getting an annoying bug which I can't quite figure out why it is happening.

First, I tried tracking which functions were being called to see if one of them got called twice and it was inserting the second row. Then I tried inserting a test row into the database without using all the classes who handle it. It still does it, so the problem won't be in the classes.

Here is the code I last used to test (the insert statement is the same as the one from the data mapper I'm using, only the values I've hardcoded for testing purposes):

$sql = "INSERT INTO `jobs` (title, description, estimate, creator, responsible, lastEdit) VALUES ('".uniqid()."', 'some text', 48, 1, 2, NOW());";

// Note: I've already added the needed namespaces at the top of the page

$db = new \PDO('mysql:host=' . Config::get('mysql/host') .
            ';dbname=' . Config::get('mysql/db'),
            Config::get('mysql/username'),
            Config::get('mysql/password')
        );
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

$query = $db->prepare($sql);

$query->setFetchMode(\PDO::FETCH_ASSOC);

$query->execute();
echo '<pre>';
var_dump($query);
echo '</pre>';
die();

This code is inserting 2 rows (titles and ids are different) into the database.

The interesting thing is that the var_dump shows that the query was the first entry. If the page is running multiple times, it should show the second entry or both.

I'm testing at the entry point of the app /public/index.php and there aren't any redirects or refreshes. I'm using a .htaccess document which rewrites the url (I'm using MVC for my site):

Options -MultiViews
RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+) index.php?url=$1 [QSA,L]

Here is the create code for the jobs table:

CREATE TABLE `jobs` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `title` VARCHAR(50) NOT NULL,
    `description` TEXT NOT NULL,
    `status` ENUM('INPROGRESS','FINISHED') NOT NULL DEFAULT 'INPROGRESS',
    `estimate` INT(11) NOT NULL,
    `creator` INT(11) NOT NULL,
    `responsible` INT(11) NOT NULL,
    `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `lastEdit` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (`id`),
    INDEX `job_creator_key` (`creator`),
    INDEX `job_responsible_key` (`responsible`),
    CONSTRAINT `job_creator_key` FOREIGN KEY (`creator`) REFERENCES `users` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
    CONSTRAINT `job_responsible_key` FOREIGN KEY (`responsible`) REFERENCES `users` (`id`) ON UPDATE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
  • 写回答

1条回答 默认 最新

  • ds211107 2015-05-01 10:32
    关注

    Your script is executed twice and I know why:

    your .htaccess is redirecting all missing/404 files to index.php

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^(.+) index.php?url=$1 [QSA,L]
    

    all files like..... favicon.ico

    when you visit your site, browser is automatically trying to download favicon.ico. but its missing so request is redirected to index.php - that's why you are running index.php twice.

    these kinds of bugs can be easily debugged in browser webdeveloper tools like firebug (tab "Network") so you can see all request to website. also you could check web server request log.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题