-1

I'm trying to compare products and I'm already finished. I just have a problem that my product features are not under the right product names because I need to fill the gaps between with empty <td></td>.

enter image description here

Here is my code from the function that fills the values.

function datatable($id)
{
 $conn = connection();
 $productPost = $_POST["product"];

 $sqlSpecTitle = "Select title as title from product where uid = '$id'";
 $resultTitle = mysqli_query($conn, sqlSpecTitle) or die("database error:" . mysqli_error($conn));
 
 foreach ($productPost as $product)
 {
   $sqlSpecValue = "Select productname, title, value from text join product on uid = uid join feature on uid = uid where productname = '$product" and uid = '$id';
   $resultValue = mysqli_query($conn, $sqlSpecValue or die("database error:" . mysqli_error($conn));

  if(mysqli_num_row($resultValue) > 0
  {
    while($row = mysqli_fetch_assoc($resultTitle))
    {
      echo "<td>" . $row['title'] . "<td>";
    }
    while ($row = mysqli_fetch_assoc($resultValue))
    {
     if($row['value'] == null)
     {
      echo "<td>" . "empty" . "<td>";
     }
     else
     {
       echo "<td> . $row['value'] . "</td>";
     }
    }
   }
  }
 }

The productnames are getting filled in another function that is as much the same.

function headerTable()
    {
        $conn = connection();
        $productPost = $_POST["product"];

        foreach ($productPost as $product) {
            $sqlSpecValue = "SELECT productname, title, value from text
            join product on uid = uid
            join feature on uid = uid
            where productname = '$product';
            $resultValue = mysqli_query($conn, $sqlSpecValue) or die("database error:" . mysqli_error($conn));


            $row = mysqli_fetch_assoc($resultValue);
            echo "<td id='product'>" . $row['productname'] . "</td>";
        }
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
skeler137
  • 9
  • 2
  • `$foreach ($productPost as product)` Is that your actual code with the `$` before `foreach` but missing from `product`? – brombeer Oct 20 '21 at 07:39
  • Ah sry. I did a pasting mistake there. I changed it. Thx for your reply. – skeler137 Oct 20 '21 at 07:44
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Oct 20 '21 at 09:35
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Oct 20 '21 at 09:35

1 Answers1

0

You make a "join" over between your tables, so you only get data if you have something in "text" table. Just switch to "right join" and it should work.

Nalar
  • 1
  • I get all the data I need. I just need to fill empty 's if there is no value for the specified productname. – skeler137 Oct 20 '21 at 08:09
  • Yep, but by using right join, you'll get null value for product/feature when text is not filled, instead of not having data. With that, your "if" block should work. – Nalar Oct 20 '21 at 08:36
  • I put the data into an array and investigated it I can't see any "null" values in there. The problem here is that $row['value'] always gets the whole data of my result. – skeler137 Oct 20 '21 at 09:05