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
}
?>
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
Related
So, I am doing a library system. A librarian can view all the books, and may choose to edit a book and it's details. However, when I click edit, the values do not show up in the input field.
This is my edit portion of the code. I am getting an error:
count(): Parameter must be an array or an object that implements
Countable on line 7
which is if (count($record) == 1 ) {:
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
if (count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$BookNo = $n['BookNo'];
$ISBN = $n['ISBN'];
$title = $n['title'];
$author = $n['author'];
$publisher = $n['publisher'];
$status = $n['status'];
$cost = $n['cost'];
}
}
?>
This is my displaying of the data for the librarian, along with the edit button:
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['BookNo']; ?></td>
<td><?php echo $row['ISBN']; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['author']; ?></td>
<td><?php echo $row['publisher']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['cost']; ?></td>
<td>
<a href="viewBook.php?edit=<?php echo $row['BookNo']; ?>" class="edit_btn" >Edit</a>
</td>
Followed by, the fields in which the librarian can edit the details.
<?php
if (isset($_GET['edit'])) { ?>
<form method="post" action = "viewBook.php">
<input type="hidden" name="BookNo" value="<?php echo $BookNo; ?>">
<input type="text" name="ISBN" value="<?php echo $ISBN; ?>">
<input type="text" name="title" value="<?php echo $title; ?>">
<input type="text" name="author" value="<?php echo $author; ?>">
<input type="text" name="publisher" value="<?php echo $publisher; ?>">
<input type="text" name="status" value="<?php echo $status; ?>">
<input type="text" name="cost" value="<?php echo $cost; ?>">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Save</button>
<?php endif ?>
<?php } ?>
</form>
When I click the edit button, I get the error stated above, as well as the text fields not having the details already written inside.
count() function only works with arrays and other countable fields
use the mysqli_num_rows
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
if (mysqli_num_rows($record) == 1 ) {
$n = mysqli_fetch_array($record);
$BookNo = $n['BookNo'];
$ISBN = $n['ISBN'];
$title = $n['title'];
$author = $n['author'];
$publisher = $n['publisher'];
$status = $n['status'];
$cost = $n['cost'];
}
}
?>
this mysqli_num_rows can give you result of the count of amount of data from sql query
I think this will work for you.
use mysqli_fetch_assoc()
So, I am doing a library system. A librarian can view all the books, and may choose to edit a book and it's details. However, when I click edit, the values do not show up in the input field.
This is my edit portion of the code. I am getting an error:
count(): Parameter must be an array or an object that implements Countable on line 7
which is if (count($record) == 1 ) {:
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM bookinfo WHERE BookNo='$BookNo'");
if (count($record) == 1 ) {
$n = mysqli_fetch_assoc($record);
$BookNo = $n['BookNo'];
$ISBN = $n['ISBN'];
$title = $n['title'];
$author = $n['author'];
$publisher = $n['publisher'];
$status = $n['status'];
$cost = $n['cost'];
}
}
?>
Am looping products out from product table to add them to cart table. only the selected product by checking the checkbox should be added, but if you select, the selected ones do not correspond..
HERE IS THE HTML GETTING THE PRODUCTS OUT
<html>
<form action="#" id="" class="horizontal-form" method="post">
<?php
$LISTP = "SELECT * FROM products ORDER BY id";
$sn = 0;
$stmt = $pdo->prepare($LISTP);
$stmt->execute();
while($list = $stmt->fetch(PDO::FETCH_ASSOC)){
$sn = $sn + 1;
$ID = $list['id'];
$NAME = $list['name'];
?>
<input type="checkbox" name="slected[]" class="checkboxes" value="1" />
<input type="hidden" name="productid[]" class="" value="<?php echo $ID;?>" />
<input type="text" name="name[]" class="" value="<?php echo $NAME;?>" />
<?php }?> </form>
<?php
// now when we submot the form
$slected = $_POST['slected'];
$prod = $_POST['productid'];
$name = $_POST['name'];
foreach($prod as $key => $product){
if($slected[$key]>0){
echo $product.' '.$name[$key].' '.#$slected[#$key].'--<br>';
}
// the problem is here, if you check all product it will work well, but if you check the second one
// it would echo the second one giving it the name of the first one which was not checked at all
?>
I used to do this the same way in the past, and have discovered what I think is a better way. Rather than having a hidden input that stores the ID, just use the ID as the index for all of the form variable keys:
<input type="checkbox" name="slected[<?php echo $ID; ?>]" class="checkboxes" value="1" />
<input type="text" name="name[<?php echo $ID; ?>]" class="" value="<?php echo $NAME;?>" />
Then, your PHP can be simplified:
// now when we submot the form
$slected = $_POST['slected'];
$name = $_POST['name'];
foreach( (array)$slected as $ID => $on ) {
echo $product . ' ' . $name[$ID] . ' ' . $ID . '--<br>';
}
So - basically, your $slected variable will contain an array of only items that are selected, AND you have the product ID built in to the $slected array.
I've got a question about databases and checkboxes. I've got a table looking like:
Website is looking like:
At the bottom of the page I also have a button, so when I submit the checked checkboxes will be updated to 1 or 0 in the database. (True or false)
So when I click on the 3rd checkbox under trained, it will update the trained column in the database with a user/room id of '3583'. (ID is shown right of the screen)
Code:
<form class='verwerkInfo' method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>?license=6'>
<td>
<?php if($room->trained == 1) { ?> <input type='checkbox' name="<?php echo $room->room_id; ?>" checked> <?php echo "Y"; } else{ ?> <input type='checkbox' name="<?php echo $room->room_id; ?>"> <?php echo "N"; }?> </td>
<Td><?php if($room->active == 1) { ?> <input type='checkbox' name="<?php echo $room->room_id; ?>" checked> <?php echo "Active"; } else { ?> <input type='checkbox' name="<?php echo $room->room_id; ?>" <?php echo "Inactive"; } ?>
</td>
<Td><?php echo $room->configuration; ?></td>
<td><?php echo $room->room_id; ?></td>
<td><?php var_dump($room->user_id); }?></td>
</tr>
So I guess I have a problem in the names of the checkboxes.
The query is looking like:
$trainedQuery = "UPDATE room_users
SET trained = 1
WHERE user_id = $room->user_id";
The $room->user_id is referring to the user_id in the database.
Here's a way to give the checkboxes unique names and pass extra information with each element:
name="trained[<?php echo $room->room_id; ?>]" value="<?php echo $room->user_id; ?>"
Then in the PHP script that processes the form submission you can:
foreach ( $_POST['trained'] as $room_id => $user_id ) {
// This query needs protection from SQL Injection!
$trainedQuery = "UPDATE room_users SET trained = 1 WHERE user_id = $user_id";
}
It's not clear what the relationship is between room_id and user_id and why you're updating the room_user table with only user_id. What do you do with the room_id?
Is this what you actually need:
// This query needs protection from SQL Injection!
$trainedClear = "UPDATE room_users SET trained = 0 WHERE user_id = $user_id";
$db->exec($trainedClear); // first clear all
foreach ( $_POST['trained'] as $room_id => $user_id ) {
// This query needs protection from SQL Injection!
$trainedQuery = "UPDATE room_users SET trained = 1
WHERE user_id = $user_id AND room_id = $room_id";
$db->exec($trainedQuery); // then add selections
}
// assuming there's a database connection `$db-exec`.
// Replace with your actual connection and query method.
Refactored checkbox columns for clarity:
<?php
$room_id = $room->id;
$room_configuration = $room->configuration;
$room_user_id = $room->user_id;
if ( $room->trained == 1 ) {
$trained_checked = 'checked';
$trained_label = 'Y';
}
else {
$trained_checked = '';
$trained_label = 'N';
}
if ( $room->active == 1 ) {
$active_checked = 'checked';
$active_label = 'Active';
}
else {
$active_checked = '';
$active_label = 'Inactive';
}
echo <<<EOT
<td><input type="checkbox" name="trained[$room_id]" value="$room_user_id" $trained_checked> $trained_label</td>
<td><input type="checkbox" name="active[$room_id]" value="$room_user_id" $active_checked> $active_label</td>
<td>$room_configuration</td>
<td>$room_id</td>
<td>$room_user_id</td>
EOT;
?>
Just change the checkbox names attribute with adding yes,no and active
name="yes_<?php echo $room->room_id; ?>"
name="no_<?php echo $room->room_id; ?>"
name="act_<?php echo $room->room_id; ?>"
I have some issue about the inserting data. It will insert only the waybillno data but the quantity is always same. Please check my code - I think the model is wrong.
Controller
public function create_cargo_manifest(){
$core_model = new Core_m;
$core_model->save_cargo_details($this->input->post());
redirect('core/cargo_lookup/');
}
Model
function save_cargo_details(){
$quantity = $this->input->post('quantity');
$waybilldate = $this->input->post('waybilldate');
$data = array();
foreach($this->input->post('sys_wbdetails') as $sys_wbdetails) {
$data[] = array(
'waybillno' => $sys_wbdetails,
'quantity' => $quantity,
'waybilldate' => $waybilldate,
);
}
return $this->db->insert_batch('sys_cargodetails', $data);
}
View
<?php foreach($waybill_header as $waybill_header) { ?>
<?php echo form_open('core/create_cargo_manifest'); ?>
<td><input type="checkbox" name="sys_wbdetails[]" value="<?php echo $waybill_header->waybillno; ?>"></td>
<td><?php echo $waybill_header->waybillno; ?></td>
<td><?php echo $waybill_header->waybilldate; ?><input type="hidden" value="<?php echo $waybill_header->waybilldate; ?>" name="waybilldate"></td>
<td><input type="text" size="5" value="<?php echo $waybill_header->quantity; ?>" name="quantity"></td>
<td><input type="submit" value="save"></td>
<?php } ?>
<?php form_close(); ?>
All your inputs for quantity have the same name : quantity. So you're only submitting the last value in your form. You need to use an array for those inputs (quantity[]), just like for your checkboxes. And you might want to do the same for the waybilldate inputs.
<td><input type="text" size="5" value="<?php echo $waybill_header->quantity; ?>" name="quantity[]"></td>
And then in PHP, something like that :
$data = array();
// Count distinct entries in the form
$count = count($this->input->post['sys_wbdetails']);
for($i=0; $i < $count; $i++) {
$data[] = array(
'waybillno' => $this->input->post['sys_wbdetails'][$i],
'quantity' => $this->input->post['quantity'][$i],
'waybilldate' => $this->input->post['waybilldate'][$i],
);
}
EDIT : also, take a look at this answer if you want a clean way to keep track of which form input goes where.
I am developing an ordering page in PHP for some products. The user needs to be able to select multiple types of products, so checkboxes will be necessary in the HTML form.
I've established an array for the checkboxes via the "name" attribute.
My problem is, I want to display what the user has selected on a confirmation page, so I'd like the "value" of those checkboxes to ultimately return a product name AND a product price. As far as I can tell I'm going to be stuck with one value only, so I've attempted to get around this:
<?php
//variables for the array and the count
$parray = $_POST['product'];
$pcount = count($parray);
//arrays for each product's information; i've only listed one for simplicity
$br = array("Big Red", 575);
/*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/
if (!empty($parray))
{
for ($i=0; $i < $pcount; $i++)
{
$key = $parray[i];
echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>";
}
}
?>
Is there anyway to make my $key variable actually act as if it is the array's name it is set to?
If not, is there any good way to do this?
So in your HTML table, you'll need to render each checkbox like this:
<input type="checkbox" name="selectedIDs[]" value="$key" />
where $key is the item number.
Then, in your PHP, you'll have a $_POST["selectedIDs"] variable, which is an array of all the item numbers that were checked.
Assuming you've got a product list array like this:
$products = array( 1 => array("Big Red", 575), 2 => array("Spearmint", 525));
You can then print a list of the selected products using a loop like this:
for($i = 0; $i < count($_POST["selectedIDs"]); $i++) {
$key = $_POST["selectedIDs"][$i];
$product = $products[$key];
echo "<tr><td height='40'></td><td>" . $product[0] . "</td><td>" . $product[1] . "</td></tr>";
}
The only real difference between this and what you wrote is that my $products array is two-dimensional, and my $key is used to grab the relevant product from the $products array.
You can give the value of the options the value of the ID of the corresponding item. Then you'll receive an array of ID's, which you can then again map to the $br array, where you look up the name and price of an item by its ID. You could use the array key as ID, so:
<!-- Form: -->
<input type="checkbox" name="product[]" value="1" />
<input type="checkbox" name="product[]" value="..." />
<input type="checkbox" name="product[]" value="15" />
[...]
<?php
$product = array();
$product[1] = array('name' => "Big Red", 'price' => 575);
$product[...] = array('name' => "...", 'price' => ...);
$product[15] = array('name' => "Small Green", 'price' => 475);
foreach ($_POST['product'] as $pId)
{
echo $products[$pId]['name'] . " costs " . $products[$pId]['price'];
}
?>
If of course your array originates from a database, you can use the table's ID's as values for the checkboxes. You could then implode the $_POST['product'] (of course after escaping it) with a comma, and use it in a SELECT ... WHERE ID IN ($productIds) query.
If i'm understanding you correctly, you'll have checkboxes something like this:
<input type="checkbox" name="product_id[]" value="1">Product #1<br/>
<input type="checkbox" name="product_id[]" value="2">Product #2<br/>
<input type="checkbox" name="product_id[]" value="3">Product #3<br/>
That should post whatever is checked as an array to your server.
gen_paper1.php
<div style="overflow: auto; height: 500px; border: 1px solid;">
<form action="" method="post">
<fieldset style="font-size: 15px; font-weight: bolder;">
<h4><u>Your Existing Header's are listed below</u> </h4>
<hr / >
<?php
$xhdr = mysql_query("SELECT * from s_user_header ORDER BY 'ASC'");
while($rheader = mysql_fetch_array($xhdr)){
$h = $rheader['h_content'];
$hid = $rheader['h_id'];?>
<script>
$(document).ready(function(){
//$('input[type=checkbox]').val('<?php echo $hid ; ?>').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
//$('#c').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
$('').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
});
</script>
<table>
<td><input type="checkbox" id="c" name="u_hdr[]" value="<?php echo $hid; ?>"></td>
<td style="font-weight: bolder"><?php echo $h."<br />"; ?></td>
</table>
<?php } ?>
</fieldset>
</div>
gen_paper2.....3.....
i need all this values in further page, so have taken hidden variables
<input type="hidden" name="hstdid" value="<?php echo $stdid; ?>">
<input type="hidden" name="hsubid" value="<?php echo $subid; ?>">
<input type="hidden" name="hdifid" value="<?php echo $difid; ?>">
<input type="hidden" name="hmarks" value="<?php echo $t_marks; ?>">
<input type="hidden" name="h_hid[]" value="<?php echo $hh; ?>">
gen_paper4.php
$getstdid = $_POST['hstdid3'] ;
$getsubid = $_POST['hsubid3'] ;
$getdifid = $_POST['hdifid3'] ;
$gethmarks = $_POST['htmarks'] ;
$hdr= $_POST['h_hdrid'] ;
$h = implode($hdr);
<table>
<?php
$h1 = explode(",",$h);
count($h1) . "<br />";
for($i=0;$i<count($h1);$i++){
$h1[$i];
$xheader = mysql_query("SELECT * from s_user_header WHERE h_id = ".$h1[$i]);
while($row = mysql_fetch_array($xheader)){ ?>
<tr>
<td><b>
<?php echo $i + 1 . ".   "; ?>
</b></td><td>
<?php echo $header = $row['h_content'] . "<br />";
?></td>
</tr>
<?php
}
}
//echo $header; ?>
</table>