具有多维$ _GET的UPDATE语句

时间:2015-07-13 22:49:56

标签: php sql multidimensional-array

我正在尝试创建一个UPDATE语句,其值为多维$ _GET

  Array
(
    [social_link] => Array
        (
            [0] => https://www.facebook.com
            [1] => https://www.twitter.com
        )

    [social_id] => Array
        (
            [0] => 1
            [1] => 2
        )

    [social_network] => Array
        (
            [0] => facebook
            [1] => twitter
        )

)

我的数据库表格为'Social',其中'_ID , social_network, social_link'行名称为

我试过的SQL

foreach ($_GET as $index ) {
$sql = "UPDATE Social SET social_link= '{$index['social_link']}' WHERE _ID= '{$index['social_id']}' AND social_network= '{$index ['social_network']}' ";

$stmt = $conn->prepare($sql);

$stmt->execute();
}

但它不起作用。

3 个答案:

答案 0 :(得分:1)

我相信这是完成你想要做的事情的一种相当粗略的方法。在尝试进行更新之前,它还将确保所有三个变量都可用。

<?php

// Loop through based on number of social IDs present
for( $i = 0, $icount = count( $_GET['social_id'] ); $i < $icount; $i++ )
{
    // Get values of social_id, social_link, and social_network
    // If they're not set, set them to false
    $social_id      = ( isset( $_GET['social_id'][$i] )      ? $_GET['social_id'][$i]      : false );
    $social_link    = ( isset( $_GET['social_link'][$i] )    ? $_GET['social_link'][$i]    : false );
    $social_network = ( isset( $_GET['social_network'][$i] ) ? $_GET['social_network'][$i] : false );

    // If all values are present for this entry, update the row
    if( $social_id !== false && $social_link !== false && $social_network !== false )
    {
        $sql  = "UPDATE Social 
                 SET    social_link = '{$social_link}'
                 WHERE  _ID = '{$social_id}' AND social_network = '{social_network}'";
        $stmt = $conn->prepare($sql);
        $stmt->execute();
    }
}

?>

您应该根据需要更改它,但我还建议您使用参数绑定进行查询。

答案 1 :(得分:1)

如果您构建像

这样的查询字符串会更容易
social[0][link]=https://www.facebook.com&social[0][id]=1&social[0][network]=facebook&
social[1][link]=https://www.twitter.com&social[1][id]=2&social[1][network]=twitter

http_build_query()可以帮助您构建该字符串。)

这会导致_GET&#34;看起来像&#34;

$_GET === [ 'social'=>
    [
        [ 'link'=>'https://www.facebook.com', 'id'=>'1', 'network'=>'facebook' ],
        [ 'link'=>'https://www.twitter.com', 'id'=>'2', 'network'=>'twitter' ]
    ]
];

和迭代数据的脚本,如

<?php
$sql = "
    UPDATE
        Social
    SET
        social_link=?
    WHERE
        _ID=?
        AND social_network=?
";
$stmt = $conn->prepare($sql)
    or trigger_error( 'prepare failed: '.join(',', $conn->error_list), E_USER_ERROR );
$stmt->bind_param('sss', $link, $id, $network)
    or trigger_error( 'bind failed: '.join(',', $stmt->error_list), E_USER_ERROR );

// <-- test isset($_GET['social']) && is_array($_GET['social']) before here -->
foreach ($_GET['social'] as $row ) {
    // <-- test existence of $row[link,id,network] here -->
    $link = $row['link'];
    $id = $row['id'];
    $network = $row['network'];
    $stmt->execute()
        or trigger_error( 'execute failed: '.join(',', $stmt->error_list), E_USER_ERROR );
}

顺便说一句:您执行UPDATE操作,从客户端的角度来看,这似乎也是脚本的主要操作。因此,您应该使用POST而不是GET。

答案 2 :(得分:0)

对于您发布的数组,您没有在多维数组上进行迭代。

做出一些假设......代码可能是

for ($i = 0; $i <= count($_GET[social_link]); $i++) {
    $sql = "UPDATE Social SET social_link= '{$_GET['social_link'][$i]}' WHERE _ID= '{$_GET['social_id'][$i]}' AND social_network= '{$_GET ['social_network'][$i]}' ";
    $stmt = $conn->prepare($sql);
    $stmt->execute();
}

另外,请记住:每当您在SQL中直接使用$_GET时,SQL injection attack就会杀死一只可爱的小猫。

相关问题