Сохраните выбранные флажки в таблице базы данных MySQL, используя PDOPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Сохраните выбранные флажки в таблице базы данных MySQL, используя PDO

Сообщение Anonymous »


I have a list of optional clothing items as checkboxes, there may be a greater number than the 5 below.

Код: Выделить всё

shoes, pants, skirt, socks, jacket //list of possible choices
A comma-separated array is created in jquery of the chosen item. Let's say the following are chosen:

Код: Выделить всё

shoes, socks, jacket //array posted as $_POST['clothes']
In the db, each customer has these options in the clothes table with 'yes' or 'no' under the clothing items. However, the clothing item are named a bit differently but map out to the same options:
'clothes' table before insert

Код: Выделить всё

customer_id  dress_shoes  elegant_pants  long_skirt  ankle_socks  biker_jacket
1            no            yes           no           no           no
With the $_POST['clothes'], I'm trying to loop through the array, updating the corresponding fields to , and the non corresponding fields to in the db. I'm having a hard time doing that.
'clothes' table after insert

Код: Выделить всё

customer_id  dress_shoes  elegant_pants  long_skirt  ankle_socks  biker_jacket
1            yes            no           no          yes          yes
I tried using

Код: Выделить всё

array_intersect()
to get the items to mark as 'yes':

Код: Выделить всё

$clothesArray = array("shoes", "socks", "jacket"); // Posted clothes
$clothesArrayAll = array("shoes", "pants", "skirt", "socks", "jacket"); // All clothes
$common = array_intersect($clothesArrayAll,$clothesArray);
print_r($common);
Array ( [0] => shoes [3] => socks [4] => jacket )
I'm trying to somehow loop through the

Код: Выделить всё

$clothesArrayAll
, give a 'yes' to common clothes, and a 'no' to all others in the array. Then, I'm trying to update the 'clothes' table via PDO, setting each corresponding field to a 'yes' or 'no' in the most efficient way. I'm stuck after getting the common clothes array above and not sure how to proceed.


Источник: https://stackoverflow.com/questions/125 ... -using-pdo
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»