简体   繁体   中英

Relational database querying failing

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?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM