PHPとMaria DBを用いてスケジュール管理のデータベースを作成しております。
入力FormからテキストデータはSQLの方に格納され、読みだした時にも正常に読み出しされるのですが、チェックボックスを使用して配列でSQLに格納したデータを読みだした時にチェックボックスにて表示したいのですが、うまく表示されません。
Warning: array_diff(): Argument #1 is not an array in C:\xampp\xxx\xxx.php on line 163
※$diffValue = array_diff($row["productarry"], $product);のところ
SQL側には"productarry"というカラムは作成されていて、中身も入っているように見えます。
配列以外のデータはテキストで表示されているので問題ないとは思うのですが、、、
SQLからのデータを配列として見られていないのか、そもそも配列として認識されていないのか、はたまた別理由なのか見当が付きません。
ご指導いただければと思います。
PHP
1 //MySQLデータベースに接続する 2 $pdo = new PDO($dsn, $user, $password); 3 $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 4 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 5 $sql = "SELECT * FROM データベース名 WHERE id LIKE(:id)"; 6 $stm = $pdo->prepare($sql); 7 $stm->bindValue(':id', "{$id}", PDO::PARAM_STR); 8 $stm->execute(); 9 $resulte = $stm->fetchAll(PDO::FETCH_ASSOC); 10 foreach ($resulte as $row) { 11 12********************** 13if (isSet($row["productarry"])){ 14 // 「product」かどうか確認する 15 $product = ['製品A','製品B','製品C','製品D','製品E']; 16 $diffValue = array_diff($row["productarry"], $product); 17 // 規定外の値が含まれていなければOK 18 if (count($diffValue)==0){ 19 // チェックされている値を取り出す 20 $productarry = $row["productarry"]; 21 } else { 22 $productarry = []; 23 } 24 } 25 26 27 echo "<div class='form-group'>", "<div class='col-lg-8'>", "<div class='checkbox' id='product'>", "<h4>", "Product Type", "</h4>", 28 "<label>", "<input type='checkbox' name='productarry[]' value='製品A' $productarry", "製品A", "</label>", "<span style='margin-right: 25px;'>", "</span>", 29 "<label>", "<input type='checkbox' name='productarry[]' value='製品B' > $productarry", "製品B", "</label>", "<span style='margin-right: 25px;'>", "</span>", 30 "<label>", "<input type='checkbox' name='productarry[]' value='製品C' > $productarry ", "製品C", "</label>", "<span style='margin-right: 25px;'>", "</span>", 31 "<label>", "<input type='checkbox' name='productarry[]' value='製品D' > $productarry", "製品D", "</label>", "<span style='margin-right: 25px;'>", "</span>", 32 "<label>", "<input type='checkbox' name='productarry[]' value='製品E' > $productarry", "製品E", "</label>", "<span style='margin-right: 25px;'>", "</span>", 33 "<br>", 34 "<br>", 35 "</div>", 36 "</div>", 37 "</div>";