tags:

views:

45

answers:

2

I have a list of URLs in my homepage which is like SO in the following form

<a href="?questions&questions=777">link1</a>
<a href="?questions&questions=666">link2</a>

The following PHP script has a problem in the parameter of $_GET.

 $dbconn = pg_connect("host=localhost port=5432 dbname=masi user=masi password=123");
 // A QUESTION SELECTED BY USER 
 // to generate the answers for the given question 
     $result = pg_prepare($dbconn, "query9", "SELECT title, answer
         FROM answers 
         WHERE questions_question_id = $1;");            // WARNINGS refer HERE
     // TODO I am not sure if the syntax is correct for parametr inside $_GET                             
     $result = pg_execute($dbconn, "query9", array($_GET["'question_id' = $questions_question_id"]));
           // Problem HERE inside the $_GET

Thank you to Nos and Jikhan for solving the problem with $dbconn and to Simon for solving the main problem of this thread!

How can you get only the question_id from the URL such that Postgres understand the query?

+4  A: 

It seems that the problem is not the query but the connection to the database (dbconn)

Jikhan
This problem is now solved.
Masi
+1  A: 

Your $_GET statement also looks rather strange. I don't know what the point of repeating 'questions' in your link is but if we assume your links are formatted as so:

<a href="?questions=777">link1</a>

You can get access to the ID 777 as so:

$question_id = $_GET['questions'];

I believe pg_execute() just expects the array of values in the order you write them in the prepare statement. So you don't need to try to assign the variable $questions_question_id - this simply won't work.

I'd also ensure this contains what you expect it to (i.e. just an ID number).

$question_id = filter_input(INPUT_GET, 'questions', FILTER_SANITIZE_NUMBER_INT);

The filter family of functions are available in PHP 5.2 and get rid of any unwanted characters. See http://uk2.php.net/manual/en/function.filter-input.php

filter_input() returns null if the GET variable isn't set.

simonrjones
**Do you need the explicit sanitizing, since we use `pg_prepare`?** I feel that `pg_prepare` sanitizes the input automatically.
Masi
sanitizing will only remove dangerous characters such as semi-colons. It won't guarantee the ID variable is actually a number. For security you still need to ensure the ID is just an integer. It's always worth filtering incoming variables and preparing all SQL!
simonrjones