1

I have a problem to insert and update database at the same time, I have a form that user can edit the information or add new field, what I want is when an user edits the form and if the user adds new filed(s) I want I can insert and update the database together.

Here is my code

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";
}
}

In edit page I added hidden input section_id to get primary key for the field is being edit, and will give NULL for a new field, on action I try to do if section_id != 'NULL' then UPDATE the field.

I tried to use ON DUPLICATE KEY UPDATE to check if section_id is duplicate but it does not work at all.

Sorry about my English, any help will be appreciated :)

3
  • Why down vote my question? if you think you have a solution for my question please share your solution instead down vote my question. Commented Sep 3, 2015 at 3:06
  • do you need a sample of insert on duplicate update example Commented Sep 3, 2015 at 3:13
  • Hi @Drew if possible yes please, thanks :) Commented Sep 3, 2015 at 3:38

1 Answer 1

2

All it takes is one index clash that would violate a duplicate for the row to be updated, and not for a new row to be created. The index clash can be a primary key, a one on another index be it single column or composite index across multiple columns.

Granted the below is rather lame, but as imaginative as I can do right now.

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 |
+----+----------+-------------+
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, you are right I should use on duplicate key update field=field+1, got it thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.