Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当密钥不总是已知时,在重复密钥更新时插入?_Php_Mysql - Fatal编程技术网

Php 当密钥不总是已知时,在重复密钥更新时插入?

Php 当密钥不总是已知时,在重复密钥更新时插入?,php,mysql,Php,Mysql,如果密钥已知,我正在尝试更新记录,否则我要插入它并获取插入的id,目前我有: if(isset($data['applicationId'])) { //update $sql=" UPDATE myTable SET data='jsonstring' WHERE id = {$data['applicationId']} "; } else { //insert and

如果密钥已知,我正在尝试更新记录,否则我要插入它并获取插入的id,目前我有:

    if(isset($data['applicationId']))
    {
        //update
        $sql="
         UPDATE myTable SET data='jsonstring' WHERE id = {$data['applicationId']}
        ";
    }
    else
    {
        //insert and get id
        $sql="
         INSERT INTO myTable SET data='jsonstring'
        ";
    }
是否可以使用
INSERT…ON DUPLICATE KEY UPDATE
将上述查询简化为一个查询,即使密钥并不总是已知

我试过这个:

        INSERT INTO myTable
        (
            id,
            data

        )
        VALUES
        (
            ?, # <- I may not know this!!
            'jsonstring'
        )
        ON DUPLICATE KEY UPDATE 
            data = 'jsonstring'
插入myTable
(
身份证件
数据
)
价值观
(

?,#请原谅,因为您的问题不是100%清楚。但是,我可以告诉您的概念是,您希望能够对一条sql语句进行多个查询。这可以通过多查询命令完成。但是,如果您希望在下一个查询中放置查询中的一些数据,我认为它不会起作用。为多查询提供的链接y


是的,您可以这样做,假设
id
是您的主键并自动递增。您将有两个不同的查询,一个是您知道应用程序id时的查询,另一个是您不知道应用程序id时的查询

第一,当你知道的时候:

INSERT INTO myTable
(
    id,
    data
)
VALUES
(
    1337, # <- insert id
    'jsonstring'
)
ON DUPLICATE KEY UPDATE 
        data = 'jsonstring';
但是要注意


快乐编码

您能详细说明您的问题吗?
惰性重复密钥更新
应该可以正常工作。也就是说,如果您通过的
id
已经插入,
数据
将更新为'jsonstring',否则将插入带有您提供的
id
的新记录。您是否收到可以忽略的错误是吗?看起来很有希望,明天早上我会试试,然后告诉你。
First, Simple update query will run. If it runs successfully, it will not go to if condition and your ID will be the one which was used in updating.

And, if that ID is not available (means update query fails, $Query will be false), so pointer jumps to if condition and insert the query. Now, new Inserted ID we can get.

$ID=$data['applicationId'];
    $Query=mysql_query("UPDATE myTable SET data='jsonstring' WHERE id='$ID' ");
    if(!$Query)
    {
       $InsertQuery=mysql_query("INSERT INTO myTable SET data='jsonstring'");
       $ID=mysql_insert_id();
    }

    So, $ID will be your ID.(either updated or currently inserted)
    $sql="INSERT INTO myTable
        (
            id,
            data
        )
        VALUES
        (" .
            isset($data['applicationId']) ? $data['applicationId'] : 'NULL'
            .",
            'jsonstring'
        )
        ON DUPLICATE KEY UPDATE 
                data = 'jsonstring';
    ";
First, Simple update query will run. If it runs successfully, it will not go to if condition and your ID will be the one which was used in updating.

And, if that ID is not available (means update query fails, $Query will be false), so pointer jumps to if condition and insert the query. Now, new Inserted ID we can get.

$ID=$data['applicationId'];
    $Query=mysql_query("UPDATE myTable SET data='jsonstring' WHERE id='$ID' ");
    if(!$Query)
    {
       $InsertQuery=mysql_query("INSERT INTO myTable SET data='jsonstring'");
       $ID=mysql_insert_id();
    }

    So, $ID will be your ID.(either updated or currently inserted)