MYSQL更新,用PHP数组替换delete

发布于 2024-10-10 19:28:52 字数 1032 浏览 0 评论 0原文

我有一个查询,我需要使用 php 在 $_POST 数组上运行它非常简单,因为它获取数组中的所有值,然后对更新数据库的每个值运行查询,这是棘手的部分:取决于用户选择的复选框,查询必须更新、替换或删除数据库中的值,具体取决于 $_POST 数组中的内容。我只能让查询成功运行数组中的最后一个值。
我尝试过“替换为”和“插入忽略为”,但没有成功。我开始认为需要更复杂的编码:(这是我到目前为止所拥有的:

foreach($params['text'] as $boilertext)
{
    $sql = 'UPDATE '._DB_PREFIX_.'boilertexts 
            SET id_product="'.pSQL($boilertext).'" WHERE id_text="'.$params['id'].'"';
    if(!Db::getInstance()->Execute($sql))
    {
        echo mysql_error();
    }
}

编辑:

这是我得到的帖子参数:

Array ( 
       [text] => Array ( [0] => 8 [1] => 6 [2] => 10 ) // the Text ID i'm updating
       [textValue] => "Some text that I'm adding to another DB table"
       [id] => 28 // The ID that all the params from ['text'] are linked too
       [update] => update // Name of the submit button I'm using for form processing
     ) 

我的问题是,尽管 foreach 正在运行 SQL x 数量每个查询的次数(通过打印每个查询的计数来测试)它只会插入 ['text'] 数组中的最后一个值,如果是的话,它似乎也不会从数据库中更新/删除文本 ID。不在数组中

I have a query I need to run on a $_POST array with php it's quite simple as it takes all the values in the array then runs a query for each of the values which updates the database, here is the tricky part: As the array is dependent of checkboxes selected by the user the query has to either Update, Replace or remove values in DB dependent on what is in the $_POST array. I've only been able to have the query successfully run the last value in the array.
I've tried both 'REPLACE INTO' and 'INSERT IGNORE INTO' but with no luck. I'm starting to think much more complex coding is going to be needed :( here's what I have so far:

foreach($params['text'] as $boilertext)
{
    $sql = 'UPDATE '._DB_PREFIX_.'boilertexts 
            SET id_product="'.pSQL($boilertext).'" WHERE id_text="'.$params['id'].'"';
    if(!Db::getInstance()->Execute($sql))
    {
        echo mysql_error();
    }
}

EDIT:

This is the post params i'm getting:

Array ( 
       [text] => Array ( [0] => 8 [1] => 6 [2] => 10 ) // the Text ID i'm updating
       [textValue] => "Some text that I'm adding to another DB table"
       [id] => 28 // The ID that all the params from ['text'] are linked too
       [update] => update // Name of the submit button I'm using for form processing
     ) 

My issue is that although the foreach is running the SQL x amount of times for each query (tested it by printing out a count for each query) it will only insert the last value in ['text'] array. It also doesn't seem to update/remove the text id from the DB if it is not in the array

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2024-10-17 19:28:52

它最终只会设置为最后一个值,因为您要更新同一记录的次数与数组中的值相同。

仅处理一条记录中的一个字段,它不能通过这种方式神奇地变成多条记录 - 您需要一个单独的表,或使用 INSERT

It will only end up set as the last value, as you are updating the same record the number of times that you have values in your array.

Only one field in one record is being processed, it can not magically become multiple records this way - you would need a separate table, or use INSERT.

但可醉心 2024-10-17 19:28:52

您真正要做的是对同一条记录进行 3 次更新。这就是为什么您只获得数据库中的最后一个值。

您正在使用值 8、6、10 更新记录 id_text=28。

What you really do is 3 updates on same record . Thats why you get only last value in database.


You are updating record id_text=28 with values 8,6,10.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文