Enter $ value to a field and depending on the value, modify another field

0

I have two columns:

  

stock = string "yes" or "no"

And the other column with amount:

  

stock_c = value 0 to 100 (limit 100)

Use utf8_general_ci collation and the columns are in the products

table

What I want to achieve is:

$stock_c = $_POST['cantidad']; // 0 a 100
mysqli_query($cnx, "UPDATE productos SET stock_c = $stock_c");

$query = "SELECT * FROM productos WHERE productos.stock_c = $stock_c";
mysqli_query($cnx,$query);
// en este caso verificariamos si stock es = o mayor a 1.
if($stock_c => 1) {
 mysqli_query($cnx,"UPDATE productos SET stock='si' WHERE id_producto=$id_producto");
}
else {
 mysqli_query($cnx,"UPDATE productos SET stock='no' WHERE id_producto=$id_producto");
    }
// si lo es, actualiza stock = "Si" caso contrario ="No"

Surely something is wrong in the code, I just structured it while thinking the question to put as a supposed example or "almost total" as a guide for the solution.

In summary:

If $stock_c = 0 | Update or Enter in $stock = "No";

If $stock_c =>1 | Update or Enter in $stock = "Si";

Something to add:

The user or client will have a% co_of numeric% for the amount to add.

Here you should have a limit set to $ stock_c

And if <input> deactivate the $stock_c = 0

Code that at the moment I use to reflect $ stock

  <div class="stock"><p>Stock: 
        <?php
        if (strcasecmp($columnas['stock'], 'si') === 0) {
        echo '<span class="product-stock st-si">' .$columnas['stock'] ."</span>";
      }
      else{
      echo '<span class="product-stock st-no">' .$columnas['stock'] ."</span>";
      }
        ?></p> </div>

This code was obtained by asking: Check stock and add classes Thanks to @DevJoel .

    
asked by Juan 27.01.2018 в 19:35
source

1 answer

1

You can make a single update and set both columns at the same time.
So for example:

$id_producto = 1;
$stock_c = (int) $_POST['cantidad']; // 0 a 100
if ($stock_c < 0) { $stock_c = 0; }
else if ($stock_c > 100) { $stock_c = 100; }
$stock = $stock_c > 0 ? 'si' : 'no';

// Update
$sql = "UPDATE productos 
   SET stock_c = $stock_c,
       $stock = '$stock'
   WHERE id_producto = $id_producto";
mysqli_query($cnx, $sql);
    
answered by 27.01.2018 / 20:54
source