mochaMame
mochaMame

Reputation: 57

PHP/MySQL stops Updating after table reaches certain number of rows

I have a script that loads all rows and columns of a table at the same time as text fields so they can be edited simultaneously, then pushing a submit button MySQL table is UPDATED with all values.

A separate button will add or delete rows. Basically it's an inventory system.

This works great for me until my table reaches about 150 rows at which point the script will display all table values but updating and deleting will no longer work. Adding new rows is still working.

There are no errors, it looks exactly the same as when the updates are actually working. I'm running a recent version of WAMP.

It seems like a memory issue. I've tried the following modifications, it is my first time working with databases so there might be something simple I missed:

PHP.ini
memory_limit = 2048M
post_max_size = 128M
upload_max_filesize = 64M

MY.ini
innodb_buffer_pool_size = 512M
key_buffer_size = 256M
max_allowed_packet = 128M
sort_buffer_size = 128M
net_buffer_length = 8K
read_buffer_size = 64M
read_rnd_buffer_size = 64M
myisam_sort_buffer_size = 512M
ead_buffer_size = 10M
write_buffer_size = 10M
sort_buffer_size_size = 40M
net_buffer_length = 64K

My table has 17 columns at the moment. Dropping columns will allow me to add 10 or 20 new rows but then the problem happens again.

So my questions: 1. How can I cheat and force my sloppy code to work. 2. How can I clean up my code to take care of things 'properly'.

Just a disclaimer, my code is most likely full of security issues, feel free to comment if you must but please give good explanations/examples when you do because this is my first project and I won't be able to understand your one liners.

Here is my code for the problem page. Remember that it works perfectly fine for me when my table is small. At the moment my UPDATE function is divided into 5 UPDATES, this was just a test but didn't seem to help. Originally the whole thing was a single UPDATE.

        <?php
include_once $_SERVER['DOCUMENT_ROOT'] . "/NAMAsoft/mochaOFFICE/main/links_config.php";
include_once ($standard_headtag);
ini_set('memory_limit', '1024M');
?>
</head>
<body style=''>

<?php
include ($DBconnect);
$tbl_name="inventorylist"; // Table name 
echo ini_get("memory_limit")."\n";
ini_set("memory_limit","2048M");
echo ini_get("memory_limit")."\n";

echo "
<style>
table { width:1200px;table-layout:fixed;}
table tr { height:1em;  }
td {overflow:hidden;white-space:nowrap;  }
</style>
<form name='form1' method='post' action=''>
<table border='0' cellspacing='1' cellpadding='0'>
<tr>
<td width='80px' align='center'><strong>Department</strong></td>
<td width='80px' align='center'><strong>品名</strong></td>
<td width='80px' align='center'><strong>Product#</strong></td>
<td width='80px' align='center'><strong>Maker</strong></td>
<td width='80px' align='center'><strong>Cost</strong></td>
<td width='80px' align='center'><strong>/Per</strong></td>
<td width='80px' align='center'><strong>Import Fee %</strong></td>
<td width='80px' align='center'><strong>Prep Cost</strong></td>
<td width='80px' align='center'><strong>MOQ</strong></td>
<td width='80px' align='center'><strong>Weight</strong></td>
<td width='80px' align='center'><strong>Size (t)</strong></td>
<td width='80px' align='center'><strong>Category</strong></td>
<td width='80px' align='center'><strong>Page</strong></td>
<td width='80px' align='center'><strong>Specs</strong></td>
<td width='80px' align='center'><strong>Comment</strong></td>
<td width='80px' align='center'><strong>Stock</strong></td>
</tr>
</td>
<td align='center'>
        <select name='department_add' id='department_add' data-native-menu='false'>";
        $sql1 = "SELECT * FROM menulist";
        $result1 = $conn->query($sql1);
        if ($result1->num_rows > 0) {
        $count1 = $result1->num_rows;
        // output data of each row
        while($row1 = $result1->fetch_assoc()) {
        echo "<option value='".$row1['department']."'>".$row1['department']."</option>";}
        } else {
        echo "Error: " . $sql1 . "<br>" . $conn->error;
        }
            echo "
        </select>
