I am having a very annoying problem trying to import a wordpress db backup I am using wootickets to sell tickets for an event and for a customers db its complaing on xampp that their is a a illegal double here in the insert into statement shown below. Their hudreds of these values scattered thruought the the file i persume used for security.
INSERT INTO fl_postmeta ( meta_id , post_id , meta_key , meta_value ) VALUES ( 165743, 3313, '_tribe_wooticket_security_code', 'e801d16bb2' );
MySQL said: Documentation
1367 - Illegal double '342e880385' value found during parsing
My Question is why would this be the case when i exported from a linux platform to xamp for testing how do i fix this
The value 342e880385 should be put inside single quotes, like so:
INSERT INTO fl_postmeta ( meta_id , post_id , meta_key , meta_value )
VALUES ( 162936, 3115, '_tribe_wooticket_security_code', '342e880385' );
Otherwise, MySQL will try to parse it as a number.
Related
I'm working with Prestashop and I've got a module that generates shipping labels for my orders. When a label is made it generates a tracking code that can be used on the carriers website. The problem though is that our shippers have to copy and paste this code into prestashop for customers to receive a tracking email. It's my goal to automate this process to minimize human error within our system.
I've found the table and column within prestashop's database that houses the tracking code (ps_order_carrier, tracking_number) and I have confirmed that changing this value in the database effects the order in the way I have intended. Within the prestashop module there is a section of code that submits the tracking number and various order information like the order id which I will likely need to it's own table so I'm assuming I can just duplicate this portion of code and modify it to meet my needs. The following is the code within the module:
$sql = 'INSERT INTO '._DB_PREFIX_.$this->name.'_labels
(
id_order,
id_shipment,
postage_label_ref_id,
postage_label_object,
postage_label_updated_at,
postage_label_label_url,
tracking_code,
selected_rate_ref_id,
selected_rate_object,
selected_rate_updated_at,
selected_rate_service,
selected_rate_carrier,
selected_rate_shipment_ref_id,
tracker_ref_id,
tracker_object
)
VALUES
(
"'.(int)$data['id_order'].'",
"'.pSQL($data['id_shipment']).'",
"'.pSQL($data['postage_label']['ref_id']).'",
"'.pSQL($data['postage_label']['object']).'",
"'.pSQL($data['postage_label']['updated_at']).'",
"'.pSQL($data['postage_label']['label_url']).'",
"'.pSQL($data['tracking_code']).'",
"'.pSQL($data['selected_rate']['ref_id']).'",
"'.pSQL($data['selected_rate']['object']).'",
"'.pSQL($data['selected_rate']['updated_at']).'",
"'.pSQL($data['selected_rate']['service']).'",
"'.pSQL($data['selected_rate']['carrier']).'",
"'.pSQL($data['selected_rate']['shipment_ref_id']).'",
"'.pSQL($data['tracker']['ref_id']).'",
"'.pSQL($data['tracker']['object']).'"
)';
Db::getInstance()->Execute($sql);
I made a copy of this directly underneath and amended it to meet my needs but it doesn't seem to do what I want it to. I have a feeling I'm messing up the syntax. I've tried a few different variations of it so it's a bit chaotic but here is what I have as of writing this:
$sql2 = 'INSERT INTO `ps_order_carrier` WHERE `id_order` = '.(int)$data['id_order'].'
(
tracking_number
)
VALUES
(
"'.pSQL($data['tracking_code']).'"
)';
Db::getInstance()->Execute($sql2);
Any help would be greatly appreciated as this would save us so much time.
Thanks!
You're trying to update a row using an "INSERT" query.
$sql2 = 'UPDATE `ps_order_carrier` SET tracking_number = "'.pSQL($data['tracking_code']).'" WHERE `id_order` = '.(int)$data['id_order'].'
Db::getInstance()->Execute($sql2);
But the best way would be to first load the orderCarrier Object and change its tracking value without doing a direct query to DB.
$order = new Order($data['id_order']);
$orderCarrier = new OrderCarrier($order->getIdOrderCarrier());
$orderCarrier->tracking_number = $data['tracking_code'];
$orderCarrier->save();
To avoid all kind of mistakes in MySQL query syntax Prestashop have some classes and functions you can use:
Db::getInstance()->insert($this->name.'_labels',
array(
'id_order' => (int)$data['id_order'],
'id_shipment' => pSQL($data['id_shipment']),
/*and so on*/
)
);
Remember to cast all ID values to int and use pSQL in all string values.
There is an update function if you need it too.
Good luck.
there is no where clause in the insert statement.
specify all the values - and you get a new record.
$sql2 = 'INSERT INTO ps_order_carrier
(
tracking_number
, id_order
)
VALUES
(
"'.pSQL($data['tracking_code']).'"
, "'.(int)$data['id_order'].'"
)';
not tested - you may still need to fiddle with the quotes...
I have a custom pod, product_type, in that pod I have a custom field named label. I want to find all the pods where the label matches a certain strings.
Iv been trying this (and some other variations):
$mypod = pods('product_type', array(
'where' => 'product_type.label LIKE "%'.$someLabelString.'%"',
'limit'=>-1
));
I've also tried changing the product_type.label part of the query to t.label, but to no avail. I get this error:
Database Error; SQL: SELECT DISTINCT `t`.* FROM `wp_posts` AS `t` WHERE ( ( `product_type`.`label` LIKE "%hello%" ) AND ( `t`.`post_type` = "product_type" ) AND ( `t`.`post_status` IN ( "publish" ) ) ) ORDER BY `t`.`menu_order`, `t`.`post_title`, `t`.`post_date`; Response: Unknown column 'product_type.label' in 'where clause'
Here’s a super handy break down of what field syntax to use for each pod type:
http://pods.io/docs/code/pods/find/#additional-information
Custom simple lists consist of a label (for display) and a value (the underlying “raw” value). For the ‘where’ parameter, you probably need to check against whatever you set as the ‘value’.
When you create a simple custom defined list, a box becomes available for “Custom Defined Options”. As the help pop-up says: “One option per line, use value|Label for separate values and labels”
If you added the label field to your pod, you would just use product_type.label if your pod is table-based for storage, otherwise if it’s meta-based you would use label.meta_value
I am trying to insert data into a MySQL table which contains 19 columns however not all the rows are being stored.
Only a few of the rows are being stored and I'm getting the error message:
There is error in your SQL syntax. Check your syntax for your SQL version.
Although when I echo the variables, they are working fine.
My code is as follows:
$sql="CREATE TABLE tb(tb1 VARCHAR(50),tb2 VARCHAR(50),tb3 VARCHAR(100),tb4 VARCHAR(100),tb5 VARCHAR(100),tb6
VARCHAR(100),tb7 VARCHAR(100),tb8 VARCHAR(100),tb9 VARCHAR(100),tb10 VARCHAR(100),tb11 VARCHAR(100),tb12
VARCHAR(100),tb13 VARCHAR(100),tb14 VARCHAR(100),tb15 VARCHAR(100),tb16 VARCHAR(100),tb17 VARCHAR(100),tb18
VARCHAR(100),tb19 VARCHAR(100))";
foreach ($xml->product as $character)
{
$a1=$character->category->primary;
$b2=$character->category->secondary;
$c3=$character->URl->product;
$d4=$character->URL->productImage;
$e5=$character->URL->buy;
$f6=$character->description->short;
$g7=$character->description->long;
$h8=$character->discount->amount;
$i9=$character->discount->time;
$j10=$character->price->sale;
$k11=$character->price->retail;
$l12=$character->brand;
$m13=$character->shipping->cost->amount;
$n14=$character->shipping->cost->currency;
$o15=$character->shipping->information;
$p16=$character->shipping->availability;
$q17=$character->keywords;
$r18=$character->upc;
$s19=$character->m1;
$sql="INSERT INTO tb
(tb1,tb2,tb3,tb4,tb5,tb6,tb7,tb8,tb9,tb10,tb11,tb12,tb13,tb14,tb15,tb16,tb17,tb18,tb19) VALUES
('$a1','$b2','$c3','$d4','$e5','$f6','$g7','$h8','$i9','$j10','$k11','$l12','$m13','$n14','$o15','$p16','$q17','$r18','$s19')";
mysql_query($sql,$conn);
}
If ANY of your values contains an apostrophe, your query breaks.
Use mysql_real_escape_string() around each of your values as a quick fix.
A more correct and future-proof solution is to stop using mysql_* functions and instead start using PDO, making use of features like prepared statements as these take care of escaping things for you.
This is a formatted comment. A frequent mistake with this type of query is that the number of fields does not match the number of values. That is easier to troubleshoot if you type your query like this:
insert into table (
field1
, field2
, etc
)
values (
value1
, value2
, etc
)
This format makes it easier to count the number of fields and values. Sometimes the problem is with a certain field or value. This format, with the commas at the start of the line, make it easier to comment out blocks of code to isolate the problem.
Excuse me if I ask the obvious, but I am quite mySQL illiterate .
I am referring to a standard wordpress DB install, although this is not a wordpress specific question, but more mySQL general knowledge ..
If I want to change all the values of a certain field across all posts , I usually do :
UPDATE `wp_postmeta` SET `meta_value` = replace(meta_value, 'old_value', 'new_value') WHERE `meta_key` LIKE 'my_meta_key'
The problem is as follows :
In that specific DB , the INITIAL values were inserted with a white space prefix and suffix , e.g. :
"city name"
was actually inserted as
" city name "
of course , being a PHP person, I immediately thought of the trim() command - which I found out exists also in mySQL.
SO now I thought I could just do
UPDATE `wp_postmeta` SET `meta_value` = TRIM('meta_value') WHERE `meta_key` LIKE 'my_meta_key'
But It is not working ..
What Am I doing wrong ? I know that mySQL is "ticks sensetive" ( so to speak ) But I tried both 'meta_value' and `meta_value' ( sorry, markdown limitations seems to truncate the backticks)
Doing replace(meta_value, ' ', '') is dengaurous because some cities havea spaces in their name, and LTRIM RTRIM is somewhat not working for me as well
I also know there is a way to set a variable in sql ( #my_var )
but what is the correct syntax of doing that in my case ?
SET #my_var = `meta_value`
and then
UPDATE `wp_postmeta` TRIM(#my_var) WHERE `meta_key` LIKE 'my_meta_key'
I of course know how to fix it in a php loop (with trim() ) but I would like to learn what is the best way to do it in mySQL..
To refer to the column, you need backticks:
UPDATE `wp_postmeta`
SET `meta_value` = TRIM(`meta_value`)
WHERE `meta_key` LIKE 'my_meta_key';
Your expression TRIM('meta_value') is trimming the string 'meta_value', not the value in the column.
Hopefully I'm going about this the right way, if not I'm more than open to learning how this could be done better.
I need to pass a comma separated list of integers (always positive integers, no decimals) to a stored procedure. The stored procedure would then use the integers in an IN operator of the WHERE clause:
WHERE [PrimaryKey] IN (1,2,4,6,212);
The front-end is PHP and connection is made via ODBC, I've tried wrapping the parameter in single quotes and filtering them out in the stored procedure before the list gets to the query but that doesn't seem to work.
The error I'm getting is:
Conversion failed when converting the varchar value '1,2,4,6,212' to data type int.
I've never done this before and research so far has yielded no positive results.
Firstly, let's use a SQL Function to perform the split of the delimited data:
CREATE FUNCTION dbo.Split
(
#RowData nvarchar(2000),
#SplitOn nvarchar(5)
)
RETURNS #RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare #Cnt int
Set #Cnt = 1
While (Charindex(#SplitOn,#RowData)>0)
Begin
Insert Into #RtnValue (data)
Select
Data = ltrim(rtrim(Substring(#RowData,1,Charindex(#SplitOn,#RowData)-1)))
Set #RowData = Substring(#RowData,Charindex(#SplitOn,#RowData)+1,len(#RowData))
Set #Cnt = #Cnt + 1
End
Insert Into #RtnValue (data)
Select Data = ltrim(rtrim(#RowData))
Return
END
To use this, you would simply pass the function the delimited string as well as the delimiter, like this:
SELECT
*
FROM
TableName
WHERE
ColumnName IN (SELECT Data FROM dbo.Split(#DelimitedData, ','))
If you still have issues, due to the datatype, try:
SELECT
*
FROM
TableName
WHERE
ColumnName IN (SELECT CONVERT(int,Data) FROM dbo.Split(#DelimitedData, ','))
You can pass a comma separate list of values. However, you cannot use them as you like in an in statement. You can do something like this instead:
where ','+#List+',' like '%,'+PrimaryKey+',%'
That is, you like to see if the value is present. I'm using SQL Server syntax for concatenation because the question is tagged Microsoft.