Forum Moderators: coopster
$class_name = $_POST['class_name'];
$query = "SELECT classname_id FROM classnames WHERE classname_text ='" . $class_name . "'";
my query works if i change the class name in the database to dog, and change my script to
$query = "SELECT classname_id FROM classnames WHERE classname_text ='dog'";
there's something about the php var and my quotes.
please help!
thanks from jennifer
Are there any special characters within the database that might be interfering with the php code? A common example would be a word containing ' that can close quotes under certain circumstances.
Another thing you could do is try getting the error message from mysql. Maybe try something like the following...
$query = mysql_query ("SELECT classname_id FROM classnames WHERE classname_text ='dog' ")or die(mysql_error());
This would print the sql error on the page, and can help to debug the code.
I would also add...
echo "$class_name";
Just to be sure the variable is being set. I am sure this will sound patronizing buy is it POST and not GET. Make sure you are receiving in the correct method I have fallen into that trap a few times :)
Mack.
You could escape them using something like the following, Its probably not the most elegant way of doing it but it will let you know if it is the issue...
$class_name = $_POST['class_name'];
$class_name = str_replace("(", "\(", $class_name);
$class_name = str_replace(")", "\)", $class_name);
Mack.
$hostname = "localhost";
$database = "customers";
$username = "xx";
$password = "xx";
$con = mysql_connect($hostname, $username, $password);
mysql_select_db($database, $con);
$query = "SELECT classname_id FROM classnames WHERE classname_text ='".$class_name."'";
$result = mysql_query($query,$con);
if (!$result)
{
die('Error: ' . mysql_error());
}
$id = mysql_result($result,0);
$query = "SELECT classname_id FROM classnames WHERE classname_text LIKE '%".$class_name."%'";
It's not a very efficient select, but will let you know if there's something missing or extra somewhere.
You could also try this to get rid of any extra white space that might be in the select but not the DB:
$class_name=trim($class_name);
$query = "SELECT classname_id FROM classnames WHERE classname_text = '".$class_name."'";
Also, are you using any encoding on either the insertion into the DB or the passing of the variables to the PHP script EG htmlentities(), etc. which would make the echo display properly, but not match in the select... Delete the functions I have below as necessary, but this trims the white space, decodes everything, and strips the slashes in an effort to make sure you are working with raw data, then escapes the string as necessary:
$class_name=mysql_real_escape_string(stripslashes(html_entity_decode(trim($class_name))));
$query = "SELECT classname_id FROM classnames WHERE classname_text = '".$class_name."'";
It seems we usually like finding the solutions to the challenging issues that seem to accompany coding around here... It's not nearly as much fun if it's an easy 'by the book' solution that doesn't require a degree of creativity to incorporate. LOL