gmc1103 Posted October 29, 2015 Share Posted October 29, 2015 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? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 29, 2015 Share Posted October 29, 2015 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(). Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted October 29, 2015 Author Share Posted October 29, 2015 Hi Thanks for your contribution with var_dump i have string 'Livros' (length=6) array (size=1) 0 => string '12' (length=2) Is not correct? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 29, 2015 Share Posted October 29, 2015 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. Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted October 29, 2015 Author Share Posted October 29, 2015 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 Quote Link to comment Share on other sites More sharing options...
Solution mac_gyver Posted October 29, 2015 Solution Share Posted October 29, 2015 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. Quote Link to comment Share on other sites More sharing options...
gmc1103 Posted October 29, 2015 Author Share Posted October 29, 2015 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.