将PHP数组作为文本插入MySQL数据库

I am willing to insert an array to a database as normal text.

$subjectcodes[1] = "";
$subjectcodes[2] = "";
$subjectcodes[3] = "";
$subjectcodes[4] = "";
$subjectcodes[5] = "";
$subjectcodes[6] = "";
$subjectcodes[7] = "";

etc...

I give my array an stringname called list_update. So you get this ( I am having "\" so it doesn't see it as PHP )

    $list_update="
    \$subjectcodes\[1\] = \"\";
    \$subjectcodes\[2\] = \"\";
    \$subjectcodes\[3\] = \"\";
    \$subjectcodes\[4\] = \"\";
    \$subjectcodes\[5\] = \"\";
    \$subjectcodes\[6\] = \"\";
    \$subjectcodes\[7\] = \"\";
"

Now when I use my query to input my list into a table it doesn't do anything.

 mysqli_query($connect,"INSERT INTO subjects (list)
        VALUES (" . $list_update . ") WHERE user='". $userid ."'");

Does someone know how I can solve this?

Thank you very much in advance ;)

Do it as below:

$subjectcodes[1] = "";
$subjectcodes[2] = "";
$subjectcodes[3] = "";
$subjectcodes[4] = "";
$subjectcodes[5] = "";
$subjectcodes[6] = "";
$subjectcodes[7] = "";

json_encode($subjectcodes);

Insert into your DB.

After you fetch it from DB, then decode it as below:

$array = json_decode($yourArrayItemFromDB);

ANd you will have your original array like $subjectcodes.

Using this way, you can not only save arrays, but you can also save objects in DB.

A PHP array or object or other complex data structure cannot be transported or stored or otherwise used outside of a running PHP script. If you want to persist such a complex data structure beyond a single run of a script, you need to serialize it. That just means to put the structure into a "lower common denominator" that can be handled by things other than PHP, like databases, text files, sockets. The standard PHP function serialize is just a format to express such a thing, it serializes a data structure into a string representation that's unique to PHP and can be reversed into a PHP object using unserialize. There are many other formats though, like JSON or XML.

From here