I have two columns:
stock = string "yes" or "no"
And the other column with amount:
stock_c = value 0 to 100 (limit 100)
I 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 I was thinking about the question to put as a supposed example or "almost total" as a guide for the solution.
In summary:
Yes $stock_c = 0
| Update or Login$stock = "No";
Yes $stock_c =>1
| Update or Login$stock = "Si";
Something to add:
The user or customer will have a <input>
number for the amount to add.
Here you should have a limit set according to $stock_c
And if you $stock_c = 0
disable the<input>
Code that for 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>
I got this code by asking: Check stock and add classes Thanks to @DevJoel .
You can single
update
and set both columns at the same time.For example: