Select ID from table2 insert into table1 - sql-server
I have two tables - table Kontrahent and table Orders. First, I insert data into the Kontrahent table, then into the Orders table. Inserting into Orders, in the IDAccount column I have to insert the value from the IDKontrahent column from the Kontrahent table. How?
try {
//Wstawianie nowego kontrahenta
$query = "INSERT INTO dbo.Kontrahent (Nazwa,Odbiorca,Dostawca,NIP,Regon,Uwagi,KodPocztowy,Miejscowosc,UlicaLokal,AdresWWW,Email,Utworzono,Zmodyfikowano,Telefon,Fax,OsobaKontaktowa,Pracownik,IDKraju,NrKonta,SWIFT,NazwaBanku,IDPaymentType,Archiwalny,IDRodzajuTransportu,SupplyCity,UlicaDostawy,KodPocztowyDostawy,NazwaAdresuDostawy,OsobaKontaktowaDostawy,TelefonDostawy,IDPlatnikaVat,CzyFirma,CzyGlownaFirma,NazwaKonta,LimitKredytu,IDPriceList) VALUES ('$complete_billing_name',1,0,'','','','$billing_postcode','$billing_state','$billing_address','','$billing_email','$isoDate','$ModDate',$billing_phone,'',NULL,0,616,'','','',NULL,0,NULL,'','','','','','',NULL,NULL,NULL,NULL,NULL,NULL)";
$result = $conn->prepare($query);
var_dump($result);
unset($query);
} catch (Exception $e) {
die(print_r($e->getMessage()));
}
This not working :
try{
$select = "SELECT IDENT_CURRENT(Kontrahent) ";
$result22 = $conn->query($select);
$result22 ->execute();
$query = "INSERT INTO dbo.Orders (IDOrderType, IDAccount, Number, IDOrderStatus, IDPaymentType, Remarks, IDUser, IDWarehouse, IDCurrency, IDCompany) VALUES (15,$result22,$customer_id,2,1,NULL,1,10,1,1)";
$result = $conn->query($query);
var_dump($result);
unset($result);
} catch (Exception $e) {
die(print_r($e->getMessage()));
}
EDIT:
The current code looks like this:
// Inserting data into Order:
// IDOrder (broadcast automatically)
// Set OrderTypeID = 15 for orders from customers (14 orders from suppliers, 16 are offers)
// IDAccount - is the customer ID from the Contractor table
// Number - is the document number, e.g. order number from presta
// IDOrderStatus - is the current status of the order from the OrderStatus table (e.g. Open or you can add your own ..)
// IDPaymentType - this is the payment method for the order (from the PaymentTypes table)
// Remarks - Order notes
// IDUser - what user creates this entry, e.g. 1 = Admin (Users table)
// IDWarehouse - in which warehouse to create the order (ID from the Warehouse table)
// IDCurrency = 1 for PLN
// IDCompany = 1
// Get ID
$query = "SELECT IDENT_CURRENT('dbo.Kontrahent') AS ID";
$stmt = $conn->prepare($query);
if ($stmt->execute() === false) {
die("Error executing query.");
};
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$id = $result;
var_dump($result);
print_r($id);
$stmt = null;
// Insert into Orders
$query = "
INSERT INTO dbo.Orders
(IDOrderType,
IDAccount,
Number,
IDOrderStatus,
IDPaymentType,
Remarks,
IDUser,
IDWarehouse,
IDCurrency,
IDCompany)
VALUES
(15, ?, ?, 2, 1, NULL, 1, 10, 1, 1)
";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $id, PDO::PARAM_INT);
$stmt->bindValue(2, $customer_id, PDO::PARAM_INT);
if ($stmt->execute() === false) {
die("Error executing query.");
};
print_r($stmt);
$stmt = null;
// Get ID Order from Orders
$query = "SELECT TOP 1 IDOrder FROM dbo.Orders ORDER BY IDOrder DESC";
//$query ="SELECT IDENT_CURRENT('dbo.Orders') AS IDTowaru";
$stmt = $conn->prepare($query);
if ($stmt->execute() === false) {
die("Error executing query.");
};
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$idorder = $result;
var_dump ($idorder, $result);
$stmt = null;
Search for IDTowaru by KodKreskowy. the KodKreskowy is only one identical in the store and application.
$query = "SELECT IDTowaru FROM dbo.Towar WHERE KodKreskowy = '$product_sku' ";
$stmt = $conn->prepare($query);
if ($stmt->execute() === false) {
die("Error executing query.");
};
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$iditem = $result;
var_dump($iditem);
$stmt = null;
Inserting OrderLines with IDItem from Towar table and IDOrder from Orders table
// Inserting into OrderLines:
// IDOrderLine (broadcast automatically)
// IDOrder - Id of the order header from the Order table
// IDItem -ID of the item from the Item or Current Status table
// Quantity - Quantity of the item
// PriceNet - net price
// PriceGross - gross price including VAT
// IDVat - VAT rate ID from the VATRates table
// Remarks - any additional comments
// IDUser - what user creates this entry, e.g. 1 = Admin (Users table)
$query = "
INSERT INTO dbo.OrderLines
(IDItem,
IDOrder,
Quantity,
PriceNet,
PriceGross,
IDVat,
Remarks,
IDUser)
VALUES
(?, ?, ?, ?, ?, 1, 1, 1)
";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $iditem, PDO::PARAM_INT);
$stmt->bindParam(2, $idorder, PDO::PARAM_INT);
$stmt->bindValue(3, $quantity, PDO::PARAM_INT);
$stmt->bindValue(4, $product_price, PDO::PARAM_INT);
$stmt->bindValue(5, $product_price, PDO::PARAM_INT);
if ($stmt->execute() === false) {
die("Error executing query.");
};
$stmt = null;
} catch (Exception $e) {
die(print_r($e->getMessage()));
}
}
This is error :
SQLSTATE[HY000]: General error: 20018 The INSERT statement conflicted with the FOREIGN KEY constraint "FK_OrderLines_Towar". The conflict occurred in database "greenmonkey", table "dbo.Towar", column 'IDTowaru'. [20018] (severity 16) [ INSERT INTO dbo.OrderLines (IDItem, IDOrder,Quantity, PriceNet, PriceGross, IDVat, Remarks, IDUser) VALUES (1, 1, 1, 42, 42, 1, 1, 1) ]1
Why IDAccount and Number is 1?
object(PDOStatement)#14917 (1) { ["queryString"]=> string(225) " INSERT INTO dbo.Orders (IDOrderType, IDAccount, Number, IDOrderStatus, IDPaymentType, Remarks, IDUser, IDWarehouse, IDCurrency, IDCompany) VALUES (15, ?, ?, 2, 1, NULL, 1, 10, 1, 1) " } array(1) { ["IDTowaru"]=> float(99) } array(1) { ["IDTowaru"]=> int(825) }
Next, why IDOrder and IDItem is 1?
[ INSERT INTO dbo.OrderLines (IDItem, IDOrder,Quantity, PriceNet, PriceGross, IDVat, Remarks, IDUser) VALUES (1, 1, 1, 42, 42, 1, 1, 1) ]1
I think that you should consider at least the following:
you need to prepare and execute your statements using PDO::prepare and PDOStatement::execute
you need to use parameterized statements to prevent SQL injection issues and set each parameter value using PDOStatement::bindParam
you need to fetch the result from your SELECT statement using PDOStatement::fetch
The next example, based on your code, may help to find a solution to your problem:
<?php
try{
// Insert into Kontrahent
$query = "
INSERT INTO dbo.Kontrahent
(Nazwa, Odbiorca, Dostawca, NIP, Regon, Uwagi, KodPocztowy, Miejscowosc, UlicaLokal, AdresWWW, Email, Utworzono, Zmodyfikowano, Telefon, Fax, OsobaKontaktowa, Pracownik, IDKraju, NrKonta, SWIFT, NazwaBanku, IDPaymentType, Archiwalny, IDRodzajuTransportu, SupplyCity, UlicaDostawy, KodPocztowyDostawy, NazwaAdresuDostawy, OsobaKontaktowaDostawy,TelefonDostawy, IDPlatnikaVat, CzyFirma, CzyGlownaFirma, NazwaKonta, LimitKredytu, IDPriceList)
VALUES
(?, 1, 0, '', '', '', ?, ?, ?, '', ?, ?, ?, ?, '', NULL, 0, 616, '', '', '', NULL, 0, NULL, '', '', '', '', '', '', NULL, NULL, NULL, NULL, NULL, NULL)
";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $complete_billing_name, PDO::PARAM_STR);
$stmt->bindParam(2, $billing_postcode, PDO::PARAM_STR);
$stmt->bindParam(3, $billing_state, PDO::PARAM_STR);
$stmt->bindParam(4, $billing_address, PDO::PARAM_STR);
$stmt->bindParam(5, $billing_email, PDO::PARAM_STR);
$stmt->bindParam(6, $isoDate, PDO::PARAM_STR);
$stmt->bindParam(7, $ModDate, PDO::PARAM_STR);
$stmt->bindParam(8, $billing_phone, PDO::PARAM_STR);
if ($stmt->execute() === false) {
die("Error executing query.");
};
$stmt = null;
// Get ID
$query = "SELECT IDENT_CURRENT('dbo.Kontrahent') AS ID";
$stmt = $conn->prepare($query);
if ($stmt->execute() === false) {
die("Error executing query.");
};
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$id = $row['ID'];
$stmt = null;
// Insert into Orders
$query = "
INSERT INTO dbo.Orders
(IDOrderType, IDAccount, Number, IDOrderStatus, IDPaymentType, Remarks, IDUser, IDWarehouse, IDCurrency, IDCompany)
VALUES
(15, ?, ?, 2, 1, NULL, 1, 10, 1, 1)
";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $id, PDO::PARAM_INT);
$stmt->bindValue(2, $customer_id, PDO::PARAM_INT);
if ($stmt->execute() === false) {
die("Error executing query.");
};
$stmt = null;
} catch (Exception $e) {
die(print_r($e->getMessage()));
}
?>
Related
i am facing this Supplied connection object Parameter is invalid while i tried to insert data in db2
i tried to insert the data into db 2 but shows stmt = ibm_db.prepare(conn,insert_sql) Exception: Supplied connection object Parameter is invalid my code is def register(): error = '' if request.method == 'POST' : username = request.form['username'] password = request.form['password'] address = request.form['address'] mobileNo = request.form['mobileNo'] email = request.form['email'] insert_sql = "INSERT INTO users VALUES (?,?,?,?,?)" stmt = ibm_db.prepare(conn,insert_sql) ibm_db.bind_param(prep_stmt, 2, username) ibm_db.bind_param(prep_stmt, 3, password) ibm_db.bind_param(prep_stmt, 4, address) ibm_db.bind_param(prep_stmt, 5, mobileNo) ibm_db.bind_param(prep_stmt, 6, email) ibm_db.execute(stmt) error = 'You have successfully registered !' return render_template('register.html', error = error) def register(): error = '' if request.method == 'POST' : username = request.form['username'] password = request.form['password'] address = request.form['address'] mobileNo = request.form['mobileNo'] email = request.form['email'] insert_sql = "INSERT INTO users VALUES (?,?,?,?,?)" stmt = ibm_db.prepare(conn,insert_sql) ibm_db.bind_param(prep_stmt, 2, username) ibm_db.bind_param(prep_stmt, 3, password) ibm_db.bind_param(prep_stmt, 4, address) ibm_db.bind_param(prep_stmt, 5, mobileNo) ibm_db.bind_param(prep_stmt, 6, email) ibm_db.execute(stmt) error = 'You have successfully registered !' return render_template('register.html', error = error) result is : stmt = ibm_db.prepare(conn,insert_sql) Exception: Supplied connection object Parameter is invalid
CakePHP Custom Query Paginator
Currently I am got 2 page require Pagination. I also do some research such as setting or at model do the custom Query. However the Paginator can't work. Now I am trying add custom query paginator to index page. (I need to use group by) I also trying to add custom query paginator to view page. ( I need base on the id) Controller: public function index() { $setting = $this->Paginator->settings = $this->Report->query(' SELECT SUM( seats_occupied ) as totalOccupied, event_date, r.event_date_id FROM reports r, event_dates ed WHERE r.event_date_id = ed.event_date_id GROUP BY event_date '); debug($this->Paginator->paginate()); $this->set('showDate',$setting); } public function view($eventDateId = null){ if (!$eventDateId) { throw new NotFoundException(__('Invalid post')); } // We only want single post so we use FindById. $post = $this->Report->findAllByEventDateId($eventDateId); // debug($this->Paginator->paginate($post)); if (!$post) { //if user requet fypcakephp/view without id will throw the not found except throw new NotFoundException(__('Invalid post')); } $setting = $this->Paginator->settings = $this->Report->query(" SELECT * FROM reports where event_date_id='$eventDateId' order by period asc "); //array( 'conditions' => array( 'event_date_id' => $post)); debug($setting); $this->set('viewData', $setting); //Then send this data } I also got try do at model custom Query however If I do this way , my view can't get the data I want due to I using "Group By". //To custom the paginate display the thing you want. (This one return) //public function paginate($conditions, $fields, $order, $limit, $page = 1, // $recursive = null, $extra = array()) //{ // $recursive = 0; // // // Mandatory to have // $this->useTable = false; // $sql = ''; // // // $sql .= " select * from reports;"; // // // // $sql .= " SELECT SUM( seats_occupied ) as totalOccupied, event_date,r.event_date_id // FROM reports r, event_dates ed WHERE r.event_date_id = ed.event_date_id // GROUP BY event_date; "; // // // // // Adding LIMIT Clause //// $sql .= (($page - 1) * $limit) . ', ' . $limit; // // $results = $this->query($sql); // // return $results; //} //public function paginateCount($conditions = null, $recursive = 0, $extra = array()) //{ // $sql = ''; // // $sql .= "SELECT SUM( seats_occupied ) as totalOccupied, event_date ,r.event_date_id // FROM reports r, event_dates ed WHERE r.event_date_id = ed.event_date_id // GROUP BY event_date"; // // $this->recursive = $recursive; // // $results = $this->query($sql); // // return count($results); //}
Which of Database::<QUERY_TYPE> I should use in Kohana 3.3?
I need perform query "LOCK TABLE myTable WRITE" <?php $db = Database::instance(); $query = 'LOCK TABLE `myTable` WRITE'; $db->query(Database::WHAT_IS_THE_TYPE_SHOULD_SPECIFY_HERE_?, $query); In framework Kohana 3.3 file ~/modules/database/classes/Kohana/Database.php implements folowing types: const SELECT = 1; const INSERT = 2; const UPDATE = 3; const DELETE = 4; but none of them fit in my case. Any idea. Thanks.
Google works wonders. http://forum.kohanaframework.org/discussion/6401/lockunlock/p1 You can pass NULL as the first argument: $db = Database::instance(); $query = 'LOCK TABLE `myTable` WRITE'; $db->query(NULL, $query); From Kohana: http://kohanaframework.org/3.3/guide-api/Database_MySQL#query if ($type === Database::SELECT) { // Return an iterator of results return new Database_MySQL_Result($result, $sql, $as_object, $params); } elseif ($type === Database::INSERT) { // Return a list of insert id and rows created return array( mysql_insert_id($this->_connection), mysql_affected_rows($this->_connection), ); } else { // Return the number of rows affected return mysql_affected_rows($this->_connection); } As you can see that's why you're able to pass NULL.
Why "InvalidRequest" on 2nd hit of page 2 of pagination?
I do pagination and a link that works to page 2 looks like this /q?query=regionID%3D4700188&cursor=False:CqoECuYBCtgB_wDAQM8HgAD_AP8A_wD__wDAQCQA_wD_AP8A_wD_AP__AMBAHAD_AP8A_wD_AP8A___-9wllzNf_Z_-_jIGSkJGLnpCPjZCVmpyL_wB0baCgmYuMoKD_AF2ej4-akZiWkZr_AHN0bZaRm5qH_wBdnpuM_wBzdG2bkJyglpv_AF2emMaFmZLOiZ2RrZedzL2Gnc2Pk6bMrYa8iIysvpS5lLi0nsrOiLKy_wBzf56YxoWZks6JnZGtl53MvYadzY-TpsythryIjKy-lLmUuLSeys6IsrL_AP_-EDIh4o_3av1KJSkSDURvY3VtZW50SW5kZXganwEoQU5EIChJUyAiY3VzdG9tZXJfbmFtZSIgImFwcGVuZ2luZSIpIChJUyAiZ3JvdXBfbmFtZSIgInN-bW9udGFvcHJvamVjdCIpIChJUyAibmFtZXNwYWNlIiAiIikgKElTICJpbmRleF9uYW1lIiAiYWRzIikgKEVRIDQ3MDAxODguMDAwMDAwMDAwMDAgInNuYnJfcmVnaW9uSUQiKSk6GwoOKE4gc2RhdGVfZGF0ZSkQARkAAABzFPtzQjoaCg0oTiBzbmJyX2hvdXIpEAEZAAAAAAAAEEA6HAoPKE4gc25icl9taW51dGUpEAEZAAAAAAAAMkBKHAgBOhVzdDpidGlfZ2VuZXJpY19zY29yZXJAkE5SGQoMKE4gb3JkZXJfaWQpEAEZAAAAAAAA8P8 But every now and then, seemingly random, I get error message that doesn't seem to be a timeout but an error with the query. When I hit reload I got this error, is it a timeout error? "GET /q?query=regionID%3D4700188&cursor=False:CqoECuYBCtgB_wDAQM8HgAD_AP8A_wD__wDAQCQA_wD_AP8A_wD_AP__AMBAHAD_AP8A_wD_AP8A___-9wllzNf_Z_-_jIGSkJGLnpCPjZCVmpyL_wB0baCgmYuMoKD_AF2ej4-akZiWkZr_AHN0bZaRm5qH_wBdnpuM_wBzdG2bkJyglpv_AF2emMaFmZLOiZ2RrZedzL2Gnc2Pk6bMrYa8iIysvpS5lLi0nsrOiLKy_wBzf56YxoWZks6JnZGtl53MvYadzY-TpsythryIjKy-lLmUuLSeys6IsrL_AP_-EDIh4o_3av1KJSkSDURvY3VtZW50SW5kZXganwEoQU5EIChJUyAiY3VzdG9tZXJfbmFtZSIgImFwcGVuZ2luZSIpIChJUyAiZ3JvdXBfbmFtZSIgInN-bW9udGFvcHJvamVjdCIpIChJUyAibmFtZXNwYWNlIiAiIikgKElTICJpbmRleF9uYW1lIiAiYWRzIikgKEVRIDQ3MDAxODguMDAwMDAwMDAwMDAgInNuYnJfcmVnaW9uSUQiKSk6GwoOKE4gc2RhdGVfZGF0ZSkQARkAAABzFPtzQjoaCg0oTiBzbmJyX2hvdXIpEAEZAAAAAAAAEEA6HAoPKE4gc25icl9taW51dGUpEAEZAAAAAAAAMkBKHAgBOhVzdDpidGlfZ2VuZXJpY19zY29yZXJAkE5SGQoMKE4gb3JkZXJfaWQpEAEZAAAAAAAA8P8 HTTP/1.1" 200 8611 "http://www.koolbusiness.com/q?query=regionID%3D4700188" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36" "www.koolbusiness.com" ms=133 cpu_ms=42 cpm_usd=0.060962 app_engine_release=1.8.1 instance=00c61b117cd1983617eb4b0968a84b71874563 D 2013-07-06 06:20:37.095 query regionID=4700188 E 2013-07-06 06:20:37.120 Search failed Traceback (most recent call last): File "/base/data/home/apps/s~montaoproject/2013e.368581150756737282/search_demo.py", line 87, in find_documents return index.search(query) File "/python27_runtime/python27_lib/versions/1/google/appengine/api/search/search.py", line 2732, in search _CheckStatus(response.status()) File "/python27_runtime/python27_lib/versions/1/google/appengine/api/search/search.py", line 413, in _CheckStatus raise _ERROR_MAP[status.code()](status.error_detail()) InvalidRequest: Failed to execute search request "regionID=4700188" Code def find_documents(query_string, limit, cursor): try: date_desc = search.SortExpression(expression='date', direction=search.SortExpression.DESCENDING, default_value=datetime.now().date()) hr_desc = search.SortExpression(expression='hour', direction=search.SortExpression.DESCENDING, default_value=datetime.now().hour) min_desc = search.SortExpression(expression='minute', direction=search.SortExpression.DESCENDING, default_value=datetime.now().minute) # Sort up to 5000 matching results by subject in descending order sort = search.SortOptions(expressions=[date_desc, hr_desc, min_desc], limit=10000) # Set query options options = search.QueryOptions(limit=limit, cursor=cursor, sort_options=sort, number_found_accuracy=10000) # the number of results to return # returned_fields=['author', 'subject', 'summary'], # snippeted_fields=['content'] query = search.Query(query_string=query_string, options=options) index = search.Index(name=_INDEX_NAME) # Execute the query return index.search(query) except search.Error: logging.exception('Search failed') return None regions_in = [ ('3', u'Entire India'), ('4703187', u'Andaman & Nicobar Islands'), ('4694186', u'Andhra Pradesh'), ('4699188', u'Arunachal Pradesh'), ('4692186', u'Assam'), ('4702186', u'Bihar'), ('4698185', u'Chandigarh'), ('4676188', u'Chhattisgarh'), ('4691190', u'Dadra & Nagar Haveli'), ('4704183', u'Daman & Diu'), ('4699183', u'Delhi'), ('4702187', u'Goa'), ('4691189', u'Gujarat'), ('4700186', u'Haryana'), ('4703185', u'Himachal Pradesh'), ('4694187', u'Jammu & Kashmir'), ('4699189', u'Jharkhand'), ('4701185', u'Karnataka'), ('4695189', u'Kerala'), ('4700189', u'Lakshadweep'), ('4697186', u'Madhya Pradesh'), ('4694184', u'Maharashtra'), ('4700187', u'Manipur'), ('4703186', u'Meghalaya'), ('4698184', u'Mizoram'), ('4692187', u'Nagaland'), ('4696185', u'Orissa'), ('4676189', u'Pondicherry'), ('4693185', u'Punjab'), ('4701186', u'Rajasthan'), ('4701187', u'Sikkim'), ('4701188', u'Tamil Nadu'), ('4697187', u'Tripura'), ('4699190', u'Uttaranchal'), ('4692188', u'Uttar Pradesh'), ('4700188', u'West Bengal'), ] class RegionSearch(SearchBaseHandler): """Handles regional search requests.""" def get(self): """Handles a get request with a query.""" category = None cityentity = None next_cursor = None country = '' if self.request.host.find('hipheap') > -1: country = 'USA' elif self.request.host.find('koolbusiness') > -1: country = 'India' elif self.request.host.find('montao') > -1: country = 'Brasil' number_returned = 0 regionname = None cityname = None regionentity = None region = None cursor = self.request.get('cursor') uri = urlparse(self.request.uri) query = '' regionID = regionid = self.request.get('regionid', 0) cityID = cityid = self.request.get('cityid', 0) categoryID = categoryid = self.request.get('category', 0) if uri.query: query = parse_qs(uri.query) try: query = query['query'][0] except KeyError, err: query = '' # logging.info('KeyError') # Try find region ID and/or cityID and categoryID a.s.a.p. if regionid or query.find('regionID') > -1: regionID = re.sub("^regionID=(\d+).*", r'\1', query) region = Region.get_by_id(long(regionID)) regionname = region.name if regionid: regionID = regionid region = Region.get_by_id(long(regionID)) regionname = region.name if cityid or query.find('cityID') > -1: cityID = re.sub("^.*cityID=(\d+).*", r'\1', query) if cityid: cityID = cityid city = montaomodel.City.get_by_id(long(cityID)) cityID = city.key().id() cityentity = city cityname = city.name region = Region.get_by_id(long(city.region.key().id())) regionID = region.key().id() if categoryid or query.find('category') > -1: categoryID = re.sub("^.*category=(\d+).*", r'\1', query) if categoryid: categoryID = categoryid logging.debug('query %s', query) if cursor: results = find_documents(query, 50, search.Cursor(cursor)) else: results = find_documents(query, 50, search.Cursor()) if results and results.cursor: next_cursor = results.cursor.web_safe_string namedquery = query query = query.replace(' and company_ad=0', '' ).replace(' and company_ad=1', '' ).replace(' and category:(6010 OR 6020 OR 6030 OR 6040 OR 6090)' , '' ).replace(' and category:(6010 OR 6020 OR 6030 OR 6040 OR 6090)' , '' ).replace(' and category:(1020 OR 1010 OR 1030 OR 1050 OR 1080 OR 1100 OR 1090)' , '' ).replace(' and category:(2010 OR 2030 OR 2040 OR 2080 OR 2070)' , '' ).replace(' and category:(3040 OR 3050 OR 3030 OR 3060)' , '' ).replace(' and category:(4010 OR 4020 OR 4040 OR 4030 OR 4090 OR 4060 OR 4070)' , '') query = re.sub("regionID=\d+", '', query) query = query.replace('category and', '') query = query.replace('type=s', '') query = query.replace('type=w', '') query = query.replace('type=r', '') query = query.replace('type=b', '') query = query.replace('cityID and', '') query = query.replace('and ', '') query = query.replace(' and', '') query = query.replace('regionID', '') query = query.replace('=', '%3D') namedquery = namedquery.replace('=', '%3D') query = re.sub("cityID%3D\d+", '', query) query = re.sub("category%3D\d+", '', query) query = query.replace(' ', ' ') # to do: make into a dictionary for O(1) access if int(regionid) > 0: regionname = region_id_to_name[regionid] #if regionID and query.find('cityID') < 1: # region = Region.get_by_id(long(regionID)) form = SearchForm() form.w.choices = [ ('4703187', u'Andaman & Nicobar Islands'), ('4694186', u'Andhra Pradesh'), ('4699188', u'Arunachal Pradesh'), ('4692186', u'Assam'), ('4702186', u'Bihar'), ('4698185', u'Chandigarh'), ('4676188', u'Chhattisgarh'), ('4691190', u'Dadra & Nagar Haveli'), ('4704183', u'Daman & Diu'), ('4699183', u'Delhi'), ('4702187', u'Goa'), ('4691189', u'Gujarat'), ('4700186', u'Haryana'), ('4703185', u'Himachal Pradesh'), ('4694187', u'Jammu & Kashmir'), ('4699189', u'Jharkhand'), ('4701185', u'Karnataka'), ('4695189', u'Kerala'), ('4700189', u'Lakshadweep'), ('4697186', u'Madhya Pradesh'), ('4694184', u'Maharashtra'), ('4700187', u'Manipur'), ('4703186', u'Meghalaya'), ('4698184', u'Mizoram'), ('4692187', u'Nagaland'), ('4696185', u'Orissa'), ('4676189', u'Pondicherry'), ('4693185', u'Punjab'), ('4701186', u'Rajasthan'), ('4701187', u'Sikkim'), ('4701188', u'Tamil Nadu'), ('4697187', u'Tripura'), ('4699190', u'Uttaranchal'), ('4692188', u'Uttar Pradesh'), ('4700188', u'West Bengal'), ] if region or cityentity: # to do:use memcache form.area.choices = [] # to do: use memcache for the list for cityitem in City.all().filter('region =', region.key()).order('-vieworder').order('name' ).fetch(99999): form.area.choices.append([str(cityitem.key().id()), cityitem.name]) if cityentity: form.area.data = str(cityentity.key().id()) if self.request.host.find('hipheap') > -1: if region and (str(region.key().id()), region.name) \ in form.w_us.choices: form.w_us.choices.remove((str(region.key().id()), region.name)) else: if region and (str(region.key().id()), region.name) \ in form.w.choices: form.w.choices.remove((str(region.key().id()), region.name)) if region: regionname = region.name if results: number_returned = len(results.results) template_values = { 'results': results,'regionname':regionname, 'cursor': next_cursor, 'country': country, 'user': self.current_user, 'number_returned': number_returned, 'loggedin': self.logged_in, 'VERSION': VERSION, 'region': region, 'regionname': regionname, 'jobs_count': get_jobs_count(self, regionID, cityID), 'estate_count': get_estate_count(self, regionID, cityID), 'electronics_count': get_electronics_count(self, regionID, cityID), 'home_count': get_home_count(self, regionID, cityID), 'leisure_count': get_leisure_count(self, regionID, cityID), 'vehicles_count': get_vehicles_count(self, regionID, cityID), 'cityentity': cityentity, 'request': self.request, 'categoryID': categoryID, 'form': form, 'query': query, 'namedquery': namedquery, 'cityname': cityname, 'category': category, } self.render_template('q.htm', template_values) class India(SearchBaseHandler): def get(self): """Handles a get request with a query.""" regionname = None country = 'India' cursor = self.request.get('cursor') region = None if self.request.host.find('hipheap') > -1: country = 'USA' elif self.request.host.find('koolbusiness') > -1: country = 'India' elif self.request.host.find('montao') > -1: country = 'Brasil' uri = urlparse(self.request.uri) query = '' if uri.query: query = parse_qs(uri.query) try: query = query['query'][0] except KeyError, err: query = '' # logging.info('KeyError') if cursor: results = find_documents(query, 50, search.Cursor(cursor)) else: results = find_documents(query, 50, search.Cursor()) next_cursor = None if results and results.cursor: next_cursor = results.cursor.web_safe_string query = query.replace(' and company_ad=0', '' ).replace(' and company_ad=1', '') regionname = 'Entire India' regionID = 0 cityID = 0 form = SearchForm() form.w.choices = region_id_to_name template_values = { 'regions':region_id_to_name, 'form': form, 'results': results, 'cursor': next_cursor, 'region': region, 'country': country, 'number_returned': len(results.results), 'jobs_count': get_jobs_count_india(self, regionID, cityID), 'estate_count': get_estate_count_india(self, regionID, cityID), 'electronics_count': get_electronics_count_india(self, regionID, cityID), 'home_count': get_home_count_india(self, regionID, cityID), 'leisure_count': get_leisure_count_india(self, regionID, cityID), 'vehicles_count': get_vehicles_count_india(self, regionID, cityID), 'user': users.get_current_user(), 'loggedin': self.logged_in, 'region': region, 'regionname': regionname, 'city': '', 'cityentity': None, 'request': self.request, 'form': SearchForm(), 'query': query, } self.render_template('q.htm', template_values) The log file where the first hit is OK and the reload fails is here. Update The bug is changing. Now it just didn't happen until the third hit of the 2nd page. It doesn't make sense at all not to encounter a pagination error until the third hit of the second page. This is absurd so tell me what is going on and how to investigate the trace? I changed the code to say more about the trace but python doesn't inform me what it is: It's not only RegionSearch that's failing, it's also search for empty string on pages after page 2. Since it never happens on the first page I suspect this must have something to do with the cursorbut I've been totally unable to figure out what is wrong with the cursor. The exception doesn't tell me anything, it just says ´this is not working´and nothing about why. I changed handling but it didn't generate any more output about the error: `return index.search(query) except search.Error as e: logging.exception('Search failed %s', e)` It does occur randomöly without seeming to be a timeout and the exception does not inform about the problem, all it says is "search failed" and the query that fails, a query which works if I put it in the search field. So it doesnt make sense anyhow.
I can only guess that it's RegionSearch that's failing. You'll need to handle exceptions on your queries. See https://developers.google.com/appengine/docs/python/datastore/exceptions The exception will tell you what the error is. Hard to guess. EDIT: try: # do query except search.PutError as e: logging.exception('caught PutError %s', e) except search.InternalError as e: logging.exception('caught InternalError %s', e) except search.DeleteError as e: logging.exception('caught DeleteError %s', e) except search.TransientError as e: logging.exception('caught TransientError %s', e) except search.InvalidRequest as e: logging.exception('caught InvalidError %s', e) except search.Error as e: logging.exception('caught unknown error %s', e)
Try setting the SortExpression's default_value to None, that worked for me. I was getting the same issue in my Test/QA instance however in my PROD instance worked fine, setting the default_value to None solved the problem in the Test/QA instance for good.
cakephp auth and user register stopped working
I have following piece of code: public function check() { $result = $this->User->field('id', array('username' => 'alek#lol.pl')); debug($result); } In my users table I have record: NSERT INTO `users` (`id`, `username`, `password`, `role`, `created`, `modified`) VALUES ('5', 'alek#lol.pl', '64c918dd65b67b455248c2498bcc5421a66b68a6', 'member', '2012-04-24 10:56:37', '2012-04-24 10:56:37'); and the result of this controller method check is: false And the sql query is: Nr Query Error Affected Num. rows Took (ms) 1 SELECT `User`.`id` FROM `freebooks`.`users` AS `User` WHERE `username` = 'alek#lol.pl' LIMIT 1 1 1 0 So SQL return one row but in cake result is 'false' - whats wrong? something broken?? when I execute this SQL statement in my PhpMyAdmin it correctly returns id = 5 And when I change one line to $result = $this->User->find('all'); this prints true This is impossible!!!
$this->User->field('id', array('username', $this->request->data['User']['username'])); you can check like this: if(!empty($this->User->field('id', array('username', $this->request->data['User']['username']));)) { // code } OR $id = $this->User->field('id', array('username', $this->request->data['User']['username'])); $this->User->id = $id; if($this->User->exists()) { // code }