将多个数据插入MySQL并更新(如果存在)

时间:2015-09-03 02:35:29

标签: php mysql

我有同时插入和更新数据库的问题,我有一个表单,用户可以编辑信息或添加新字段,我想要的是用户编辑表单时以及用户是否添加新字段( s)我希望我可以一起插入和更新数据库。

这是我的代码

function insert_update_db(){
global $db;

$section = $_POST["page"];

$fieldsArray = array(   "section_id", // Primary key
                        "section_title",
                        "section_name",
                        "section_content",
                    );


$fields   = '`' . implode('`, `', $fieldsArray ) . '`';


$sql    = "INSERT INTO `db_section` ($fields) VALUES"; 
$valueArray = array();
$indexKey   = array(); 

foreach ($section["section"] as $value) {


    $section_id             = ($value["section_id"] != "" ? $db->quote($value["section_id"]) : "NULL" ); // Check if curr field has a ID
    $title                  = $value["title"];
    $name                   = $value["name"];
    $content                = $value["content"];


    $valueArray[] = "($section_id, '$title','$name', '$content')";

    if($section_id != "NULL"){
        $indexKey[] = str_replace("'", "", $section_id);
        $sql_update = "UPDATE `db_section` SET 
                                `section_title` = '$section_title',
                                `section_name` = '$name',
                                `section_content` = $content
                    WHERE `section_id` = $section_id;";

        $update = $db->query($sql_update);

        echo $sql_update;

        if($update){
            $db->sql_status = "Success";
        }
    }
}

$sql .= implode(",", $valueArray);

$sql .= " ON DUPLICATE KEY UPDATE ";

$sql .= "section_id=" . implode(" AND section_id=", $indexKey);

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

if($insert){
    $db->sql_status = "Success";
}else{
    $db->sql_status = "Error";
}
}

在编辑页面中,我添加了隐藏的输入section_id以获取正在编辑该字段的主键,并为新字段提供NULL,如果{{1}我尝试执行的操作然后section_id != 'NULL'字段。

我尝试使用UPDATE来检查ON DUPLICATE KEY UPDATE是否重复,但它根本不起作用。

对不起我的英语,我们将不胜感激任何帮助:)

1 个答案:

答案 0 :(得分:2)

所需要的只是一个索引冲突,它会违反要更新的行的副本,而不是要创建新行。索引冲突可以是主键,另一个索引可以是单列,也可以是多列的复合索引。

当然,下面的内容相当蹩脚,但我现在可以做到富有想象力。

create table user
(
    id int auto_increment primary key,
    userName varchar(20) not null,
    friendCount int not null,
    unique key(userName)
);

insert user(userName,friendCount) values('Jason7',0) on duplicate key update friendCount=friendCount+1;
select * from user;
+----+----------+-------------+
| id | userName | friendCount |
+----+----------+-------------+
|  1 | Jason7   |           0 |
+----+----------+-------------+

insert user(userName,friendCount) values('Fred',0) on duplicate key update friendCount=friendCount+1;
select * from user;
+----+----------+-------------+
| id | userName | friendCount |
+----+----------+-------------+
|  1 | Jason7   |           0 |
|  2 | Fred     |           0 |
+----+----------+-------------+

insert user(userName,friendCount) values('Fred',0) on duplicate key update friendCount=friendCount+1;
+----+----------+-------------+
| id | userName | friendCount |
+----+----------+-------------+
|  1 | Jason7   |           0 |
|  2 | Fred     |           1 |
+----+----------+-------------+