Jump to content

I-AM-OBODO

Members
  • Content Count

    394
  • Joined

  • Last visited

Posts posted by I-AM-OBODO

  1. On 6/22/2019 at 10:13 PM, Barand said:

    I think the problem is this line ...

    
    $update->bindParam(':item_qty', $item_q - $_POST['item_qty'][$k]);

    ... where you are attempting to bind an expression and not a variable.

    Try changing the query (and you also need to bind the category)

    
        $update = $pdo->prepare("
                            UPDATE stocks
                            SET item_qty = item_qty - :item_qty                 -- changed line
                            WHERE category = :category
                            AND  product_name = :product_name
                            AND item_size = :item_size
                            AND item_type = :item_type
                            ");
        foreach($_POST['category'] as $k => $category){
            $update->execute( [ 
                                ':item_qty'    => $_POST['item_qty'][$k],
                                ':category'    => $category,
                                ':product_name'=> $_POST['product_name'][$k],
                                ':item_type'   => $_POST['item_type'][$k],
                                ':item_size'   => $_POST['item_size'][$k]
                              ] );  
        }

     

    Thank you so very much!

  2. 10 hours ago, Barand said:

    Process the $_POST data in a very similar method to that used for the shopping table inserts.

    I don't know if you are trying that as , once again, you have left us to guess what the code is that produced your error messages.

    This is what i did and the error: Fatal error: Cannot pass parameter 2 by reference

    	$update = $pdo->prepare("
    UPDATE stocks
    SET item_qty = :item_qty
    WHERE category = :category
    AND  product_name = :product_name
    AND item_size = :item_size
    AND item_type = :item_type
    ");
    	foreach($_POST['category'] as $k => $category){
            $update->bindParam(':item_qty', $item_q - $_POST['item_qty'][$k]);
            $update->bindParam(':product_name', $_POST['product_name'][$k]);
            $update->bindParam(':item_type', $_POST['item_type'][$k]);
            $update->bindParam(':item_size', $_POST['item_size'][$k]);
            $update->execute();
        }
    	
  3. On 6/18/2019 at 7:33 AM, Barand said:

    If that code is in the same page as your previous code then remember those $_POST items are now arrays.

    Once again you can can throw away the form and update stocks with a single query...

    
    UPDATE stocks s
           JOIN temp_shopping t 
                USING (product_name, item_type, item_size, category)
    SET s.item_qty = s.item_qty - t.item_qty
    WHERE t.trans_ref = ?

    (This assumes you have checks into your application to ensure you have sufficient stock when making a sale.)

    And you have another table that requires normalizing

    
                                        +------------------+
                                        |  stocks          |
                                        +------------------+
                              +--------<| product_id       |
                              |         | item_qty         |
                              |         +------------------+
                              |
    +-----------------+       |
    |  product        |       |
    +-----------------+       |
    | product_id      |-------+
    | product_name    |       |
    | category        |       |         +------------------+
    | size            |       |         |  shopping        |
    | type            |       |         +------------------+
    | price           |       |         |  shopping_id     |
    +-----------------+       |         |  trans_ref       |
                              +--------<|  product_id      |
                                        |  qty             |
                                        |  date            |
                                        +------------------+

     

    Good day. I still havent been able to update the stocks. I have tried so many things and encountered various errors:

    Cannot pass parameter 2 by reference
    Fatal error: Cannot pass parameter 2 by reference

     

    Please can you help me with the update stuff?

    Thanks

  4. 13 minutes ago, Barand said:

    If that code is in the same page as your previous code then remember those $_POST items are now arrays.

    Once again you can can throw away the form and update stocks with a single query...

    
    UPDATE stocks s
           JOIN temp_shopping t 
                USING (product_name, item_type, item_size, category)
    SET s.item_qty = s.item_qty - t.item_qty
    WHERE t.trans_ref = ?

    (This assumes you have checks into your application to ensure you have sufficient stock when making a sale.)

    And you have another table that requires normalizing

    
                                        +------------------+
                                        |  stocks          |
                                        +------------------+
                              +--------<| product_id       |
                              |         | item_qty         |
                              |         +------------------+
                              |
    +-----------------+       |
    |  product        |       |
    +-----------------+       |
    | product_id      |-------+
    | product_name    |       |
    | category        |       |         +------------------+
    | size            |       |         |  shopping        |
    | type            |       |         +------------------+
    | price           |       |         |  shopping_id     |
    +-----------------+       |         |  trans_ref       |
                              +--------<|  product_id      |
                                        |  qty             |
                                        |  date            |
                                        +------------------+

     

    Thanks. but am working on the form type version. Will look at the other version later. thanks

  5. On 6/16/2019 at 7:45 PM, Barand said:

    You might have read it but you totally ignored it.

    Good bye.

    Hello,
    I tried doing some deduction from another table but its giving me Array to string conversion

    	$update = $pdo->prepare("
    UPDATE stocks
    SET item_qty = item_qty - '$_POST[item_qty]'
    WHERE category = '$category'
    AND product_name = '$_POST[product_name]'
    AND item_type = '$_POST[item_type]'
    AND item_size = '$_POST[item_size]'
    ");
    $update->execute();
    	
  6. On 6/16/2019 at 1:45 PM, Barand said:

    Just a comment on your tables.

    Data items like product_name, category, price are properties of the product and should, therefore, only be in the product table. The shopping table should hold just the product_id. (Read up on "data normalization")

    
    +-----------------+
    |  product        |
    +-----------------+
    | product_id      |-------+
    | product_name    |       |
    | category        |       |         +------------------+
    | size            |       |         |  shopping        |
    | price           |       |         +------------------+
    +-----------------+       |         |  shopping_id     |
                              |         |  trans_ref       |
                              +--------<|  product_id      |
                                        |  qty             |
                                        |  date            |
                                        +------------------+
    

     

    Thank you very much. All seem to be working well for now. I had to delete the table and start all over again. The form version is working well.

  7. 3 hours ago, Barand said:

    Then you have a primary key or unique key constraint on "trans_id" in your shopping table, which seems an odd thing to do as it appears that you are extracting groups of product items with the same trans_id ( $_SESSION['trans_id'] ) from your temporary table to insert into the shopping table, all with that same trans_id..

    I cant seem to locate trans_id and the error is reporting as duplicate should be trans_ref and not trans_id. however, the trans_ref is the reference for the transaction. since we can have multiple items in a single transaction/session.

    thanks

  8. 56 minutes ago, Barand said:

    You might have read it but you totally ignored it.

    Good bye.

    oops! oversight. this is what i got:

     

    Array
    (
        [category] => Array
            (
                [0] => Beverages
                [1] => Milk
                [2] => Cereals
                [3] => Beverages
                [4] => Milk
            )

        [product_name] => Array
            (
                [0] => Bournvita
                [1] => Dano
                [2] => Golden morn
                [3] => Milo
                [4] => Peak
            )

        [item_type] => Array
            (
                [0] => Refill
                [1] => Evap
                [2] => NONE
                [3] => Tin
                [4] => Evap
            )

        [item_size] => Array
            (
                [0] => 1
                [1] => 160
                [2] => 1
                [3] => 500
                [4] => 380
            )

        [item_qty] => Array
            (
                [0] => 4
                [1] => 2
                [2] => 3
                [3] => 2
                [4] => 3
            )

        [item_price] => Array
            (
                [0] => 1800
                [1] => 1400
                [2] => 1800
                [3] => 1700
                [4] => 1400
            )

        [price] => Array
            (
                [0] => 7200
                [1] => 2800
                [2] => 5400
                [3] => 3400
                [4] => 4200
            )

        [total_price] => ₦23,000.00
        [send] => Complete Shopping
    )

     

    and error is: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '799988' for key 'trans_id''
    but the first item is inserted into the database

     

    Thanks

  9. 13 minutes ago, Barand said:

    Read the replies.

     

    Yeah. I have read it. But this error: Warning: Invalid argument supplied for foreach()  is from your own code:

    the line number is 127:

    foreach ($_POST['category'] as $k => $category) {
  10. 7 minutes ago, Barand said:

    Can you post the output you are now getting from

    
    echo '<pre>', print_r($_POST, 1), '</pre>';

     

    Array
    (
        [category] => Milk
        [product_name] => Peak
        [item_type] => Refill
        [item_size] => 380
        [item_qty] => 2
        [item_price] => 3500
        [price] => 7000
        [total_price] => ₦26,300.00
        [send] => Complete Shopping
    )
  11. 7 hours ago, Barand said:

    Your problem is the input names. You have multiple items with the same name so, when posted, the last value with that name has overwritten all the previous ones. Only the final row is posted.

    You need to append "[ ]" to your input names so they are posted in arrays (EG name="category[]" ) . To process ...

    
    if ($_SERVER['REQUEST_METHOD']=='POST') {
        $stmt = $pdo->prepare("INSERT INTO shopping(
                                trans_ref,
                                category,
                                product_name,
                                item_type,
                                item_size,
                                item_qty,
                                item_price,
                                price,
                                date
                                )
                                VALUES(
                                :trans_ref,
                                :category,
                                :product_name,
                                :item_type,
                                :item_size,
                                :item_qty,
                                :item_price,
                                :price,
                                CURDATE()
                                )
                                ");
        foreach ($_POST['category'] as $k => $category) {
            $record = [ 'trans_ref' => $_SESSION['trans_ref'],
                        'category'  => $category,
                        'product_name' => $_POST['product_name'][$k],
                        'item_type' => $_POST['item_type'][$k],
                        'item_size' => $_POST['item_size'][$k],
                        'item_qty' => $_POST['item_qty'][$k],
                        'item_price' => $_POST['item_price'][$k],
                        'price' => $_POST['item_price'][$k] * $_POST['item_qty'][$k],
                      ];
            $stmt->execute($record);
        }
    }

    That should fix your code. Now to fix your method.

    Firstly, don't store derived values. You can calculate the total price when required (SELECT qty * price as totalprice, ...)

    Secondly, a single INSERT..SELECT SQL query will replace all the above code (two if you count the check that data exists to be transferred). Here's how I would do it (no form required)

    
    $res = $pdo->prepare("SELECT SUM(item_qty * item_price) 
                          FROM temp_shopping
                          WHERE trans_ref = ?
                          ");
    $res->execute([$_SESSION['trans_ref']]);
    $total_price = $res->fetchColumn();
    
    if ($total_price == 0) {
        echo "NO RECORDS TO TRANSFER<br>";
    }
    else {
        $res = $pdo->prepare("INSERT INTO shopping(
                                trans_ref,
                                category,
                                product_name,
                                item_type,
                                item_size,
                                item_qty,
                                item_price,
                                date
                                )
                                SELECT
                                    trans_ref,
                                    category,
                                    product_name,
                                    item_type,
                                    item_size,
                                    item_qty,
                                    item_price,
                                    CURDATE()
                                FROM temp_shopping
                                WHERE trans_ref = ?    
                                ");
        $res->execute([$_SESSION['trans_ref']]);
        echo "Total value transferred : " . number_format($total_price,2) . '<br>;';
        
    }                     

     

    Thanks so very much. Will try out your second option and the table normalization once i get this up and running. But i have having error on your first code: Warning: Invalid argument supplied for foreach()

     

    Thanks

  12. 13 hours ago, Barand said:

    Why are you using an intermediate form to move data from one table to another?

    Is this a guessing game where we have to guess how the form sending the above POST data is getting its data from your "temporal" table? - Sorry, I don't play games.

    Sorry. And thanks for your time. Its no guessing game. I omitted the where the data is coming from. I used a form cos i feel it will work for the purpose, cos i tried to use SELECT, INSERT but since the first table and destination table does not have the same number of rows, it didnt work or I couldnt get it to work and so i decided to use form. The previous code is the POST. Below is the rest of the code

    $stmt = $pdo->prepare("SELECT * FROM temp_shopping");
    $stmt->execute();
    $num_rows = $stmt->rowCount();
    	if($num_rows < 1){
        
    echo '<div class="alert bg-danger text-center">NO RECORD FOUND</div>';
    }else{
            
    	$total_price = 0;
    $stmt = $pdo->query("
    SELECT *
    FROM
    temp_shopping
    WHERE trans_ref = '$_SESSION[trans_ref]'
    ORDER BY product_name ASC
    "
    );
    	echo "<form action='' method='post'>";
    echo "<table width='100%'>";
    echo "<tr>
    <th>Category</th>
    <th>Product Name</th>
    <th>Product Type</th>
    <th>Size</th>
    <th>Qty</th>
    <th>Unit Price</th>
    <th>Total</th>
    </tr>";
    // keeps getting the next row until there are no more to get
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        // Print out the contents of each row into a table
    echo "<tr><td>";
    echo "<input type='text' class='form-control' name='category' value='".$row['category']."' readonly /> ";
    echo "</td><td>";
    echo "<input type='text' class='form-control' name='product_name' value='".$row['product_name']."' readonly /> ";
    echo "</td><td>";
    echo "<input type='text' class='form-control' name='item_type' size='7' value='".$row['item_type']."' readonly /> ";
    echo "</td><td>";
    echo "<input type='text' class='form-control' name='item_size' size='5' value='".$row['item_size']."' readonly /> ";
    echo "</td><td>";
    echo "<input type='text' class='form-control' name='item_qty' size='5' value='".$row['item_qty']."' readonly /> ";
    echo "</td><td>";
    echo "<input type='text' class='form-control' name='item_price' size='7' value='".$row['item_price']."' readonly /> ";
    echo "</td><td>";
    echo "<input type='text' class='form-control' name='price' size='7' value='".$row['item_price'] * $row['item_qty']."' readonly /> ";
    $total_price += $row['item_price'] * $row['item_qty'];
    echo "</td></tr>";
    	}
    echo "<tr>";
    echo "<td colspan='6' align='right'>";
    echo "<strong>TOTAL PRICE :</strong>";
    echo "</td>";
    echo "<td align='left'>";
    echo "<strong><input type='text' class='form-control' name='total_price' size='7' value='"."₦".number_format($total_price,2)."' readonly /> ";
    echo "</td>";
    echo "</tr>";
    echo "</table>";        
    }
    	
  13. 13 hours ago, Barand said:

    What does the data being posted to the form look like. IE What does this output...

    
    echo '<pre>', print_r($_POST, 1), '</pre>';

    ?

    The data on the form is derived from(a while loop) in a temporal table in the database. From there the data and the summation(total) is to be stored the the actual table. But the problem am having is that only the last row is being inserted into the actual table.

    Thanks

  14. Hi guys,
    I have a table that i want all the entries to be inserted all at once.
    Mine is just inserting only one of the item, how can i get it to work? I also attached a sample table.

    Thanks

    	if(isset($_POST['send'])){
        
        $category = $_POST['category'];
        $item_type = $_POST['item_type'];
        $item_size = $_POST['item_size'];
        $product_name = $_POST['product_name'];
        $item_qty = $_POST['item_qty'];
        $price = $_POST['price'];
        $total_price = $_POST['total_price'];
        $item_price = $_POST['item_price'];
        
    $sql = "
    INSERT INTO shopping(
    trans_ref,
    category,
    product_name,
    item_type,
    item_size,
    item_qty,
    item_price,
    price,
    date
    )
    	VALUES(
    :trans_ref,
    :category,
    :product_name,
    :item_type,
    :item_size,
    :item_qty,
    :item_price,
    :price,
    NOW()
    )
    ";
    $stmt = $pdo->prepare($sql);
    $stmt->execute(array(
    ':trans_ref' => $_SESSION['trans_ref'],
    ':category' => $category,
    ':product_name' => $product_name,
    ':item_type' => $item_type,
    ':item_size' => $item_size,
    ':item_qty' => $item_qty,
    ':item_price' => $item_price,
    ':price' => $price
    ));
    	
    if ( $stmt->rowCount()){    
        echo '<div class="alert bg-success text-center">SHOPPING COMPLETED</div>';
    }else{
        echo '<div class="alert bg-danger text-center">SHOPPING NOT COMPLETED. A PROBLE OCCURED</div>';
    }
    	
    }
    	

    sample.jpg

  15. Hi guys,
    I have a form that the reference_id is auto generated and is stored in a session. Is there a way to use only the first auto generated reference_id until the session is unset or destroyed?

    thanks

  16. 1 minute ago, Barand said:

    P.S.

    If you just want the user_name

    
    echo $_SESSION['test']['user_name'];

     

    yeah i know. but the problem is that i want to get all the values from a form, then list them just like a shopping cart where u find all the added items before continue shopping or checkouts. i guess its a multi-dimensional array. maybe am wrong though.

    thanks

  17. 6 minutes ago, Barand said:

    The usual advice I would give for this problem would be for you to echo $value within the loop so you can see what the variable contains.

    In your case you have already done this and you will have seen the values "5" and "obodo".

    As it is then blatantly obvious that $value is not an array, specifically not an array with a key of 'user_id', then it's hard to see why you even need to ask the question.

    I dont get you? for instance now, i want value of only the user_name and instead of outputting "obodo" it gives error. and you said $value is not an array?

  18. Hi guys, why am i getting this error: Illegal string offset 'user_id' 

    but when echo $value it brings the correct output.

    Thanks

    	    $user_id = 5;
        $user_name = "obodo";
        $_SESSION['test'] = array('user_id' => $user_id, 'user_name' => $user_name);
    	
        foreach( $_SESSION['test'] as $value )
        {
             
    	         echo $value['user_id'];   //give error
            /* echo $value //works  */
          
        }
         

  19. 34 minutes ago, mac_gyver said:

    do not pass the price through the form.

    you should get a minimum of data from the form, because all form data must be validated before using it. if all you are submitting is the item id and the quantity, you only have two pieces of form data that you must validate.

    the work-flow should be -

    1) user selects an item, modifies the quantity (the initial quantity field value should be 1), and submits the form.

    2) the server side processing code detects that a post method form has been submitted, validates the input data, and adds the item id (as the array index) and quantity to a cart session array variable.

    3) to display the cart, get the item id's (see array_keys()), query the database to get all the item ids, names, descriptions, and prices for the items in the cart. loop over the retrieved data to display the cart, getting the corresponding quantity from the cart session array variable using the item id, performing any total calculation and display as needed.

    Thanks

  20. 19 minutes ago, mac_gyver said:

    if the point of retrieving the price is to calculate the total before you add the item to the cart, there's no real need to do this, assuming that after each item is added to the cart you are displaying the contents of cart. you would retrieve the item name, description, price, and would calculate the total when displaying the contents of the cart. You can just display the price next to the item name in the option list if the sales rep needs access to the unit price.

    Am not really after the calculation, what I'm concerned about is retrieving the value of price based on selected products so that the price shows up on the price input area for further processing. If I get this, my job is halve done (assuming I'm sticking with dropdown option)

  21. 17 minutes ago, ginerjm said:

    I"m not sure what you are saying.   We have given you advice.  Time now for you to make the decision.  If it is a large inventory that would make the dropdown selection tedious, a client might not like the site.  But - since your user is an employee, that may not be an issue for you.   

    You have to decide what you want to do.  The easy way in my mind is to generate a page that gives the user just enough info to choose an item with and then submit that selection to the server and create a more detailed web page for that single item.  Unless you have a terribly slow or over-worked server this should not be a concern for the app.

    Ok. The app is gonna run on WAMP. So the issue of slow server is to a great extent eliminated. The system will have a good CPU and RAM for speed.  Will have to think of something and if nothing comes handy, will make do with the dropdown/select option. Thanks guys.

  22. 2 hours ago, mac_gyver said:

    IMO using a select/option menu to pick a product to add to a cart is not a good user interface. there's a limited amount of information that can be displayed about each product, so picking between similar products will be difficult and once you get to 20-30 items no one is going to want to use a select menu. you should list out each matching  (using a category/name selection/search) product with the name, description, price, thumbnail image, quantity field, and an add button.

    if you do have a use for a select/option menu to pick something, you would attach an onchange event handler to the select element, the option value should be the item id not the item name, the javascript would get the value of the selected option choice, use ajax to retrieve the data matching the selected item id, then populate any elements on the web page with the retrieved data. these are all very common tasks that you can research on the web to find example of. while you can display the price and calculate and display the total from the entered/selected quantity and the price, the only values that should be used on the server from the submitted data are the item id and the quantity.

    I quite agree with you. But this is to be used for small scale sales shop that sells confectioneries basically and few other stuffs. It's just to add glamour to the shop and also keep records of sales/profits. Using thumbnail/image will it not be tedious for the sales rep. Cos basically it's the sales rep only that does the shopping and the customer is issued receipt. Now I really should change/abandon my initial perception. What do u think? Any idea is highly welcomed.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.