-1
<?php
try{
    session_start();
    if(!isset($_SESSION['on'])) {
    header("location:homepage.php"); 
    }
    include('dbconnectie.php');
    $query = $db->prepare("SELECT id_img FROM cart WHERE id_u = :id");
    $query->bindParam("id", $_SESSION['id_u']);
    $query->execute();
    $result = $query->fetchALL(PDO::FETCH_ASSOC);
    $query = $db->prepare("SELECT * FROM shop WHERE id_img = :id_img");
    $query->bindParam("id_img", $result);
    $query->execute();
    $result1 = $query->fetchALL(PDO::FETCH_ASSOC);
    echo "<table>";
            foreach($result1 as &$data) {
                echo "<tr>";
                    $img = $data['img_url'];
                    echo "<td>" . $data["brand"] . "</td>";
                    echo "<td>" . $data["model"] . "</td>";
                    echo "<td> Condition: " . $data["cond"] . "/10 </td>";
                    echo "<td> Prijs: &dollar; " . number_format($data["price"],2,",",".") . "</td>";
                    echo "<td> <img src='$img' width='400' height='300' ></img> </td>";
                    echo "<td>" . $data['id_img'] . "</td>";
                echo "</tr>";
            }
    echo "</table>";
    } catch(PDOException $e) {
        die("Error!: " . $e->getMessage());
    }
?>
<html>
    <title>Just for kicks</title>
    <header>
         <?php
            include("#nav.php");

        ?>
    </header>
</html>

on line 13 it says: Notice: Array to string conversion in /storage/ssd2/719/5658719/public_html/cart.php on line 13 ?>

I am unsure how to fix this. What I'm trying to do is get all the id_img's from table cart that correspond with $_SESSION['id_u'] which in the table is id_u. Then what I'm trying to do is for every id_img i'm trying to get all of the details from table shop which corresponds to id_img. like brand, model and cond.

Someone in the comments referred me to a possible answer but even with that I'm unable to understand how to fix the problem I'm having.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Arjen
  • 87
  • 7

2 Answers2

0

First of all at line 11 you store in $result an array.

public array PDOStatement::fetchAll ([ int $fetch_style [, mixed $fetch_argument [, array $ctor_args = array() ]]] )

and it looks like this (more or less):

array('id' => '123',
    '0' => '123')

You should try: $query->bindParam("id_img", $result[0]);

So you will bring string as variable in your query.

I suggest you to use var_dump($result) after you store data of your fetch in $result so you will have better view on whats going on with your variable.

  • Ok so I'm a bit confused here, if I replace line 11 with what u put and line 13 with what u wrote, it still says it's not working. – Arjen May 28 '18 at 21:33
  • You do not have to replace line 11. First line of code is description of `fetchAll()` function. You have to put after 11 line `var_dump($result)` to check what cointain your $result and then change properly line 13. –  May 29 '18 at 06:17
0

You may need another foreach over $result if there are several possible records. The main thing is to try and reproduce how you did the foreach below. Change the reference in "id_img", $result); so the $result element is like $data['id_img']

-Nigel Ren

Arjen
  • 87
  • 7