Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-01-2012, 07:56 AM   PM User | #1
turkmeister
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
turkmeister is an unknown quantity at this point
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.
turkmeister is offline   Reply With Quote
Old 10-01-2012, 08:19 AM   PM User | #2
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
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.
stevenmw is offline   Reply With Quote
Old 10-01-2012, 08:27 AM   PM User | #3
turkmeister
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
turkmeister is an unknown quantity at this point
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 =]
turkmeister is offline   Reply With Quote
Old 10-01-2012, 08:32 AM   PM User | #4
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
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">
stevenmw is offline   Reply With Quote
Old 10-01-2012, 08:36 AM   PM User | #5
turkmeister
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
turkmeister is an unknown quantity at this point
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..
turkmeister is offline   Reply With Quote
Old 10-01-2012, 08:39 AM   PM User | #6
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
Should

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

PHP Code:
$ses_id $user->get('sesid'); 
?
stevenmw is offline   Reply With Quote
Old 10-01-2012, 08:44 AM   PM User | #7
turkmeister
New to the CF scene

 
Join Date: Oct 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
turkmeister is an unknown quantity at this point
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..
turkmeister is offline   Reply With Quote
Old 10-01-2012, 09:04 AM   PM User | #8
stevenmw
Regular Coder

 
stevenmw's Avatar
 
Join Date: Jun 2007
Location: OK
Posts: 446
Thanks: 26
Thanked 30 Times in 30 Posts
stevenmw is an unknown quantity at this point
Oh, no you can't use PHP variables in html input fields.

Last edited by stevenmw; 10-01-2012 at 09:11 AM..
stevenmw is offline   Reply With Quote
Old 10-01-2012, 07:50 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,194
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:53 PM.


Advertisement
Log in to turn off these ads.