我想要做的是将每个插入查询的列设置为$i
的最新值我以前从未在MySQL中使用过触发器,但我相信这是最简单的方法。我愿意接受其他想法和建议。现在我有:
$i = 1;
foreach($_FILES["upload_project_images"]["name"] as $key => $name) {
$p_image_query = "
delimiter |
CREATE TRIGGER update_start_num BEFORE INSERT ON project_images
FOR EACH ROW
BEGIN
UPDATE `project_images` SET `NEW.i_start_num` = '$i' WHERE `i_project_id` = '$prject_id' AND 'i_type' = '2';
END |
delimiter;
INSERT INTO `project_images` (i_name, i_type, i_project_id,i_start_num) VALUES ('$upload_project_images_name', '2', '$project_id','$i');";
$result=mysql_query($p_image_query) or die(mysql_error());
$i++;
}
我的想法是i_start_num
将等于最后$i
,因此我可以在更新查询时选择它。
我的第一个想法就是在另一个之后运行一个查询,但是我尝试了之后无效:
$i = 1;
foreach($_FILES["upload_project_images"]["name"] as $key => $name) {
//insert the file data into the database
$p_image_query = "INSERT INTO `project_images` (i_name, i_type, i_project_id,i_start_num) VALUES ('$upload_project_images_name', '2', '$project_id','$i')"
$result=mysql_query($p_image_query) or die(mysql_error());
$i++;
//update the starting image id number on all project images
$p_update_startnum_qry ="UPDATE `project_images` SET `i_start_num` = '$i' WHERE `i_project_id` = '$prject_id' AND 'i_type' = '2'";
$p_update_startnum_qry_result=mysql_query($p_update_startnum_qry) or die(mysql_error());
}
哪个也失败了。
答案 0 :(得分:1)
嗯,也许可以试试这个:
// pseudo code
INSERT INTO `project_images` (i_name, i_type, i_project_id,i_start_num)
VALUES ('$upload_project_images_name', '2', '$project_id','$i')
ON DUPLICATE KEY UPDATE `project_images`
SET `i_start_num` = '$i'
WHERE `i_project_id` = '$prject_id' AND 'i_type' = '2'