Problems with the $ _GET

2

I have this code and a value in the url that I call with the function: $ _GET (called cID = (Number)) I'm stuck and I can not make the query I do with the json, the only way it works is adding the number manually, but I need you to take it from the browser.

in this query:

$query = mysqli_query($con, "SELECT * FROM calendar where customers_id = '11'"); 

the number 11 must be taken from the url with the function $ _GET I have placed the following code and the table disappears, it does not read the url and it returns a value of zero:

I've tried with:

$query = mysqli_query($con, "SELECT * FROM calendar where customers_id = '$_GET['cID']'");

and with:

$query = mysqli_query($con, "SELECT * FROM calendar where customers_id = '.$_GET['cID'].'");

and with:

$query = mysqli_query($con, "SELECT * FROM calendar where customers_id = '.$HTTP_GET_VARS['cID'].'");

and it does not work

this is the original URL the variable is & cID = (value) and the value of & cID = (value) is what I need to general the Query:

link

here I leave the complete code but it does not work, could you help me with this:

if($type == 'fetch')
{
  $action = (isset($_GET['action']) ? $_GET['action'] : '');
  $events = array();

  $query = mysqli_query($con, "SELECT * FROM calendar where customers_id = '$_GET['cID']'");

  while($fetch = mysqli_fetch_array($query,MYSQLI_ASSOC))
  {
    $e = array();
    $e['id'] = $fetch['id'];
    $e['title'] = $fetch['title'];
    $e['start'] = $fetch['startdate'];
    $e['end'] = $fetch['enddate'];
    $e['customers_id'] = $_GET['cID'];


    array_push($events, $e);
  }
  echo json_encode($events);
}
    
asked by Ivan Diaz Perez 15.06.2016 в 18:01
source

4 answers

1

First of all, if something does not work for you, check before making changes if you throw some kind of result the variables you expect to have, and if you throw it, what result is it.

var_dump($_GET['cID']);

I should give you a browser exit similar to this:

string '11' (length=1)

As you can see, despite being a number, it represents it as a string.

If you want to convert it to integer, just put in front of the variable (int) .

var_dump((int)$_GET['cID']);

In this case it would give an output in the browser like this:

int 1

If all goes well and we receive the data correctly it is time to pass the parameters to the query.

When you are using the double quotes " for the query, it is not necessary to run around or enclose the variable. You can directly pass a variable to it.

Since what you expect in a number as an identifier, the query will admit it as a string or a integer . But as a good practice, if you wait for a whole number, the ideal would be to pass a whole number to the query.

// he puesto 0 como alternativa pero puedes poner lo que necesites
$id = (isset($_GET['cID'])) ? (int)$_GET['cID'] : 0;
$query = mysqli_query($con, "SELECT * FROM calendar where customers_id = $id");

If testing with a static value gives you results, it is clear that it is not a query problem at first sight. With% previous var_dump() you can check if you get the parameters from the url, and what parameters are what you actually receive.

I add

On the code that you have put later, check this line:

$strSQL = mysqli_query($con, "SELECT * FROM calendar where customers_id =" . $_GET['cID'];

You need to close the function mysqli_query .

$strSQL = mysqli_query($con, "SELECT * FROM calendar where customers_id =" . $_GET['cID']);

EDITED 2nd

You can try to see all the added parameters that the url brings:

var_dump($_GET);

With the comment url, I should give you an output like this:

array (size=3)
  'page' => string '1' (length=1)
  'cID' => string '11' (length=2)
  'action' => string 'edit' (length=4)
    
answered by 15.06.2016 / 18:49
source
2

try this

$strSQL="SELECT * FROM calendar where customers_id =".$_GET['cID'];
 $query = mysqli_query($con,$strSQL);
    
answered by 15.06.2016 в 18:17
1

Following what is stated in this question from the OS in English:

$_GET is not a function but an array so to access:

<?php
echo $_GET['cID']; //Sustituye por el QueryString deseado.
    
answered by 15.06.2016 в 18:11
1

You are not escaping the characters in your query.

$query = mysqli_query($con, "SELECT * FROM calendar where customers_id = '".$_GET['cID']."'");
    
answered by 15.06.2016 в 18:14