Too Many Post Data variables?

我与影子孤独终老i 提交于 2019-11-28 10:46:27

问题


It's my first time posting a question:

I'm working on a WordPress plugin that allows the user to create rows of data in the database. I'm experiencing a problem when there are many (upwards of 100) rows of data to be updated by a form. Each row of data holds eight POST data variables, so when there are 100 rows in the form, over 800 post variables are sent. However, only a certain number of the variables update the database, right now only 112 rows update. I can't figure out what would stop the function from completing the update to the database. It almost seems like I'm overloaded with too many post variables or post data size?

Everything works perfectly with fewer entries, but once it goes over 100 rows, things stop working.

Here is my table structure:

$sql2 = "CREATE TABLE IF NOT EXISTS $item_table (
    id smallint(5) NOT NULL AUTO_INCREMENT,
    menu smallint(5) NOT NULL,
    itemorder smallint(5) NOT NULL,
    item text NOT NULL,
    description text,
    image tinytext NOT NULL,
    value tinytext NOT NULL,
    value2 tinytext NOT NULL,
    UNIQUE KEY id (id)
    ) $charset_collate;";
}

Here is my POST data handler function:

foreach($_POST['id'] as $i){

    $image = $_POST['image'][$i];
    $item = $_POST['item'][$i];
    $desc = $_POST['desc'][$i];
    $value = $_POST['value'][$i];
    $value2 = $_POST['value2'][$i];
    $order = $_POST['order'][$i];

    if ($_POST['strike'][$i] == 'checked' ){
        $wpdb->query( $wpdb->prepare("DELETE FROM $item_table WHERE id = $i") );
    }
    else{
        $wpdb->update( $item_table, array(
            'image' => $image,
            'item' => $item,
            'itemorder' => $order,
            'description' => $desc,
            'value' => $value,
            'value2' => $value2
         ),
         array( 'id' => $i ) );
        }
    }

    //Sort items by order, then rewrite the order with no gaps left from deleted items
    $targetmenu = $_POST['targetmenu'];

    $rows = "SELECT * FROM $item_table WHERE menu = $targetmenu ORDER by itemorder ASC";
    $result = $wpdb->get_results($rows);
    $n = 1;
    foreach ($result as $r){
        $id = $r->id;
        $wpdb->update( $jsrm_item_table , array( 'itemorder' => $n ), array( 'id' => $id ) );
        ++$n;
    }
    $loc = "&mode=editmenu&targetmenu=".$targetmenu;

    header("Location:".JSRM_SELF.$loc);
    exit(); 

}

And here is my PHP Form:

$the_menu = $wpdb->get_row("SELECT * FROM $menu_table WHERE id = $_GET[targetmenu]");
$menuid = $the_menu->id;

$q = "SELECT * FROM $item_table WHERE menu = $menuid ORDER by itemorder ASC";
$result = $wpdb->get_results($q);
if ($result) {
?>
<form id="edit-menu-form" action="<?php echo _SELF; ?>" method="post">
    <input type="hidden" name="targetmenu" value="<?php echo $menuid; ?>">
    <input type="hidden" name="dbtouch" value="updateitems">

<table>
    <?php
    foreach ($result as $r) {
        $order = $r->itemorder;
        $image = $r->image;
        $imagesrc = ($image) ? esc_html(stripslashes($r->image)) : 'addimage.jpg';
        $item = esc_html(stripslashes( $r->item ));
        $description = esc_html(stripslashes($r->description));
        $value = esc_html(stripslashes($r->value));
        $value2 = esc_html(stripslashes($r->value2));
        $id = $r->id;
    ?>

    <tr id="<?php echo $id ?>">
        <td><?php echo $order ?></td>
        <td><a class="edit-item-img" id="item-image-<?php echo $id ?>" style="background-image:url(<?php echo $imagesrc ?>);" title="Edit image"></a>
            <input type="hidden" name="image[<?php echo $id ?>]" id="field-item-image-<?php echo $id ?>" value="<?php echo $image ?>" />
            <img class="remove-image-button" id="image-<?php echo $id ?>" src="removeimage.png"
                <?php if(!$image){ ?> 
                    style="visibility:hidden;" 
                <?php } ?>
            />
        </td>
        <td><textarea name="item[<?php echo $id ?>]"><?php echo $item ?></textarea></td>
        <td><textarea name="desc[<?php echo $id ?>]"><?php echo $description ?></textarea></td>
        <td><input type="text" name="value[<?php echo $id ?>]" value="<?php echo $value ?>" /></td>
        <td><input type="text" name="value2[<?php echo $id ?>]" value="<?php echo $value2 ?>" /></td>
        <td><input type="checkbox" class="strike" name="strike[<?php echo $id ?>]" value="checked"/></td>
        <input type="hidden" name="order[<?php echo $id ?>]" value="<?php echo $order ?>" id="order<?php echo $id ?>"/>
        <input type="hidden" name="id[<?php echo $id ?>]" value="<?php echo $id ?>" id="id<?php echo $id ?>"/>
    </tr>

    <?php
    }
    ?>  
</table/>
<p><input type="submit" id="update-items-button" value="Update All" class="button-primary"/></p>
</form>

<?php
}   
?>

回答1:


I had a similar problem today. I had a form with 250+ rows and 5 variables per row, but the $_POST variable appeared to be truncated. In my case, it stopped after 1000 elements.

There is a PHP setting called max_input_vars that defaults to 1000. This setting sets an upper limit on how many variables it will pull into your PHP script. You may need to increase this value on your server settings to make your page work. There are some security implications that I don't fully understand with increasing this value that could enable a denial of service attack.

Since you are developing a Wordpress plugin, you may need to see if there are ways to change your form to reduce the number of variables you send, because you probably can't alter server configurations for people using your plugin.

Read more about the setting here: http://www.php.net/manual/en/info.configuration.php#ini.max-input-vars



来源:https://stackoverflow.com/questions/13617339/too-many-post-data-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!