cpt-crunchy cpt-crunchy - 1 month ago 8
PHP Question

change cell value on button submit

Ok I'm being forced to bring this feature back up by my higher-ups. However this time the data will be updated without AJAX POST method.

In my table I have a minus button and a plus button. Each is acting as a submit button and when a value is added into a different input field the count will change based on which button was clicked, e.g.( +10 if plus button clicked when 10 is in input field)

My table isn't updating and can't get anything to echo out as far as errors go.
Thanks for your help.

<?php
if(isset($_GET['stock_id'])) {
$the_stock_id = mysqli_real_escape_string($connection, $_GET['stock_id']);
}
$query = 'SELECT stock_id, sku_number, category, description, price, in_stock ';
$query .= 'FROM parts_stock AS a JOIN items AS b ON a.stock_id = b.s_id ';
$query .= "WHERE a.stock_id =".$the_stock_id;

$edit_sku = mysqli_query($connection, $query);

while($row = mysqli_fetch_assoc($edit_sku)) {
$stock_id = $row['stock_id'];
$sku = $row['sku_number'];
$category = $row['category'];
$desc = $row['description'];
$price = $row['price'];
$stock = $row['in_stock'];
}

if(isset($_POST['update_stock'])) {
$price = $_POST['price'];
$mod_stock = $_POST['mod_stock'];
if(isset($_POST['rem_stock'])) {
$stock -= $mod_stock;
echo $stock;
}elseif(isset($_POST['add_stock'])) {
$stock += $mod_stock;
echo $stock;
}
$query = "UPDATE parts_stock, items SET ";
$query .= "price = '$price', ";
$query .= "in_stock = '$stock' ";
$query .= "WHERE stock_id ='$the_stock_id' ";

$update_stock = mysqli_query($connection, $query);
confirmQuery($update_stock);

$alert = <<<DELIMETER
<div class='alert alert-warning alert-dismissible fade in' role='alert'>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<strong>Inventory Updated!</strong> <a href='inventory.php?view_all_inventory'>View All Inventory</a>
</div>
DELIMETER;
}
?>
<div class="col-xs-12 col-sm-12 col-md-12">
<h2>Edit Inventory Item</h2>
<?php echo $alert; ?>
<hr>
<table class="table table-bordered table-responsive table-striped">
<thead class="thead-inverse">
<tr class="alert alert-success">
<th>SKU #</th>
<th>Category</th>
<th>Description</th>
<th>Price</th>
<th>Current Stock</th>
<th>+ / - Stock</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<form role='form' action="" method="POST">
<td><input value="<?php echo $sku; ?>" type="text" class="form-control" name="sku_number" readonly ></td>
<td><input value="<?php echo $category; ?>" type="text" class="form-control" name="category" readonly></td>
<td><input value="<?php echo $desc; ?>" type="text" class="form-control" name="description" readonly></td>
<td><input value="<?php echo $price; ?>" type="text" class="form-control" name="price" ></td>
<td><input value="<?php echo $stock; ?>" type="text" class="form-control" name="in_stock" readonly ></td>
<td><input value="" type="text" class="form-control" name="mod_stock"> </td>
<td class='btn-group'>
<button class='btn btn-danger btn-sm' type='submit' name='update_stock' value='rem_stock'><i class='glyphicon glyphicon-minus'></i></button>
<buton class='btn btn-success btn-sm' type='submit' name='update_stock' value='add_stock'><i class='glyphicon glyphicon-plus'></i></buton>
</td>
</form>
</tr>
</tbody>
</table>

</div>

Answer

There are few flaws in your code, such as:

  • Look at the following line,

    <buton class=...</i></buton>
     ^^^^^                ^^^^^
    

    It should be button, not buton

  • if(isset($_POST['rem_stock'])) {... and }elseif(isset($_POST['add_stock'])) { are wrong. The correct if conditions would be,

    if($_POST['update_stock'] == 'rem_stock') { ...
    

    and

    }elseif($_POST['update_stock'] == 'add_stock'){ ...
    
  • You're getting $the_stock_id from $_GET['stock_id'], so once you submit the form, you won't be getting any $_GET['stock_id'] to store it in $the_stock_id variable. So, make use of $_POST['in_stock'] and $_POST['sku_number'] after your form submission.

So your PHP code should be like this:

// your code

if(isset($_POST['update_stock'])) {
    $price       = $_POST['price'];
    $mod_stock   = $_POST['mod_stock'];
    $stock = $_POST['in_stock'];
    $the_stock_id = $_POST['sku_number'];

    if($_POST['update_stock'] == 'rem_stock') {
        $stock -= $mod_stock;
    }elseif($_POST['update_stock'] == 'add_stock') {
        $stock += $mod_stock;    
    }
    $query = "UPDATE parts_stock, items SET ";
    $query .= "price = '$price', ";    
    $query .= "in_stock = '$stock' ";
    $query .= "WHERE stock_id ='$the_stock_id'";

    $update_stock = mysqli_query($connection, $query);
    confirmQuery($update_stock);

  $alert = <<<DELIMETER
    <div class='alert alert-warning alert-dismissible fade in' role='alert'>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <strong>Inventory Updated!</strong> <a href='inventory.php?view_all_inventory'>View All Inventory</a>
    </div>
DELIMETER;
}

and your submit button should be like this:

<button class='btn btn-success btn-sm' type='submit' name='update_stock' value='add_stock'><i class='glyphicon glyphicon-plus'></i></button>

Note(s):

  • Always turn on error reporting, add these two lines at the very top of your PHP scripts to debug any syntax related issues.

    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
  • Learn about prepared statements because right now your query is susceptible to SQL injection. Also see how you can prevent SQL injection in PHP.