</td>
<td align='center'>
<input name='productname_add' type='text' id='productname_add' value=''>
</td>
<td align='center'>
<input name='productnum_add' type='text' id='productnum_add' value=''>
</td>
<td align='center'>
<input name='maker_add' type='text' id='maker_add' value=''>
</td>
<td align='center'>
<input name='cost_add' type='text' id='cost_add' value=''>
</td>
<td align='center'>
<input name='per_add' type='text' id='per_add' value=''>
</td>
<td align='center'>
<input name='importfee_add' type='text' id='importfee_add' value=''>
</td>
<td align='center'>
<input name='prepcost_add' type='text' id='prepcost_add' value=''>
</td>
<td align='center'>
<input name='MOQ_add' type='text' id='MOQ_add' value=''>
</td>
<td align='center'>
<input name='weight_add' type='text' id='weight_add' value=''>
</td>
<td align='center'>
<input name='sizeT_add' type='text' id='sizeT_add' value=''>
</td>
<td align='center'>
        <select name='category_add' id='category_add' data-native-menu='false'>";
        $sql2 = "SELECT * FROM menulist";
        $result2 = $conn->query($sql2);
        if ($result2->num_rows > 0) {
        $count2 = $result2->num_rows;
        // output data of each row
        while($row1 = $result2->fetch_assoc()) {
        echo "<option value='".$row1['category']."'>".$row1['category']."</option>";}
        } else {
        echo "Error: " . $sql2 . "<br>" . $conn->error;
        }
        echo "
        </select>
</td>
<td align='center'>
        <select name='page_add' id='page_add' data-native-menu='false'>";
        $sql3 = "SELECT * FROM menulist";
        $result3 = $conn->query($sql3);
        if ($result3->num_rows > 0) {
        $count3 = $result3->num_rows;
        // output data of each row
        while($row1 = $result3->fetch_assoc()) {
        echo "<option value='".$row1['page']."'>".$row1['page']."</option>";}
        } else {
        echo "Error: " . $sql3 . "<br>" . $conn->error;
        }
        echo "
        </select>
</td>
<td align='center'>
<input name='specs_add' type='text' id='specs_add' value=''>
</td>
<td align='center'>
<input name='comment_add' type='text' id='comment_add' value=''>
</td>
<td align='center'>
<input name='stock_add' type='text' id='stock_add' value=''>
</td>
</tr>
</table>
<input type='submit' name='Add' value='Add'>
</form>
<br>
<br>";




echo "
<style>
table { width:1260px;table-layout:fixed;}
table tr { height:1em;  }
td {overflow:hidden;white-space:nowrap;  }
</style>
<form name='form1' method='post' action=''>
<table border='0' cellspacing='1' cellpadding='0'>
<tr>
<td width='30px' align='center'><strong>DEL</strong></td>
<td width='30px' align='center'><strong>Id</strong></td>
<td width='80px' align='center'><strong>Department</strong></td>
<td width='80px' align='center'><strong>品名</strong></td>
<td width='80px' align='center'><strong>Product#</strong></td>
<td width='80px' align='center'><strong>Maker</strong></td>
<td width='80px' align='center'><strong>Cost</strong></td>
<td width='80px' align='center'><strong>/Per</strong></td>
<td width='80px' align='center'><strong>Import Fee %</strong></td>
<td width='80px' align='center'><strong>Prep Cost</strong></td>
<td width='80px' align='center'><strong>MOQ</strong></td>
<td width='80px' align='center'><strong>Weight</strong></td>
<td width='80px' align='center'><strong>Size (t)</strong></td>
<td width='80px' align='center'><strong>Category</strong></td>
<td width='80px' align='center'><strong>Page</strong></td>
<td width='80px' align='center'><strong>Specs</strong></td>
<td width='80px' align='center'><strong>Comment</strong></td>
<td width='80px' align='center'><strong>Stock</strong></td>
</tr>";

$sql = "SELECT * FROM $tbl_name";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    $count = $result->num_rows;
    // output data of each row
    while($row = $result->fetch_assoc()) {

echo "
<tr>
<td align='center'>
<input type='checkbox' name='checkbox[]' value='" .$row['id']. "'>
</td>
<td align='center'> ";
$id[]=$row['id'];
echo $row['id'];
echo "
</td>

<td align='center'>
<input name='department[]' type='text' id='department' value='" .$row['department']. "'>
</td>
<td align='center'>
<input name='productname[]' type='text' id='productname' value='" .$row['productname']. "'>
</td>
<td align='center'>
<input name='productnum[]' type='text' id='productnum' value='" .$row['productnum']. "'>
</td>
<td align='center'>
<input name='maker[]' type='text' id='maker' value='" .$row['maker']. "'>
</td>
<td align='center'>
<input name='cost[]' type='text' id='cost' value='" .$row['cost']. "'>
</td>
<td align='center'>
<input name='per[]' type='text' id='per' value='" .$row['per']. "'>
</td>
<td align='center'>
<input name='importfee[]' type='text' id='importfee' value='" .$row['importfee']. "'>
</td>
<td align='center'>
<input name='prepcost[]' type='text' id='prepcost' value='" .$row['prepcost']. "'>
</td>
<td align='center'>
<input name='MOQ[]' type='text' id='MOQ' value='" .$row['MOQ']. "'>
</td>
<td align='center'>
<input name='weight[]' type='text' id='weight' value='" .$row['weight']. "'>
</td>
<td align='center'>
<input name='sizeT[]' type='text' id='sizeT' value='" .$row['sizeT']. "'>
</td>
<td align='center'>
<input name='category[]' type='text' id='category' value='" .$row['category']. "'>
</td>
<td align='center'>
<input name='page[]' type='text' id='page' value='" .$row['page']. "'>
</td>
<td align='center'>
<input name='specs[]' type='text' id='specs' value='" .$row['specs']. "'>
</td>
<td align='center'>
<input name='comment[]' type='text' id='comment' value='" .$row['comment']. "'>
</td>
<td align='center'>
<input name='stock[]' type='text' id='stock' value='" .$row['stock']. "'>
</td>
</tr>";
}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
echo "
</table>
<input type='submit' name='Submit' value='Update'>
<input type='submit' name='del' value='Delete'>
</form>";



// Check if button name "Submit" is active, do this 
if(isset($_POST['Submit'])){
    $department= $_POST['department'];
    $productname = $_POST['productname'];
    $productnum = $_POST['productnum'];
    $maker = $_POST['maker'];
    $cost = $_POST['cost'];
    $per = $_POST['per'];   
    $importfee = $_POST['importfee'];   
    $prepcost = $_POST['prepcost']; 
    $MOQ= $_POST['MOQ'];
    $weight = $_POST['weight'];
    $sizeT = $_POST['sizeT'];   
    $category = $_POST['category'];
    $page = $_POST['page'];
    $specs = $_POST['specs'];   
    $comment = $_POST['comment'];   
    $stock = $_POST['stock'];       
for($i=0;$i<$count;$i++){
$sql1 = "UPDATE $tbl_name SET 
department='".$department[$i]."', productname='".$productname[$i]."', productnum='".$productnum[$i]."'
WHERE id='".$id[$i]."'";
$result1 = $conn->query($sql1);
}
for($i=0;$i<$count;$i++){
$sql1 = "UPDATE $tbl_name SET 
maker='".$maker[$i]."', cost='".$cost[$i]."', per='".$per[$i]."', importfee='".$importfee[$i]."', prepcost='".$prepcost[$i]."'
WHERE id='".$id[$i]."'";
$result1 = $conn->query($sql1);
}
for($i=0;$i<$count;$i++){
$sql1 = "UPDATE $tbl_name SET 
MOQ='".$MOQ[$i]."', weight='".$weight[$i]."'
WHERE id='".$id[$i]."'";
$result1 = $conn->query($sql1);
}
for($i=0;$i<$count;$i++){
$sql1 = "UPDATE $tbl_name SET 
sizeT='".$sizeT[$i]."', category='".$category[$i]."', page='".$page[$i]."'
WHERE id='".$id[$i]."'";
$result1 = $conn->query($sql1);
}
for($i=0;$i<$count;$i++){
$sql1 = "UPDATE $tbl_name SET 
specs='".$specs[$i]."', comment='".$comment[$i]."', stock='".$stock[$i]."'
WHERE id='".$id[$i]."'";
$result1 = $conn->query($sql1);
}
echo("<script>location.href = 'MATERIALlist.php';</script>");
}


