How to insert an array into a database?

Source: Internet
Author: User
How to insert an array to the database? Html code: & lt; inputtype & quot; checkbox & quot; name & quot; attachment [] & quot; value & quot; 1.jgp& quot; & gt; image 1 & lt; inputtype & quot; checkbox & quot; name & quot; attachment how to insert an array into the database?
Html code:


Image 1
Image 2
Image 3


Php code:
// Image attachment area
$ Attachment = $ this-> var ['GP _ attachment '];

Foreach ($ attachment as $ value ){
P ($ value );
// Save the value to the database
}

Array format
Array
(
[0] => 1.jgp
[1] => 2.jpg
)
Php: 5.3.10


------ Solution --------------------
Define in the database:
SQL code
Data varchar (100) not null
------ Solution --------------------
PHP code
Foreach ($ attachment as $ v) mysql_query ("insert..."); // You do not know your field. you can only write it like this.
------ Solution --------------------
Extract the value of the array to the database and separate it with special characters. if necessary, extract it before processing.
------ Solution --------------------
Suggestions for supporting the third floor
------ Solution --------------------
Storage:
$ Date = "1.jgp/ 1.jgp ";
Time to use:
List ($ one, $ two) = split ('[/.-]', $ date );
Echo "one: $ one; two: $ two
\ N ";

------ Solution --------------------
First, do you think you are a character array or a number array? if you use a number array, use foreach ($ attachment as $ v)

,,
The second is the character array, which is separated by characters on the third floor and stored in the database together.
------ Solution --------------------
All attachments should belong to one topic and do not need to be modified frequently. you can use one field to restore it if necessary.

PHP code
$Arr=array('1.jpg ', '2.jpg', '3.jpg'); $ value = serialize ($ arr );
------ Solution --------------------
In any case, use addslashes to import data to the database. no one knows what the user submits.

The preceding json_encode and serialize must be followed by addslashes to avoid quotation marks or unnecessary troubles caused by malicious injection. strip_slashes is used for outbound delivery.

------ Solution --------------------
Serialization
------ Solution --------------------
Discussion

In any case, use addslashes to import data to the database. no one knows what the user submits.

The preceding json_encode and serialize must be followed by addslashes to avoid quotation marks or unnecessary troubles caused by malicious injection. strip_slashes is used for outbound delivery.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.