www.webdeveloper.com
Results 1 to 7 of 7

Thread: PHP / HTML Table Odity

  1. #1
    Join Date
    Sep 2017
    Posts
    6

    PHP / HTML Table Odity

    Hi All,

    I am hoping you can help me, I have the PHP Code below:

    PHP Code:
    <?php

    $_SESSION
    ["Category_ID1"] = (int)$_POST["Category_ID1"];
    $id1 $_SESSION["Category_ID1"];
    $_SESSION["Category_ID2"] = (int)$_POST["Category_ID2"];
    $id2 $_SESSION["Category_ID2"];
    echo 
    $id1;
    echo
    "<br>";
    echo 
    $id2;
    echo
    "<br>";
    require_once(
    'db_init.php');

                        
    $con mysqli_connect($DBHOST$DBUSER$DBPASS$DBNAME) or die('Could not connect to database server.');

    if (
    mysqli_connect_errno())
    {
    echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
    }

    $strsql "SELECT c.Client_ID AS Client_ID, c.Title, c.Forenames, c.Surname, c.Company, cc.Client_Category_ID, lc.Category_ID , lc.Category, lc2.Category_ID , lc2.Category FROM tblClient AS c INNER JOIN tblClient_Category AS cc ON cc.Client_ID = c.Client_ID INNER JOIN tblLookUp_Category AS lc ON lc.Category_ID = cc.Category_ID INNER JOIN tblClient_Category AS cc2 ON cc2.Client_ID = c.Client_ID INNER JOIN tblLookUp_Category AS lc2 ON lc2.Category_ID = cc2.Category_ID WHERE cc2.Category_ID = $id1 AND cc.Category_ID = $id2 GROUP BY c.Surname, c.Client_ID ASC";
    //$strsql = "SELECT c.Title, c.Forenames, c.Surname, c.Company, cc.Client_Category_ID, lc.Category_ID , lc.Category FROM tblClient AS c INNER JOIN tblClient_Category AS cc ON cc.Client_ID = c.Client_ID INNER JOIN tblLookUp_Category AS lc ON lc.Category_ID = cc.Category_ID WHERE cc.Category_ID = $id1 GROUP BY c.Surname, c.Client_ID ASC";

    echo $strsql;
    echo
    "<br>";
    $raw_results mysqli_query($con,$strsql);

    if(
    mysqli_num_rows($raw_results) > 0){
    echo 
    "<table border='1'>";
    echo 
    "<tr>";
    echo
    "<th>Edit</th><th>Title</th><th>Forenames</th><th>Surname</th><th>Company</th>";
    echo 
    "</tr>";

    while(
    $results mysqli_fetch_array($raw_results)){
    echo 
    "<tr>";
                
    //echo"<td>";
                //echo"<form action=\"clientframe.php\" method=\"post\" enctype=\"multipart/form-data\" target=\"frame_e\">";
                //echo"<input type=\"hidden\" name=\"Client_ID\" value=".$results['Client_ID'].">";
                //echo"<input style=\"background-color: #4CAF50\" type=\"submit\" value=\"View\">";  
                //echo"</form>";
                //echo"</td>";
    echo "<td>";
    echo 
    $results['Title'];
    echo
    "</td>";
    echo 
    "<td>";
    echo 
    $results['Forenames'];
    echo
    "</td>";
    echo 
    "<td>";
    echo 
    $results['Surname'];
    echo
    "</td>";
    echo 
    "<td>";
    echo 
    $results['Company'];
    echo
    "</td>";
    echo 
    "</tr>";
    }
    echo 
    "</table>";
    }
            else{ 
    // if there is no matching rows do following
                
    echo "No results";
            }

    //mysqli_close($con);
    ?>
    In my web browser it echo's te right values, presents the SQL statement with the variables in place, and looks OK.
    When I view the page source though it's echoing 0's, inserting 0's in the SQL script, and showing "No Results" in the page source.

    HTML Code:
    <!DOCTYPE html>
    <html>
        <head> 
            <title>View Category Contacts</title>
            <link rel="stylesheet" href="nfsdb.css" type="text/css">
        </head>
        <body>
    
    0<br>0<br>
    
    SELECT c.Client_ID AS Client_ID, c.Title, c.Forenames, c.Surname, c.Company, cc.Client_Category_ID, lc.Category_ID , lc.Category, lc2.Category_ID , lc2.Category FROM tblClient AS c INNER JOIN tblClient_Category AS cc ON cc.Client_ID = c.Client_ID INNER JOIN tblLookUp_Category AS lc ON lc.Category_ID = cc.Category_ID INNER JOIN tblClient_Category AS cc2 ON cc2.Client_ID = c.Client_ID INNER JOIN tblLookUp_Category AS lc2 ON lc2.Category_ID = cc2.Category_ID WHERE cc2.Category_ID = 0 AND cc.Category_ID = 0 GROUP BY c.Surname, c.Client_ID ASC<br>No results        
        </body>
    </html>
    This only poses me a problem as I'm currently trying to debug why a clickable "view" button that I had on each line wasn't working, which if the page doesn't think the data is there, then that'd do it.

    But why is this displaying OK to the user in the browser?

    This seems odd, any assistance much apprecaited.

    Many Thanks,
    Graham

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    22,284
    Looks like $_POST["Category_ID1"] and $_POST["Category_ID1"] are either empty or unset (which (int) will convert to zero) or actually coming in as 0. So I'd start by checking the case-sensitive array keys to make sure they match what the form is using, and then add some defensive coding to make sure they have a non-zero value.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Apr 2017
    Posts
    70
    There is no session_start.

  4. #4
    Join Date
    Sep 2017
    Posts
    6
    Thanks for the assistance with this,

    I found a solution workaround this morning - I can' explain it but I've had to use multiple pages to click through and collect the variable, rather than just doing it over one click, so this is what I've ended up with...

    Step one:

    PHP Code:
    <form action="multiplesearch22.php" method="post" enctype="multipart/form-data" target="frame_d" id="usrfrm">
            <?php
            
    require_once('db_init.php');

                        
    $conn mysqli_connect($DBHOST$DBUSER$DBPASS$DBNAME) or die('Could not connect to database server.');
                        
    $sql "SELECT * FROM tblLookUp_Category GROUP BY Category, Category_ID ASC";
                        
                        echo 
    "Choose Category 1: ";
                        
    $result mysqli_query($conn,$sql);
                        echo 
    "<select name=\"Category_ID1\" form=\"usrfrm\">";
                        while (
    $row mysqli_fetch_array($result)) {
                        echo 
    "<option value='" $row['Category_ID'] . "'>" $row['Category'] . "</option>";
                        }
                        echo 
    "</select>";
                        
                        
                    
    ?>
                <input style="background-color: #4CAF50" type="submit" value="Select Category">
    Step two:

    PHP Code:
    <form action="multiplesearch3.php" method="post" enctype="multipart/form-data" target="frame_e" id="usrfrm">
            <?php
            $_SESSION
    ["Category_ID1"] = (int)$_POST["Category_ID1"];
            
    //echo $_SESSION["Category_ID1"];
            
    require_once('db_init.php');

                        
    $conn mysqli_connect($DBHOST$DBUSER$DBPASS$DBNAME) or die('Could not connect to database server.');
                        
    $sql "SELECT * FROM tblLookUp_Category GROUP BY Category, Category_ID ASC";
                        
                        
                        
    $result mysqli_query($conn,$sql);
                        echo 
    "Choose Category 2: ";
                        echo 
    "<select name=\"Category_ID2\" form=\"usrfrm\">";
                        while (
    $row mysqli_fetch_array($result)) {
                        echo 
    "<option value='" $row['Category_ID'] . "'>" $row['Category'] . "</option>";
                        }
                        echo 
    "</select>";
                        
                        
                    
    ?>
                <input style="background-color: #4CAF50" type="submit" value="Select Category">
            </form>
    Step three:
    PHP Code:
            <form action="multiplesearch33.php" method="post" enctype="multipart/form-data" target="frame_e" id="usrfrm">
            <?php
            
            
            
    //$_SESSION["Category_ID1"] = (int)$_POST["Category_ID1"];
            //echo $_SESSION["Category_ID1"];
            //echo "<br>";
            
    $_SESSION["Category_ID2"] = (int)$_POST["Category_ID2"];
            
    //echo $_SESSION["Category_ID2"];
            //echo "<br>";
            
    $id1 $_SESSION["Category_ID1"];
            
    $id2 $_SESSION["Category_ID2"];
            
    //echo $id1;
            //echo "<br>";
            //echo $id2;
            
    ?>
            <input style="background-color: #4CAF50" type="submit" value="Search">
            </form>
    Step 4
    PHP Code:
    [PHP]<?php

            
    //$_SESSION["Category_ID1"] = (int)$_POST["Category_ID1"];
            //echo $_SESSION["Category_ID1"];
            //echo "<br>";
            //$_SESSION["Category_ID2"] = (int)$_POST["Category_ID2"];
            //echo $_SESSION["Category_ID2"];
            //echo "<br>";
            
    $id1 $_SESSION["Category_ID1"];
            
    $id2 $_SESSION["Category_ID2"];
            
    //echo $id1;
            //echo "<br>";
            //echo $id2;
            
            
    require_once('db_init.php');
            
    $con mysqli_connect($DBHOST$DBUSER$DBPASS$DBNAME) or die('Could not connect to database server.');

            
    if (
    mysqli_connect_errno())
    {
    echo 
    "Failed to connect to MySQL: " mysqli_connect_error();
    }
    $strsql "SELECT c.Client_ID AS Client_ID, c.Title, c.Forenames, c.Surname, c.KnownAs, c.Company, c.Address_line1, c.Address_line2, c.Address_line3, c.Address_Town, c.Address_County, c.Address_Postcode, c.Address_Country, c.Telephone_Primary, c.Telephone_Secondary, c.EMail_Primary, c.Email_Secondary, cc.Client_Category_ID, lc.Category_ID , lc.Category, lc2.Category_ID , lc2.Category FROM tblClient AS c INNER JOIN tblClient_Category AS cc ON cc.Client_ID = c.Client_ID INNER JOIN tblLookUp_Category AS lc ON lc.Category_ID = cc.Category_ID INNER JOIN tblClient_Category AS cc2 ON cc2.Client_ID = c.Client_ID INNER JOIN tblLookUp_Category AS lc2 ON lc2.Category_ID = cc2.Category_ID WHERE cc2.Category_ID = $id1 AND cc.Category_ID = $id2 GROUP BY c.Surname, c.Client_ID ASC";
    $result mysqli_query($con,$strsql);

    echo 
    "<table border='1'>
    <tr>
    <th>Edit</th>
    <th>Title</th>
    <th>Forenames</th>
    <th>Surname</th>
    <th>Company</th>
    </tr>"
    ;

    while(
    $row mysqli_fetch_array($result))
    {
    echo 
    "<tr>";
                 echo
    "<td>";
                echo
    "<form action=\"clientframe.php\" method=\"post\" enctype=\"multipart/form-data\" target=\"frame_e\">";
                echo
    "<input type=\"hidden\" name=\"Client_ID\" value=".$row['Client_ID'].">";
                echo
    "<input style=\"background-color: #4CAF50\" type=\"submit\" value=\"View\">";  
                echo
    "</form>";
                 echo
    "</td>";
    echo 
    "<td>" $row['Title'] . "</td>";
    echo 
    "<td>" $row['Forenames'] . "</td>";
    echo 
    "<td>" $row['Surname'] . "</td>";
    echo 
    "<td>" $row['Company'] . "</td>";
    echo 
    "</tr>";
    }
    echo 
    "</table>";
            
            
    ?>
    [/PHP]

    I can't explain why this has needed this click-through process to "commit" the variable values as it were, but this has got me out of a sticky spot...

    Many Thanks,
    Graham

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,877
    As was already posted, you don't seem to start the session anywhere here. Obviously you are not showing us everything and ignoring that which you don't care to hear. IMHO "solving" this problem with 3-4 new scripts/forms/clicks doesn't look like any good solution. And mixing the HTML and the PHP as one mess of spaghetti is definitely a not an algorithm that any of us here would ever use.

    Good luck with this and all of your future endeavors.
    JG
    PS - If you're posting here you should be using:

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


    at the top of ALL php code while you develop it!

  6. #6
    Join Date
    Sep 2017
    Posts
    6
    Apologies, for the sake of every body's sanity I did put

    PHP Code:
    <?php
    session_start
    ();
    ?>
    At the start of all of those pages, they were just in a different code block on each of the pages.

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,877
    Something that s/b the first line of every script you write.

    Why the trailing ?> on that? You don't need it IF you learn not to co-mingle your languages.
    JG
    PS - If you're posting here you should be using:

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


    at the top of ALL php code while you develop it!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.37535 seconds
  • Memory Usage 2,974KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_html
  • (6)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates