How to insert an array into the database appropriately

Source: Internet
Author: User
How do I insert an array into the database properly?
HTML code:


Picture 1
Picture 2
Picture 3


PHP Code:
Picture 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--------------------
defined in the database:
SQL Code
Data varchar (+) NOT NULL
------Solution--------------------
PHP Code
foreach ($attachment as $v) mysql_query ("Insert ..."); Do not know your field, can only write this.
------Solution--------------------
The value of the array is taken out and stored in the library, separated by special characters, need to be taken out to deal with
------Solution--------------------
Compare support for 3 floor recommendations
------Solution--------------------
Storage time:
$date = "1.JGP/1.JGP";
By the time:
List ($one, $two) = Split (' [/.-] ', $date);
echo "one: $one; one: $two
\ n ";

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

、、
Second, is a character array, on the third floor, the characters are separated, and stored together into the database.
------Solution--------------------
All attachments should belong to a topic and do not need to be modified frequently, a field is done. Restore it when you need it.

PHP code
 $arr =array (' 1.jpg ', ' 2.jpg ', ' 3.jpg '); $value = Serialize ($arr); 
------Solution--------------------
Anyway, after you use Addslashes, no one knows what the user is submitting.

above the json_encode,serialize, after all to addslashes, avoid the quotation marks or the user malicious injection caused unnecessary trouble, out of the library time strip_slashes.

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

In any case, use addslashes after storage, no one knows what the user is committing.

above the json_encode,serialize, after all to addslashes, avoid the quotation marks or the user malicious injection caused unnecessary trouble, out of the library time strip_slashes.
  • 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.