簡體   English   中英

將多個數據插入MySQL並更新(如果存在)

[英]Insert multiple Data to MySQL and Update if existing

我在同時插入和更新數據庫時遇到問題,我有一個表單,用戶可以編輯信息或添加新字段,我想要的是用戶編輯表單時以及如果用戶添加了新文件,我希望我可以一起插入和更新數據庫。

這是我的代碼

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 ,如果section_id != 'NULL'則嘗試執行操作,然后UPDATE該字段。

我嘗試使用ON DUPLICATE KEY UPDATE來檢查section_id是否重復,但它根本不起作用。

對不起,我的英語,我們將不勝感激:)

它所要做的只是一個索引沖突,該沖突將違反要更新的行而不是要創建的新行的重復項。 索引沖突可以是一個主鍵,一個在另一個索引上可以是主鍵,可以是單列,也可以是跨多列的復合索引。

當然,以下內容相當la腳,但正如我現在所能想象的那樣。

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 |
+----+----------+-------------+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM