0

Is there any problem with this since i get no output returned?Thanks in advance.

$question_text = $_POST['question_text'];

$first_word = explode(" ", $question_text);

$query ="SELECT c.field_name,t.category_name, d.domain_name FROM category_fields c, taxonomy_category t,  taxonomy_domain d
 WHERE c.category_Id = t.category_Id AND t.domain_Id = d.domain_Id
 AND c.field_name = '$first_word'";

I've changed my code to this and still no output.Is there a problem with the way i display them ?Thanks

$question_text = $_POST['question_text'];

list($first_word) = explode(' ', $question_text);

$query ="SELECT c.field_name,t.category_name, d.domain_name FROM category_fields c, taxonomy_category t, taxonomy_domain d WHERE c.category_Id = t.category_Id AND t.domain_Id = d.domain_Id AND c.field_name = '".mysql_escape_string($first_word[0])."'";

$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

echo "Keyword :{$row['c.field_name']}" . "Category : {$row['t.category_name']}" . "Domain : {$row['d.domain_name']}"; }

?>

2
  • 1
    This code is vulnerable to an SQL injection attack. Please don't interpolate POST data directly into the query--do some escaping. You might try checking what the value of $first_word is and running the query manually against your db. Commented Mar 14, 2011 at 4:50
  • print_r is your friend. print_r($first_word); will indicate why its not working. Also, list($first_word) = explode(' ', $question_text); extracts the first element of the generated array into $first_word. Commented Mar 14, 2011 at 4:52

4 Answers 4

1

$first_word is an array, not a string, in your query you want $first_word[0]

it is also very unsafe to put any user submitted value directly in to a sql query, it should always be sanitised.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks,so i just have to add the array to both the $first_word ? Query too ? Thanks :)
1

Instead of the explode line you could use following to get a correct SQL query:

$first_word = mysql_real_escape_string(strtok($question_text, " "));

The strtok cuts of the string until the first space. And escape function is necessary to prevent your script from SQL exploits.

Comments

0

Well after $first_word = explode(" ", $question_text); $first_word is an array because explode returns and array

Therefore

$query ="SELECT c.field_name,t.category_name, d.domain_name FROM category_fields c, taxonomy_category t,  taxonomy_domain d
 WHERE c.category_Id = t.category_Id AND t.domain_Id = d.domain_Id
 AND c.field_name = '$first_word'";

should be

$query ="SELECT c.field_name,t.category_name, d.domain_name FROM category_fields c, taxonomy_category t,  taxonomy_domain d
 WHERE c.category_Id = t.category_Id AND t.domain_Id = d.domain_Id
 AND c.field_name = '".mysql_escape_string($first_word[0])."'";

Read http://php.net/manual/en/function.mysql-escape-string.php for what mysql_escape_string does.

1 Comment

Hi,thanks and i have fixed the code but still no output.Is there any problem ?The new code is added above.
0
list($first_word) = explode(' ', $question_text);

This should do the trick. Sanitize your database inputs!

3 Comments

Hi,thanks and i have fixed the code but still no output.Is there any problem ?The new code is added above. –
Hi Abby; When you do list($first_word) = explode(' ', $question_text);, you no longer access $first_word as an array. The list() function extracts from the array result of explode(), the first element, and stores it in the supplied argument, in this case $first_word. You can simply use $first_word in your query. Sanitize your database inputs!
Ok i will look up on the ways to sanitize the inputs since i'm a beginner in PHP :) But a quick question, do i have to have a table to store the question_text in the method POST that i used? Currently i have a database named keyword with 3tables (category_fields,taxonomy_category,taxonomy_domain).I have make it list($first_word) and query just the $first_word bu still im getting Keyword : Category : Domain : It did not display the 1 i've stated in query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.