Jump to content

PDO with array


gmc1103
Go to solution Solved by mac_gyver,

Recommended Posts

Hi

 

Can someone help me with this function

 public function testes($livro, $quant) {
        try {
            $sql = 'INSERT INTO itens(livro, quant) VALUES ';
            $qPart = array_fill(count($livro), count($quant), "(?, ?)");
            $sql .=  implode(",",$qPart);
            $stmt = $this->db->prepare($sql); 
            $i = 1;
            foreach($quant as $item) {
               $stmt -> bindParam($i++, $item['livro']);
               $stmt -> bindParam($i++, $item['quant']);
            }
            $stmt -> execute(); 
            return $stmt;
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

And i have this error

 

Warning: Illegal string offset 'livro' in C:\xampp\htdocs\esmaior_biblio\class.user.php on line 112

 

Fatal error: Only variables can be passed by reference 112

 $stmt -> bindParam($i++, $item['livro']);

Livro is book and quant is quantity

 

If someone register a book and this book has 30 copy i want to use this function to insert 30 record of this book.

 

What's wrong? 

 

Link to comment
Share on other sites

Your $item variable is not an array. In other words, the arguments $livro and $quant don't contain what you think they contain.

 

To figure out what the variable content actually looks like, use var_dump():

var_dump($livro);
var_dump($quant);

Your code is generally a bit weird. What exactly do you expect

array_fill(count($livro), count($quant), "(?, ?)")

to do? I mean the two count().

Link to comment
Share on other sites

Is not correct?

 

 

you are passing a string and an array containing a quantity into your function. that doesn't make any sense from a definition standpoint. these should just be two values. there's no point in the quantity being an element in an array. wouldn't you just pass in the book-name/book-id and a quantity?

 

next, why are you storing the quantity in each row? if you are making a row for each book (30 rows for your stated example, 12 rows for your apparent test data), storing the quantity in each row isn't relevant. what data do you want to store in each row?

 

finally, for what purpose are you doing this? if this if for an inventory system, you would want a single row holding the quantity, not a separate row for each copy of a book.

Link to comment
Share on other sites

Hi mac_gyver

 

Thanks again for your explanation.

I think i'm making a big confusion here.

 

Your question

why are you storing the quantity in each row? I don't want to store the full quantity in each row, just 1 in each

what data do you want to store in each row? I want to store the book_id, name, year, quantity

 

finally, for what purpose are you doing this? if this if for an inventory system, you would want a single row holding the quantity, not a separate row for each copy of a book. Again you are right

But for instance, if someone of total of 12 get 10 copy, how can i know that i have 2 to borrow?

 

Thanks again

Link to comment
Share on other sites

  • Solution

the way to do inventory, is like a checking account or an credit-card account register. you store a row for each + or - transaction.

 

the table tracking the inventory would hold the book_id (you would also have a 'book' table that holds the unique information about each book/title), user_id (who added or subtracted a quantity), the date-time (when the user added or subtracted a quantity), the quantity, and a memo/comment/status column.

 

you would initially store a row for each book/title, with the a positive quantity of how many are on hand. the user_id for that row would be the admin/librarian that initially enters the information. if more books are ordered and added to the inventory, you would add a row with a positive quantity.

 

when someone checks out a book(s), you would enter a row for each different book_id, with the quantity as a negative value. when they check in a book(s), you would enter a row for each different book_id, with the quantity as a positive value.

 

to get the quantity on hand, you would use a query with SELECT SUM(quantity) .... .... GROUP BY book_id.

 

if you are giving each book a unique id/serial number, you would still do the inventory this way, but you would have a separate row for each separate book, with a serial_number column to record exactly which book was checked out/in. the quantity column would then either be a 1 or a -1.

Link to comment
Share on other sites

Ok, il will try the first example you gave me.

 

only one id and quantity, then a query to check if that book (school book) is borrow and how many has been borrowed by this user to his class.

 

I will let you know if i'm stuck...

 

Thank you for your help.

 

Best regards

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.