Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mysql not retuning query need help

    I am very new to php and mysql. I have been basically taking a crash course in it to do things for my website. I am having a proble of returning a query from mysql db table. Here is my code:

    This first bit is the page where I get the variables I need then I am posting them to the next page. Here it is.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Hit Entry Search</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
    <?php
    $user = JFactory::getUser();
    $ses_id = $user->get('id');
    ?>
    <form action="search.php" method="POST">
    <input type="text" name="query" />
    <input type="hidden" name=$ses_id value="sesid" />
    <input type="submit" value="Search" />
    </form>
    </body>
    </html>


    What it does is get what the user wants to search by and it get the user id for this user currently logged in. The jfactory is from joomla when I load the next page I lose the jfactory session so I have to grab the ID here. The next bit uses the 'query' and 'sesid' that is sent to it and puts them into variables. My query is trying to ask to search all of the table and return any that have both of these values on the same record and then display them.. I do not understand why it is not working. here it is.. some information is ** out for security reasons.

    <?php
    mysql_connect("*********", "******", "******") or die("Error

    connecting to database: ".mysql_error());
    /*
    localhost - it's location of the mysql server, usually localhost
    root - your username
    third is your password

    if connection fails it will stop loading the page and display an error
    */

    mysql_select_db("mtu1225803133696") or die(mysql_error());
    /* mtu1225803133696 is the name of database */
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
    <?php



    $query = $_POST['query'];
    $ses_id = $_POST['sesid'];
    // gets value sent over search form

    $min_length = 2;
    // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

    $query = htmlspecialchars($query);
    // changes characters used in html to their equivalents, for example: < to &gt;

    $query = mysql_real_escape_string($query);
    // makes sure nobody uses SQL injection

    $raw_results = mysql_query("SELECT * FROM jos_chronoforms_data_hitsubmit
    WHERE (`cf_user_id`='$sesid') AND (`cf_reqname`='$query')") or die(mysql_error());

    // * means that it selects all fields, you can also write: `id`, `title`, `text`
    // articles is the name of our table

    // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
    // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
    // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '%

    $query'

    if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

    while($results = mysql_fetch_array($raw_results)){
    // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

    echo "<p><h3>".$results['cf_reqname']."</h3>".$results['cf_reqdescr']."</p>";
    // posts results gotten from database(title and text) you can also show id ($results['id'])
    }

    }
    else{ // if there is no matching rows do following
    echo "No requester found by that name. Either you have not done any hits for this requester or you entered the name

    incorrectly. Names must be entered the same as you stored them.";
    }

    }
    else{ // if query length is less than minimum
    echo "You must enter at least 2 characters ";
    }
    ?>
    </body>
    </html>


    If I search and it should be a matching search it still returns no results found. Any help is greatly appreciated.

  • #2
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Please, make sure to wrap your code.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Hit Entry Search</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
    <?php
    $user = JFactory::getUser();
       $ses_id = $user->get('id');
    ?>
        <form action="search.php" method="POST">
            <input type="text" name="query" />
    <input type="hidden" name="ses_id" value="sesid" />
            <input type="submit" value="Search" />
        </form>
    </body>
    </html>
    The highlighted line in your code didn't have the " and "

    Looking at your PHP I assume you are following a tutorial. i see you managed to change the DB log in credentials but I'm thinking maybe you overlooked this.

    PHP Code:
    mysql_select_db("mtu1225803133696") or die(mysql_error());
        
    /* mtu1225803133696 is the name of database  */ 
    I'm sure I missed something, but maybe you can give those things a try.

    Good luck.

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is the actual table name. I didnt think it would hurt to leave that in. should I star it out?. I will change the part you mentioned and see if that helps. thank you so much =]

  • #4
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Oh, your database is actually named mtu1225803133696? My fault.. The input name does need the quotations though for sure.

    Code:
    <input type="text" name="sesid" value="sesid">

  • #5
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK that didn't work. I added an echo on the search.php(second bit of code) for both values and it returned the search that gets typed in but not the sesid. Any ideas? It has to be something wrong with the first bit. I will try to echo right after the jfactory to make sure it is grabbing the logged in userid and post what I get.

    edit: yup it is grabbing the correct ID and putting into the variable $ses_id
    Last edited by turkmeister; 10-01-2012 at 08:38 AM.

  • #6
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Should

    PHP Code:
    $ses_id $user->get('id'); 
    be

    PHP Code:
    $ses_id $user->get('sesid'); 
    ?

  • #7
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i echoed after that bit and it is returning the correct value and storing it into $ses_id. I think the problem might be in trying to use a variable in the input line to post to the next page. I could not find the proper way to use a variable in there like that. I even tried using the quotes you said I need and added the $ like "$ses_id" but that doesnt work either for passing it to the next page. LOL I am a noob and I am stumped. =]

    edit: should I try creating a session in php and try passing it that way? the main problem I am having is the joomla session stops when I goto my code on the second bit or I would have the jfactory statement there and not have to send it. Maybe I should try to find a way to keep the jfactory alive since the code is leaving the template and being on a page by iteself? I could add include statements or something.. idk
    Last edited by turkmeister; 10-01-2012 at 08:51 AM.

  • #8
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Oh, no you can't use PHP variables in html input fields.
    Last edited by stevenmw; 10-01-2012 at 09:11 AM.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Quote Originally Posted by stevenmw View Post
    Oh, no you can't use PHP variables in html input fields.
    Well...strictly speaking, that's true.

    But you CAN achieve the effect easily enough:
    Code:
    <?php
    $user = JFactory::getUser();
    $ses_id = $user->get('id');
    ?>
    <form action="search.php" method="POST">
        <input type="text" name="query" />
        <input type="hidden" name="sesid" value="<?php echo $ses_id;?>" />
        <input type="submit" value="Search" />
    </form>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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