我正在我的应用程序中创建一个重复的功能,这与以下代码重复整个空缺,在示例中我从空缺ID 94(其中有横幅名称&#34; image.jpg&#34;:<)复制/ p>
$vacdata = $this->vacancies_model->get($data['old_vacancy_id']);
$newvacbannername = return_unique_filename($vacdata->banner);
copy_file($vacdata->banner, $newvacbannername);
$data['newvacancyid'] = $this->vacancies_model->duplicate($data['old_vacancy_id'], $newvacbannername);
在$ newvacbannername中有字符串&#34; image_8.jpg&#34;。这是我复制的新文件名的唯一名称,副本按预期工作。
重复函数如下所示:
function duplicate($vacid, $banner)
{
$this->db->query('CREATE TEMPORARY TABLE tmptable SELECT * FROM vacancies WHERE vacancy_id = ' . $vacid);
$this->db->query('UPDATE tmptable SET vacancy_id = NULL');
$this->db->query('UPDATE tmptable SET banner = ' . $banner );
$this->db->query('UPDATE tmptable SET create_time = now()');
$this->db->query('UPDATE tmptable SET watch_counter = 0');
$this->db->query('UPDATE tmptable SET contact_info_counter = 0');
$this->db->query('UPDATE tmptable SET status = 0');
$this->db->query('UPDATE tmptable SET reminder_mail_sent = 0');
$this->db->query('UPDATE tmptable SET extend_code = NULL');
$this->db->query('INSERT INTO vacancies SELECT * FROM tmptable');
$newvacancyid = $this->db->insert_id();
$this->db->query('DROP TEMPORARY TABLE IF EXISTS tmptable');
return $newvacancyid;
}
我尝试在$ banner上执行var_dump以确保此变量包含正确的字符串,并且它确实包含image_8.jpg。所以它只是从原来的空位复制横幅,但它没有设置新的横幅名称
如果我查看已执行的SQL,我会看到以下结果:
0.0076 CREATE TEMPORARY TABLE tmptable SELECT * FROM vacancies WHERE vacancy_id = 94
0.0090 UPDATE tmptable SET vacancy_id = NULL
0.0000 UPDATE tmptable SET banner = image_8.jpg
0.0005 UPDATE tmptable SET create_time = now()
0.0023 UPDATE tmptable SET watch_counter = 0
0.0001 UPDATE tmptable SET contact_info_counter = 0
0.0002 UPDATE tmptable SET status = 0
0.0002 UPDATE tmptable SET reminder_mail_sent = 0
0.0002 UPDATE tmptable SET extend_code = NULL
0.0153 INSERT INTO空缺SELECT * FROM tmptable
但是,如果我在mysql本身查找重复的新空位,它在update语句中插入的所有行除了$ banner之外,它插入与原始空位中完全相同的名称(本例中为image.jpg)而不是image_8.jpg)。
答案 0 :(得分:0)
为什么不这样做:
INSERT INTO vacancies
SELECT
NULL, --id field
other_columns,
go_here,
NULL,
:banner, --bind this from $banner,
now(),
0,
0,
0,
0,
NULL
FROM vacancies
WHERE vacancy_id = ' . $vacid
看起来像涉及临时表的延伸,以及大量的查询,当你可以简单地在一个中执行此操作时。另外:查看绑定变量到您的查询:它更安全,推荐方法。
或者,您也可以通过交易执行此操作:
START TRANSACTION;
INSERT INTO vacancies
SELECT
*
FROM vacancies
WHERE vacancy_id = :vacid;
UPDATE vacancies
SET banner = :banner,
create_time = now(),
watch_counter = 0,
...
WHERE vacancy_id = :vacid --Get the inserted ID from mysql_insert_id();
COMMIT;