SQL插入和更新不会更新或插入所有记录,只有少数记录

时间:2015-07-23 07:30:01

标签: php mysql

我的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

- http://www.phpmyadmin.net

- 主持人: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_ids_id);

-

- 表message

的索引

ALTER TABLE message   添加PRIMARY KEY(m_id);

-

- 表message_send

的索引

ALTER TABLE message_send   添加密钥m_idm_id);

-

- 表projects

的索引

ALTER TABLE projects   添加主要密钥(pro_id),添加密钥emp_idemp_id),添加密钥s_ids_id);

-

- 表project_milestone

的索引

ALTER TABLE project_milestone   添加主要密钥(pm_id),添加密钥pm_idpm_idpro_idmilestone),添加密钥project_milestone_ibfk_1({{1} });

-

- 表pro_id

的索引

ALTER TABLE project_waiting   添加主要密钥(project_waitingknight_id),添加密钥pm_idknight_id),添加密钥knight_idpm_id);

-

- 表pm_id

的索引

ALTER TABLE project_working   添加主要密钥(project_workingpm_id),添加密钥knight_idpm_idpm_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_1s_id)ON DELETE CASCADE ON UPDATE CASCADE;

-

- 表skills

的约束

ALTER TABLE s_id 添加约束message_send FOREIGN KEY(message_send)引用message_send_ibfk_1m_id)ON DELETE CASCADE ON UPDATE CASCADE;

-

- 表message

的约束

ALTER TABLE m_id 添加约束projects FOREIGN KEY(projects)引用projects_ibfk_1emp_id)ON DELETE CASCADE ON UPDATE CASCADE, 添加约束employers FOREIGN KEY(emp_id)引用projects_ibfk_2s_id)ON DELETE CASCADE ON UPDATE CASCADE;

-

- 表skills

的约束

ALTER TABLE s_id 添加约束project_milestone外键(project_milestone)参考project_milestone_ibfk_1pro_id);

-

- 表projects

的约束

ALTER TABLE pro_id 添加约束project_waiting FOREIGN KEY(project_waiting)引用project_waiting_ibfk_1pm_id)ON DELETE CASCADE ON UPDATE CASCADE, 添加约束project_milestone FOREIGN KEY(pm_id)引用project_waiting_ibfk_2knight_id)ON DELETE CASCADE ON UPDATE CASCADE;

-

- 表knights

的约束

ALTER TABLE knight_id 添加约束project_working FOREIGN KEY(project_working)引用project_working_ibfk_1pm_id)ON DELETE CASCADE ON UPDATE CASCADE, 添加约束project_milestone FOREIGN KEY(pm_id)引用project_working_ibfk_2knight_id)ON DELETE CASCADE ON UPDATE CASCADE;

1 个答案:

答案 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(...);