Inserting wrong data in table wordpress - php

When I insert the serialize data in the postmeta table the data is modified while inserting like:
s:107:"a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}";
but when I print this data before inserting it is displaying this that is right:
a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}
while inserting it is automatically add s:107:" in front of data or "; in back of data.
Can any one please tell me why it is inserting like this.
Thanks in Advance

The s means string.
107 stands for the size of the string.

Related

Saving dynamic text box value to mysql PHP

While creating an invoice system, I have the code to add or remove the text box and section it's working fine. Once I try to save the data to MySQL its not working. I tried to insert the JSON output.
it's the JSON output of $_POST
{"sectionName":{"1":"1","2":"2"},"productCode":{"1":{"1":"1","2":"2"},"2":{"1":"1","2":"2"}},"productName":{"1":{"1":"1","2":"2"},"2":{"1":"1","2":"2"}},"quantity":{"1":{"1":"1","2":"2"},"2":{"1":"1","2":"2"}},"price":{"1":{"1":"1","2":"2"},"2":{"1":"1","2":"2"}},"notes":"","userId":"123456","invoice_btn":"Save Invoice","subTotal":"6","taxRate":"","taxAmount":"","totalAftertax":"","amountPaid":"","amountDue":""}
I not getting any idea how to insert this data to mySQL
can u provide ur sql field? like field name and type.
but if its data string and wanna insert it on single field, try wrap ur json with single quotes like
'{"sectionName":{"1":"1","2":"2"},"productCode":{"1":{"1":"1","2":"2"},"2":{"1":"1","2":"2"}},"productName":{"1":{"1":"1","2":"2"},"2":{"1":"1","2":"2"}},"quantity":{"1":{"1":"1","2":"2"},"2":{"1":"1","2":"2"}},"price":{"1":{"1":"1","2":"2"},"2":{"1":"1","2":"2"}},"notes":"","userId":"123456","invoice_btn":"Save Invoice","subTotal":"6","taxRate":"","taxAmount":"","totalAftertax":"","amountPaid":"","amountDue":""}'

How can I show all array from 0 to 4 individually

I really need your help To solve this array problem. Basically want to show each array values on wp data table. Its store on xampp wp_option table.
How can i use query to show them on data table.
Example of my databse: https://image.prntscr.com/image/Vna_-ux7T4mNEGPZAq1lrA.png
[Its shows the table column and rows]
Here are the example of my option_value json files .
{"id":1,"number":false,"input":"My table is now working","textarea":"Go to hell , you will find there","select":false,"radio":false,"hidden":false,"myFile":"C:\\\\fakepath\\\\FI.txt","fineuploader":false,"date":false,"checkbox":false,"button":false,"reset":false}
Its a json_encoded data. Now by global wpdb I want to show them in wordpress data table.
I just need the query through which i can loop all the keys and values and show them on data table.
Please help me on this.

String count added automatically while inserting data into table wordpress

When I insert the serialize data in the postmeta table the data is modified while inserting like:
s:107:"a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}";
but when I print this data before inserting it is displaying this that is right:
a:4:{s:6:"page_1";s:5:"third";s:6:"page_2";s:5:"first";s:6:"page_3";s:6:"fourth";s:6:"page_4";s:5:"fifth";}
while inserting it is automatically add string count s:107:" in front of data or "; in back of data.
Please suggest me how i resolve this.

Serialized array breaks on retrieval from database

I am saving data in a mysql database. This data is an array and the content is different data of the current user that is logged in to my system.
I do this when I save to the database:
$data = addslashes(serialize($array));
then
"UPDATE or INSERT INTO TABLE SET ... data = '$data';"
Now, the data are saved correctly since the insert or update statement return valid from my php code.
My problem is when I try to un-serialize it, it returns false and a notice is shown in my page.
What am I doing wrong?
I will bet the field in your mysql database is not big enough to save all the characters. That is why, when you un-serialize it you get an notice and nothing in return.
Try to increase the field to a MEDIUMBLOB or MEDIUMTEXT (maximum length of 16,777,215) or LONGBLOB or LONGTEXT (maximum length of 4,294,967,295) like this:
ALTER TABLE your_table MODIFY COLUMN column_name MEDIUMTEXT /* other properties*/;
And try to save and read your data again.
Now, if your data is over 4,294,967,295 (which is LONGBLOB or LONGTEXT), maybe you should check what kind of data you saving and maybe filter or remove unwanted some.
After you're getting the data from the table, are you removing the slashes before unserialize function.
Try inserting without the addslashes() and add slashes before it's taken to the array.

How to use PHP Load Data Infile to replace the current copy of the table with the new one?

We get a new copy of data in a pipe-delimited text file from time to time. I have to refresh the table or in other words, replace the existing copy with that of the newly generated copy (output.txt).
Any ideas are highly appreciated. Thank you..
TRUNCATE Table elements;
LOAD DATA INFILE '/data/out.txt' IGNORE INTO TABLE elements FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n' (ID, Title, date, time);
I agree more information is needed to answer your question. As I understand it you need to empty a database table and reload it with the information in the text file you received, which sounds like a CSV file that uses pipes instead of commas. Do I have this right?
So you need to...
Step 1 - Get your information out of the text file.
You can try something like fgetcsv() (setting the delimiter to '|'), or you could just use fgets() and use explode() to put the data in an array.
Step 2 - Insert data into your database table
Loop through you data until you have it all in there. If all goes well then...
Step 3 - Delete the old data
It might seem easier to empty the database first, and then add your data. You could do that, but if something goes wrong with the new data then what? Now you're stuck with an empty database table until you fix it. Depending on what this is used for that could be undesirable.

Categories