Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLSTATE[HY000]: General error: 1366 Incorrect integer value:

$order = new Application_Model_DbTable_Order();

$orderno = $order->select()
->from($order, 'orderno')
->where('memberid = ?', $userid)
->order('orderno DESC')
->limit(1, 0);

SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'SELECT ordertable.orderno FROM ordertable WHERE (memberid = '30') ORDER BY orderno DESC LIMIT 1' for column 'orderno' at row 1

Got this error and am wondering if there is anything wrong with my code, because I have searched everywhere for the cause but don't seem to find any help.

#SQL code for Ordertable#
`orderno` int(5) NOT NULL AUTO_INCREMENT,
`memberid` int(5) DEFAULT NULL,
PRIMARY KEY (`orderno`)

#SQL code for Item#
`itemid` int(5) NOT NULL AUTO_INCREMENT,
`image` varchar(100) NOT NULL,
`itemname` varchar(30) DEFAULT NULL,
`description` varchar(100) DEFAULT NULL,
`itemtype` varchar(20) DEFAULT NULL,
PRIMARY KEY (`itemid`)

#SQL code for Orderdetail#
`orderdetailno` int(5) NOT NULL AUTO_INCREMENT,
`orderno` int(5) NOT NULL,
`itemid` int(5) NOT NULL,
`unitcost` decimal(6,2) DEFAULT NULL,
PRIMARY KEY (`orderdetailno`),
KEY `orderno` (`orderno`),
KEY `itemid` (`itemid`)

This is my sql code if it helps I'm using MySQL.

$request = new Zend_Session_Namespace('cart');

$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$userid = $user->userid;

$order = new Application_Model_DbTable_Order();
$itemdb = new Application_Model_DbTable_Item();
$orderdetail = new Application_Model_DbTable_Orderdetail();

$data = array ('memberid' => $userid);
$order->insert($data);

$orderno = $order->select()
->from($order, 'orderno')
->where('memberid = ?', $userid)
->order('orderno DESC')
->limit(1, 0);


foreach ($request->array as $var)
{
$unitprice = $itemdb->select()
->from('$itemdb', 'unitcost')
->where('itemid = ?', $var);

$newArray = array('orderno' => $orderno,
'itemid' => $var,
'unitcost' => $unitprice, );

$orderdetail->insert($newArray);                    
}

Any guidance will be very much appreciated.

like image 878
Swap Test Avatar asked Aug 11 '12 21:08

Swap Test


2 Answers

Per https://stackoverflow.com/a/8882396/1432614, run

SELECT @@GLOBAL.sql_mode;
SELECT @@SESSION.sql_mode;

and if either include STRICT_TRANS_TABLES, remove it from the setting.

For example:

SET @@GLOBAL.sql_mode= 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
like image 103
Ross Smith II Avatar answered Oct 07 '22 19:10

Ross Smith II


The reason you are getting that error is because you are attempting to use a Zend_Db_Select object as a value in your INSERT statement.

$orderno = $order->select()
                 ->from($order, 'orderno')
                 ->where('memberid = ?', $userid)
                 ->order('orderno DESC')
                 ->limit(1, 0);

And then inside the foreach loop:

$newArray = array('orderno' => $orderno,  // <-- this becomes a SELECT statment
                  'itemid' => $var,
                  'unitcost' => $unitprice, );

$orderdetail->insert($newArray); // attempting to insert a select statement

You should execute the $orderno statement and fetch() the result if you want to use it in an insert statement:

$ordernum = $orderno->query()->fetch();

$newArray = array('orderno' => $ordernum,
                  'itemid' => $var,
                  'unitcost' => $unitprice, );

$orderDetail->insert($newArray);
like image 24
drew010 Avatar answered Oct 07 '22 20:10

drew010