I want to create stock management system in my website using php. It sounds like 'balancing' stock in database with amount of items that will purchased. If the item's amount is more than the item's stock, the website should displaying an alert. So, the amount of item cannot be more than the item's stock in database.
Here's my code :
$stock = array();
foreach ($_SESSION['cart'] as $k => $v) {
$sql = mysql_query("SELECT book_stock FROM book WHERE book_title='$k'");
while ($row = mysql_fetch_assoc($sql)) {
$stock[] = $row['book_stock'];
}
if ($stock < $v) {
echo "Stock less than the amount";
}
else {
echo "Stock sufficient";
}
}
The $k variable store the item's name in the session cart. And the $v variable store the item's amount in the transaction.
The item's amount will be increased if the visitor click 'Increase' button.
But if i increase the item's amount, the if and else logic like "isn't working". No matter how much the item's amount is added, the if and else logic keep print "Stock Sufficient".
Can anyone help me to solve this? Thanks in advance :)
Edit :
The variable in original code is in indonesian. So when i'm typing this post, i'm editing the variables too here. So if the variables is different, i'm just forget to rename it. So i edit the script again :)
$sql = mysql_query("SELECT book_stock FROM ooku WHERE book_title='$k'");
I am assuming that your query will return unique book (single book) stock detail, you have to use $stok instead of $stok[] Array
while ($row = mysql_fetch_assoc($sql)) {
$stok = $row['book_stock'];
}
Related
I am trying to allow users to add destinations to their favourites page, similar to adding products to a shopping cart. I am doing this by saving the destinations to a session array and then adding this session array onto the end of an SQL SELECT statment to print the destinations. Although the code I have written only allows me to add one destination to favourites. Can anyone point my in the right direction.
if(empty($_SESSION['cart'])){
$_SESSION['cart'] = array();
} else {
array_push($_SESSION['cart'], $_GET['theid']);
// HOW TO LOOP THROUGH SESSION CART
$str = implode(',', $_SESSION['cart']) ;
$query = "SELECT * FROM Destinations WHERE DestID IN ($str)" ;
$result = mysqli_query($connection, $query);
while($row=mysqli_fetch_assoc($result)){
echo $row['DestName'].' '.$row['DestRating'].
' <img src="Images/'.$row['DestImage'].'" /> <br/>';
}
}
I have purchase order form where I am storing product details in session i.e product name, quantity, rate etc. and saving it to database. And at the time of editing purchase order, getting product details from database and storing it in session and appending new product details to session array. It is working fine. But When I submit form then Only last record i.e. newly added record is inserted in database.
here you can assume that I am editing shopping cart after placing order.
My problem is that add,delete items in cart after placing order is logically correct or not? And if yes then How?
This is my code for save and update purchase order.
function save(&$purchase_data,$purchase_id)
{
$success=false;
//Run these queries as a transaction, we want to make sure we do all or nothing
$this->db->trans_start();
if($purchase_data)
{
if (!$purchase_id or !$this->exists($purchase_id))
{
//$purchase_data['purchase_id'] = $purchase_id = $purchase_data['purchase_id'];
$success = $this->db->insert('purchase_order',$purchase_data);
$post_array['cart']=$this->session->userdata('data');
/*print_r($post_array);
exit;*/
$purchase_id=$this->db->insert_id();
$i=0;
foreach($post_array['cart'] as $item)
{
//echo "<pre>"; print_r($item); echo "</pre>";
$query = $this->db->query("SELECT name FROM phppos_items WHERE item_id='".$item['product_id']."'");
foreach ($query->result() as $row)
{
$product_name=$row->name;
}
$product_id=$item['product_id'];
$quantity=$item['quantity'];
$unit=$item['unit'];
$unit_rate=$item['unit_rate'];
$query = $this->db->query("insert into phppos_productdetails(product_id,product_name,quantity,unit,unit_rate,purchase_id) values ('$product_id','$product_name','$quantity','$unit','$unit_rate','$purchase_id')");
$i++;
}
}
else
{
$this->db->where('purchase_id', $purchase_id);
$success = $this->db->update('purchase_order',$purchase_data);
//$this->session->set_userdata('sess_products');
$post_array['cart']=$this->session->userdata('sess_products');
$i=0;
foreach($post_array['cart'] as $item)
{
echo "<pre>"; print_r($item); echo "</pre>";
$query = $this->db->query("SELECT name FROM phppos_items WHERE item_id='".$item['product_id']."'");
foreach ($query->result() as $row)
{
$product_name=$row->name;
}
$product_id=$item['product_id'];
$quantity=$item['quantity'];
$unit=$item['unit'];
$unit_rate=$item['unit_rate'];
$query = $this->db->query("update phppos_productdetails set product_id='$product_id',product_name='$product_name',quantity='$quantity',unit='$unit',unit_rate='$unit_rate' where purchase_id='$purchase_id'");
$i++;
}
}
}
$this->db->trans_complete();
return $success;
}
you have put (where purchase_id='$purchase_id'")
so when the loop executes last time it updates all records with purchase_id as $purchase_id
Your query:
$query = $this->db->query("update phppos_productdetails set product_name='$product_name',product_id='$product_id',quantity='$quantity',unit='$unit',unit_rate='$unit_rate' where purchase_id='$purchase_id'");"
By seeing above query the parameters used in this query like $product_name is out of inner foreach scope.Hence it takes only last record and insert it into database.
If you place this query in inner foreach loop then it will inserts appropriate records as per you required.
Hope this will helps you.
I'm quite new to php and mysql so forgive me if I'm doing this completely wrong. I am making a printing balance application and the code below is a part of it.
$command="SELECT itemname FROM items";
$results = mysql_query($command);
while($row = mysql_fetch_assoc($results))
{
foreach ($row as $key => $value) {
print "<input type='radio' name='itemtype' value='$value'>".$value."</input><br />";
}
}
This here is supposedly the price printing form where the user chooses between SHORT BOND PAPER and LONG BOND PAPER (the column itemname from items). The options appear as radio buttons. It works but now I'm stuck with the issue of being able to fetch the price as inserted in their respective rows. Since the itemname and their price are all user-inputted into the database, I'm not supposed to declare their specific price into the code itself, and should be able to retrieve it from the database. I want to be able to get the prices based on the item chosen by the user once they click submit, because I'd need to do this to compute for the price of printing when multiplied with the number of pages later.
I think it's definitely possible but I'm not quite sure how. Theoretically, it would be along the lines of SELECT itemprice FROM items WHERE itemname = $value but ha, I don't think it works that way.
solution edit: here's the complete solution for reference. $compute is just a sample to test if it works while 5 is a sample number of pages that would be entered.
if ($command = mysql_query("SELECT `itemprice` FROM `items` WHERE `itemname` LIKE '" . mysql_escape_string($_POST['itemtype']) . "'"))
{
while($row = mysql_fetch_assoc($command))
{
$compute = $row['itemprice'] * 5;
echo $compute;
}
}
else
{
echo (mysql_error ());
}
It would be something like that indeed.
SELECT itemprice FROM items WHERE itemname = $_POST['itemtype']
assuming that itemprice is the actuial colum of the price. HOwever, doing it like this, makes your code vulnerable to mysql injections. So before you contine, consider reading up a little.
I am trying to pass data from a product page into a shopping cart page using an array. There are multiple attributes that the viewcart.php will receive from the previous page (price, link, title, and retailer). I’d like to save them all using an array. For each additional item a customer adds to the shopping cart, I’m trying to get a counter variable ($i) to increment an array $_SESSION[‘cart’][$i][‘attribute’]. How do I do this?
I’m not sure this is the right way to add new products to the cart. In the end, I’d like a way to be able to display all the products in the cart using a for loop. This is the start I have so far on the shopping cart script:
<?php
// The shopping cart needs sessions, so start one
session_start();
#$link = $_GET['link'];
$price = $_GET['price'];
$title = $_GET['title'];
$retailer = $_GET['retailer'];
if($link) {
//new item selected
if(!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array();
$_SESSION['items'] = 0;
$_SESSION['total_price'] ='0.00';
}
if(isset($_SESSION['cart'][$link])) {
$_SESSION['cart'][$link]++;
} else {
$_SESSION['cart'][$link] = 1;
}
}
if(($_SESSION['cart']) && (array_count_values($_SESSION['cart']))) {
echo " in your cart and we're working to display them";
}
else {
echo "<p>There are no items in your cart</p><hr/>";
}
?>
This is the for loop that I’m thinking I could use. I’m looking for some way to display all the items in the array.
for ($x=0; $x<=$i; $i++)
{
echo "The price is " . $_SESSION['cart'][$x][price] . " Retailer is " . $_SESSION['cart'][$x] [retailer] . "<br>";
}
The easiest way to do this is by creating a temp_cart table in your database.. in which you should store the items that user adds to their cart.. Then on checkout page.. you can simply display them using select query. In that way.. it will be easier for you to allow the user to edit their cart on viewcart.php page.
You could:
Have another session variable with the counter and increment it each add?
Or
$i= Count($_SESSION['cart'])+1;
Or
Don't specify an index at all:
$tmp["cart"]["retailer"] = "123";
Etc etc..
$_SESSION['cart'][] = $tmp["cart"];
I would be storing the products into a database if I was you..
I don't like the way you are doing this. Session management is very simple for shopping carts. Keep your session as lite as possible. Storing price in the session is very bad way, as it can be easily manipulated.
Here is a simple example of what you can use.
if(isset($_SESSION['items'][$_GET['item_id']])) {
$_SESSION['items'][$_GET['item_id']]++; //add one to the total count
} else {
$_SESSION['items'][$_GET['item_id']] = 1; //If added for the first time
}
Now process it
foreach($_SESSION['items'] as $id => $count) {
echo $id; // product id
echo $count; // product count
}
P.S: Remember to sanitize the input. I have omitted that
I'm trying to add product into add cart. Please tell me what is the best way.
My mess code is.
if(isset($_SESSION['id'])) {
echo "IF part";
$_SESSION['id'] = $_SESSION['id'] + $_SESSION['id'];
$k = $_SESSION['id'];
// store session data
$_SESSION[$k]['product_name']=$_REQUEST['product_name_value'];
$_SESSION[$k]['product_price']=$_REQUEST['product_price_value'];
$_SESSION[$k]['shop_name']=$_REQUEST['shop_name_value'];
$_SESSION[$k]['Quantity']=$_REQUEST['selquantity'];
$_SESSION[$k]['color']=$_REQUEST['txtcolor'];
$_SESSION[$k]['Size']=$_REQUEST['selsize'];
$_SESSION[$k]['Product_Type']=$_REQUEST['selproducttype'];
$_SESSION[$k]['Remarks']=$_REQUEST['Remarks'];
$_SESSION[$k]['final_price']=$_REQUEST['final_price_value'];
$_SESSION[$k]['txturl']=$_REQUEST['txturl'];
}else {
echo "else part";
$_SESSION['id'] = 1;
// store session data
$_SESSION[$k]['product_name']=$_REQUEST['product_name_value'];
$_SESSION[$k]['product_price']=$_REQUEST['product_price_value'];
$_SESSION[$k]['shop_name']=$_REQUEST['shop_name_value'];
$_SESSION[$k]['Quantity']=$_REQUEST['selquantity'];
$_SESSION[$k]['color']=$_REQUEST['txtcolor'];
$_SESSION[$k]['Size']=$_REQUEST['selsize'];
$_SESSION[$k]['Product_Type']=$_REQUEST['selproducttype'];
$_SESSION[$k]['Remarks']=$_REQUEST['Remarks'];
$_SESSION[$k]['final_price']=$_REQUEST['final_price_value'];
$_SESSION[$k]['txturl']=$_REQUEST['txturl'];
}
I'm trying to add these product details into array.
thanks
Don't repeat yourself ;) means if you have two lines of code that are identical check if you really need to write it twice!
if(!isset($_SESSION['cart'])) {
// create cart
$_SESSION['cart'] = array();
}
// create item
$item = array();
// fill item
$item['product_name']=$_REQUEST['product_name_value'];
$item['product_price']=$_REQUEST['product_price_value'];
$item['shop_name']=$_REQUEST['shop_name_value'];
$item['Quantity']=$_REQUEST['selquantity'];
$item['color']=$_REQUEST['txtcolor'];
$item['Size']=$_REQUEST['selsize'];
$item['Product_Type']=$_REQUEST['selproducttype'];
$item['Remarks']=$_REQUEST['Remarks'];
$item['final_price']=$_REQUEST['final_price_value'];
$item['txturl']=$_REQUEST['txturl'];
// add item to cart
$_SESSION['cart'][] = $item;
You should first do some factorization on your code with creating a function to add the data to the $_SESSION.
Then, it's impossible to give you an answer, there's no "best way", there's only a way that suits your need and you're the only one which can find it.
See below.
$sql = "INSERT INTO ordertb (time, cust_id, prod_id, quantity, pmt_mode, city, delivery_state,amtval) VALUES ( '".date("Y-m-d H:i:s")."','".$idval1."','".$item["product_code"]."','".$item["qty"]."','".$_SESSION['spmt1']."','".$_SESSION['scity']."','1','".$item["subtotal"]."')";