我的PHP代码:
for($i=0;$i<num_rows($sql);$i++)
if(isset($_POST['knight'.$i]))
{
$data=
[
$pm_id=$_POST['pm_id'.$i],
$knight_id=$_POST['knight'.$i],
];
mysql_query("update `project_waiting` set `chosen`=(b'1') where `pm_id`='$pm_id' and `knight_id`='$knight_id';");
mysql_query("insert into `project_working`(`pm_id`,`knight_id`) values('$pm_id','$knight_id');");
}
当我使用echo而不是mysql_query时,它返回:
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K1';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K1');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K2';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K2');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K3';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K3');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_1' and `knight_id`='K4';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K4');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_2' and `knight_id`='K1';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_2','K1');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_2' and `knight_id`='K4';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_2','K4');
update `project_waiting` set `chosen`=(b'1') where `pm_id`='P1_3' and `knight_id`='K2';
insert into `project_working`(`pm_id`,`knight_id`) values('P1_3','K2');
它在我的phpmyadmin上完美运行,但是当我将echo更改为mysql_query时它只影响5行(两个表)
P1_1-K1
P1_1-K4
P1_1-K3
P1_1-K2
P1_2-K1
我的完整数据库
-- phpMyAdmin SQL Dump
- 版本4.3.11
- 主持人:127.0.0.1 - 世代时间:2015年7月23日下午12:16 - 服务器版本:5.6.24 - PHP版本:5.6.8
SET SQL_MODE =&#34; NO_AUTO_VALUE_ON_ZERO&#34 ;; SET time_zone =&#34; +00:00&#34 ;;
-
knightit
创建数据库,如果不是EXISTS knightit
默认字符集utf8 COLLATE utf8_unicode_ci;
使用knightit
;
-
admin
DROP TABLE IF EXISTS admin
;
CREATE TABLE IF NOT NOT EXISTS admin
(
id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
password
二进制(60)NOT NULL,
level
int(2)NOT NULL
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
employers
DROP TABLE IF EXISTS employers
;
CREATE TABLE IF NOT NOT EXISTS employers
(
emp_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL DEFAULT&#39;&#39;,
password
longtext COLLATE utf8_unicode_ci NOT NULL,
email
varchar(32)COLLATE utf8_unicode_ci NOT NULL,
emp_name
text COLLATE utf8_unicode_ci,
coins
bigint(20)NOT NULL DEFAULT&#39; 0&#39;
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
knights
DROP TABLE IF EXISTS knights
;
CREATE TABLE IF NOT NOT EXISTS knights
(
knight_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
password
longtext COLLATE utf8_unicode_ci NOT NULL,
knight_name
text COLLATE utf8_unicode_ci,
email
varchar(32)COLLATE utf8_unicode_ci NOT NULL,
s_id
varchar(16)COLLATE utf8_unicode_ci DEFAULT NULL,
points
int(11)DEFAULT&#39; 0&#39;,
coins
int(32)NOT NULL DEFAULT&#39; 0&#39;,
status
位(2)默认b&#39; 1&#39;,
bio
longtext COLLATE utf8_unicode_ci
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
message
DROP TABLE IF EXISTS message
;
CREATE TABLE IF NOT NOT EXISTS message
(
m_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
content
text COLLATE utf8_unicode_ci NOT NULL,
send_date
日期非空
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
message_send
DROP TABLE IF EXISTS message_send
;
CREATE TABLE IF NOT NOT EXISTS message_send
(
m_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
receiver_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
status
位(1)NOT NULL默认b&#39; 0&#39;
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
projects
DROP TABLE IF EXISTS projects
;
CREATE TABLE IF NOT NOT EXISTS projects
(
pro_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
pro_name
text COLLATE utf8_unicode_ci NOT NULL,
emp_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
p_describe
text COLLATE utf8_unicode_ci NOT NULL,
price
int(32)NOT NULL,
cut
int(32)NOT NULL,
s_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
status
位(2)NOT NULL默认b&#39; 1&#39;
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
project_milestone
DROP TABLE IF EXISTS project_milestone
;
CREATE TABLE IF NOT NOT EXISTS project_milestone
(
pm_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
pro_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
milestone
int(2)NOT NULL,
job
text COLLATE utf8_unicode_ci,
deadline
int(3)DEFAULT NULL,
knight_amt
int(2)DEFAULT NULL
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
project_waiting
DROP TABLE IF EXISTS project_waiting
;
CREATE TABLE IF NOT NOT EXISTS project_waiting
(
pm_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
knight_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
chosen
位(1)NOT NULL默认b&#39; 0&#39;
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
project_working
DROP TABLE IF EXISTS project_working
;
CREATE TABLE IF NOT NOT EXISTS project_working
(
pm_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
knight_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
submit
日期DEFAULT NULL,
approve
tinyint(1)DEFAULT NULL,
paid
tinyint(1)DEFAULT NULL,
coins_paid
int(32)DEFAULT NULL
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
security
DROP TABLE IF EXISTS security
;
CREATE TABLE IF NOT NOT EXISTS security
(
name
varchar(100)COLLATE utf8_unicode_ci NOT NULL,
value
longtext COLLATE utf8_unicode_ci NOT NULL
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
skills
DROP TABLE IF EXISTS skills
;
CREATE TABLE IF NOT NOT EXISTS skills
(
s_id
varchar(16)COLLATE utf8_unicode_ci NOT NULL,
skill_name
text COLLATE utf8_unicode_ci NOT NULL
)ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
-
-
admin
ALTER TABLE admin
添加PRIMARY KEY(id
);
-
employers
ALTER TABLE employers
添加PRIMARY KEY(emp_id
);
-
knights
ALTER TABLE knights
添加主要密钥(knight_id
),添加密钥s_id
(s_id
);
-
message
ALTER TABLE message
添加PRIMARY KEY(m_id
);
-
message_send
ALTER TABLE message_send
添加密钥m_id
(m_id
);
-
projects
ALTER TABLE projects
添加主要密钥(pro_id
),添加密钥emp_id
(emp_id
),添加密钥s_id
(s_id
);
-
project_milestone
ALTER TABLE project_milestone
添加主要密钥(pm_id
),添加密钥pm_id
(pm_id
,pro_id
,milestone
),添加密钥project_milestone_ibfk_1
({{1} });
-
pro_id
ALTER TABLE project_waiting
添加主要密钥(project_waiting
,knight_id
),添加密钥pm_id
(knight_id
),添加密钥knight_id
(pm_id
);
-
pm_id
ALTER TABLE project_working
添加主要密钥(project_working
,pm_id
),添加密钥knight_id
(pm_id
,pm_id
),添加密钥knight_id
({{1} });
-
knight_id
ALTER TABLE knight_id
添加PRIMARY KEY(security
);
-
security
ALTER TABLE name
添加PRIMARY KEY(skills
);
-
-
skills
ALTER TABLE s_id
添加约束knights
FOREIGN KEY(knights
)引用knights_ibfk_1
(s_id
)ON DELETE CASCADE ON UPDATE CASCADE;
-
skills
ALTER TABLE s_id
添加约束message_send
FOREIGN KEY(message_send
)引用message_send_ibfk_1
(m_id
)ON DELETE CASCADE ON UPDATE CASCADE;
-
message
ALTER TABLE m_id
添加约束projects
FOREIGN KEY(projects
)引用projects_ibfk_1
(emp_id
)ON DELETE CASCADE ON UPDATE CASCADE,
添加约束employers
FOREIGN KEY(emp_id
)引用projects_ibfk_2
(s_id
)ON DELETE CASCADE ON UPDATE CASCADE;
-
skills
ALTER TABLE s_id
添加约束project_milestone
外键(project_milestone
)参考project_milestone_ibfk_1
(pro_id
);
-
projects
ALTER TABLE pro_id
添加约束project_waiting
FOREIGN KEY(project_waiting
)引用project_waiting_ibfk_1
(pm_id
)ON DELETE CASCADE ON UPDATE CASCADE,
添加约束project_milestone
FOREIGN KEY(pm_id
)引用project_waiting_ibfk_2
(knight_id
)ON DELETE CASCADE ON UPDATE CASCADE;
-
knights
ALTER TABLE knight_id
添加约束project_working
FOREIGN KEY(project_working
)引用project_working_ibfk_1
(pm_id
)ON DELETE CASCADE ON UPDATE CASCADE,
添加约束project_milestone
FOREIGN KEY(pm_id
)引用project_working_ibfk_2
(knight_id
)ON DELETE CASCADE ON UPDATE CASCADE;
答案 0 :(得分:0)
我已将所有问题合并到一个查询中解决了问题:
insert into `project_working`(`pm_id`,`knight_id`) values('P1_1','K1'),('valueX','valueY');
update set `chosen`=true where (`pm_id`='P1_3' and `knight_id`='K2') or (`pm_id`='P1_1' and `knight_id`='K1') or(...);