0

I am ever so close to resolving the issue I have with taking some data from my tables, basically I have a users tables, a category tables and a join tables which has foreign keys assigned to the user_id from the users table and the cat_id taken from the cats table.

I basically want to show different categories if they have been assigned to you and in my database in the join table I can select a user id and cat id and that has its own id in a row. the trouble i face is getting those categories to show against the users id when logged in?

Here is my code so far:

<?php
require ('../db_con.php');

    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    // BUILD AND DISPLAY THE CATEGORY LIST (RICOH BUILD)
    function build_cat_list()
    {

    global $dbc;

    $user = $_SESSION['user'];

    $q = sprintf("
        SELECT
           count(*)
        FROM
            cats
        INNER JOIN
            user_cat_join
        ON
            cats.cat_id = user_cat_join.cat_id
        WHERE
            user_cat_join.user_id = '%s'
        ",
        mysqli_real_escape_string($dbc, $user)
    );

    $r = mysqli_query ($dbc, $q); // Run the query.

    // FETCH AND PRINT ALL THE RECORDS
    while ($row = mysqli_fetch_array($r)) {

    echo '
    <a href="view_cat.php?cat_id='.$row["cat_id"].'">
        <div class="indexBox"">
            <div class="indexBoxHeader"  style="background-color:'.$row["cat_color"].'"><p>'
                .$row["cat_icon"].'</p>
                </div>
            <div class="indexBoxFooter">
                <p>'.$row["cat_name"].'</p>
            </div>
        </div>
    </a>';

    }

}
?>

As it stand, it is running through the entire script because what it does it builds a little box with an icon, a background color and a name printed on the box. But at the moment it renders the box but tells me there are some undefined variables which I know what the error means but struggling to overcome it?

Notice: Undefined index: cat_id in C:\MAMP\htdocs\functions\functions.inc.php on line 36

Notice: Undefined index: cat_color in C:\MAMP\htdocs\functions\functions.inc.php on line 38

Notice: Undefined index: cat_icon in C:\MAMP\htdocs\functions\functions.inc.php on line 39

Notice: Undefined index: cat_name in C:\MAMP\htdocs\functions\functions.inc.php on line 42

And of course the above variables are referring to the bottom part where it actually renders out the box. It will only show me these undefined variables if I use count(*) but if I specify them like cat_name, cat_color etc etc I get nothing and no errors?

PhpDude
  • 1,542
  • 2
  • 18
  • 33
  • 2
    Your query is only selecting `count(*)` -> `SELECT count(*) FROM ...`. So where do you expect those column names are to come from? – Sean Jun 24 '15 at 16:00
  • Doh... heehhehe Copy/Paste will get you every time – RiggsFolly Jun 24 '15 at 16:03
  • sidenote: has the session been started? – Funk Forty Niner Jun 24 '15 at 16:04
  • Rather than using the `mysqli_real_escape_string` and its brethen, look into using prepared statements. They are easier to use in the long run. https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Andy Lester Jun 24 '15 at 16:04
  • @Fred-ii- Hey yeah my session is being started in my header which is included on the page – PhpDude Jun 24 '15 at 16:09
  • When you specify your column names, you are probably getting an error like `Column 'cat_id' in field list is ambiguous` if you have a column name that is in both tables, unless you specify the table `cats.cat_id` – Sean Jun 24 '15 at 16:10
  • @Sean so now I asking for cats.cat_id the error message has gone but no errors or anything? – PhpDude Jun 24 '15 at 16:12

2 Answers2

3

Change your query to select the columns that you actually want to use

$q = sprintf("
        SELECT
            cats.cat_id,cat_color,cat_icon, cat_name
        FROM cats
        INNER JOIN user_cat_join
        ON cats.cat_id = user_cat_join.cat_id
        WHERE user_cat_join.user_id = '%s'
        ",
        mysqli_real_escape_string($dbc, $user) );

It would be better to use a prepared statement here but to keep it simple!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

Run the query using:

$r = mysqli_query($dbc,$q)
or die ("Couldn't execute query: ".mysqli_error($dbc));

This will give you the report of what error your query is running into and you can adjust the query accordingly

Change the query as RiggsFoly says and check that $user value is the right one.

As per your comments you are passing to the query in your script a value that is different from what you tested in phpmyadmin. Probably $user in your code is:

  1. The wrong value to be put in the query;
  2. The right value but for it there are no data.

Run the query in phpmyadmin with the $user value you echo in the code and you will understand what is your case.

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • `mysqli_error()` requires the db link -> `mysqli_error($dbc)`. see http://php.net/manual/en/mysqli.error.php – Sean Jun 24 '15 at 16:01
  • 1
    If you look closely. the query works fine, it is just not selecting any of the columns that they then try and use – RiggsFolly Jun 24 '15 at 16:02
  • you are right! will edit the answer. It's a bit of time since I last used mysql or mysqli... – Lelio Faieta Jun 24 '15 at 16:02
  • @RiggsFolly but OP also says: "but if I specify them like cat_name, cat_color etc etc I get nothing and no errors?" – Lelio Faieta Jun 24 '15 at 16:04
  • Here is what I get from using the suggested: `Couldn't execute query: Column 'cat_id' in field list is ambiguous` – PhpDude Jun 24 '15 at 16:08
  • @phpcoder you have to specify the table name of the table you want the cat_id value to come from. See RiggsFolly for his updated answer – Lelio Faieta Jun 24 '15 at 16:10
  • @LelioFaieta I have amended it to reflect the suggestion but now I get no return – PhpDude Jun 24 '15 at 16:13
  • at this point it is a matter of data. Probably, with the condition you have set the query finds no data. You don't see any error because the query is working fine on a syntax side but you have to check data and join. Maybe you can edit your question to set up a sqlfiddle? – Lelio Faieta Jun 24 '15 at 16:17
  • Just run your query using phpMyAdmin or whatever you use. Does the query actually return any rows? – RiggsFolly Jun 24 '15 at 16:39
  • Interestingly, I changed it to specify that user in phpymyadmin and it does return the two rows I was expecting. However, there is something obviously a miss with my code because it stlil shows blank? – PhpDude Jun 24 '15 at 17:46
  • @phpcoder and if you echo $user do you get the expected value? – Lelio Faieta Jun 24 '15 at 17:59
  • If I echo out $user it gives me the session number – PhpDude Jun 24 '15 at 18:05
  • Is it what you expected? Is that value what you replaced in your query when you run it in phpmyadmin? – Lelio Faieta Jun 24 '15 at 18:07
  • I replaced `WHERE user_cat_join.user_id = '%s'` with `WHERE user_cat_join.user_id = 11` in phpmyadmin and it returned the two rows I was expecting – PhpDude Jun 24 '15 at 18:08
  • No, 11 is the user ID i knew of to test against - I understand my code above makes a reference to user who is logged in and then knows which categories to show? – PhpDude Jun 24 '15 at 18:10