if (isset($_POST['del'])){
    $checkbox = $_POST['checkbox'];
    $count = count($checkbox);
    for($i=0;$i<$count;$i++){
        if(!empty($checkbox[$i])){ /* CHECK IF CHECKBOX IS CLICKED OR NOT */
        $id = mysqli_real_escape_string($conn,$checkbox[$i]); /* ESCAPE STRINGS */
        mysqli_query($conn,"DELETE FROM $tbl_name WHERE id = '$id'"); /* EXECUTE QUERY AND USE ' ' (apostrophe) IN YOUR VARIABLE */
        } /* END OF IF NOT EMPTY CHECKBOX */
    } /* END OF FOR LOOP */
    echo("<script>location.href = 'MATERIALlist.php';</script>");
} /* END OF ISSET DELETE */



if(isset($_POST['Add'])){
$sql = "INSERT INTO $tbl_name (department,productname,productnum,maker,cost,per,importfee,prepcost,MOQ,weight,sizeT,category,page,specs,comment,stock)
VALUES('$_POST[department_add]','$_POST[productname_add]','$_POST[productnum_add]','$_POST[maker_add]','$_POST[cost_add]','$_POST[per_add]','$_POST[importfee_add]','$_POST[prepcost_add]','$_POST[MOQ_add]','$_POST[weight_add]','$_POST[sizeT_add]','$_POST[category_add]','$_POST[page_add]','$_POST[specs_add]','$_POST[comment_add]','$_POST[stock_add]')";
if ($conn->query($sql) === TRUE) {
echo("<script>location.href = 'MATERIALlist.php';</script>");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}


$conn->close();

?> 
</body>
</html> 

Taking a suggestion from @Phil I changed my submit function to a prepared statement (maybe)... it was full of errors the first time I've corrected it and now it runs but will stop if I have to many rows in my table as before. The interesting thing is that if it updates I see echo "Updated {$stmt->affected_rows} rows" when it doesn't update there is no output at all. So it looks like the statement is not even executing?

// Check if button name "Submit" is active, do this 
if(isset($_POST['Submit'])){
    $department= $_POST['department'];
    $productname = $_POST['productname'];
    $productnum = $_POST['productnum'];
    $maker = $_POST['maker'];
    $cost = $_POST['cost'];
    $per = $_POST['per'];   
    $importfee = $_POST['importfee'];   
    $prepcost = $_POST['prepcost']; 
    $MOQ= $_POST['MOQ'];
    $weight = $_POST['weight'];
    $sizeT = $_POST['sizeT'];   
    $category = $_POST['category'];
    $page = $_POST['page'];
    $specs = $_POST['specs'];   
    $comment = $_POST['comment'];   
    $stock = $_POST['stock'];   

for($i=0;$i<$count;$i++){

$sql = "UPDATE $tbl_name SET 
department=?,productname=?,productnum=?,maker=?,cost=?,per=?,importfee=?,prepcost=?,MOQ=?,weight=?,sizeT=?,
category=?,page=?,specs=?,comment=?,stock=? 
WHERE id='".$id[$i]."'";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ssssssssssssssss',     $department[$i], $productname[$i], $productnum[$i], $maker[$i], $cost[$i] ,$per[$i] ,$importfee[$i],  
$prepcost[$i] ,$MOQ[$i], $weight[$i] ,$sizeT[$i] ,$category[$i] ,$page[$i], $specs[$i], $comment[$i], $stock[$i]);

$stmt->execute();
if ($stmt->errno) {
  echo "FAILURE!!! " . $stmt->error;
}
else echo "Updated {$stmt->affected_rows} rows";

}
$stmt->close();
}

Upvotes: 1

Views: 378

Answers (2)

mochaMame
mochaMame

Reputation: 57

This was a php.ini issue. Since I was trying to update everything at the same time the POST limit was reached:

; How many GET/POST/COOKIE input variables may be accepted
max_input_vars = 2500

147 rows x 17 columns = 2499 POST's so 148 rows was not accepted. Increasing the limit was enough to solve the issue.

Upvotes: 0

panofish
panofish

Reputation: 7889

I don't see any commit statement for your insert. The maximum inserts is dependent on your packet size when compared to your insert record size, but you could simply commit after insert in most cases. A commit statement gives the ability to rollback inserts if you need to do a logical group of inserts or updates to your tables.

Upvotes: 1

Related Questions