Query mysql does not work with variable - PHP

2

The query mysql works with normal variable, but not with the jquery post variable, for example:

DOES NOT WORK

$agenda=$_POST['agenda'];

$query = mysql_query("SELECT * FROM 'compromiso' WHERE login LIKE 'alanps' AND agenda LIKE '$agenda' AND ano LIKE '$ano' AND mes LIKE '$mes' AND dia LIKE '$list_day'") or die(mysql_error());

WORKS

$agenda="Agenda 1";

$query = mysql_query("SELECT * FROM 'compromiso' WHERE login LIKE 'alanps' AND agenda LIKE '$agenda' AND ano LIKE '$ano' AND mes LIKE '$mes' AND dia LIKE '$list_day'") or die(mysql_error());

jquery's post:

    $.post('calendario.php', {
            mes: mes,
            ano: ano,
            agenda: $("#agendanome").html()
        }, function(resposta) {
                $("#calendario").html(resposta);
        }, 'html');

If I give a print $agenda; it appears "Agenda 1" both ways! I also tried $agenda=html_entity_decode($agenda); and it did not work!

    
asked by anonymous 14.08.2014 / 03:44

2 answers

2

Grouping the answers that are in the comment section, @Jader and @ charlie-pope credits:

$("#agendanome").html() may be sending HTML tags or blank spaces / lines together. To solve use trim () and a strip_tags ():

$agenda = strip_tags(trim($_POST['agenda']));

Sending through Jquery gets better if only the content of the field, rather than the HTML together, uses $("#agendanome").text() .

$.post('calendario.php', {
            mes: mes,
            ano: ano,
            agenda: $("#agendanome").text() // esta linha foi alterada em relação ao código da pergunta
        }, function(resposta) {
                $("#calendario").html(resposta);
        }, 'html');
    
14.08.2014 / 16:11
0

PHP

 $agenda=urldecode($_POST['agenda']);
    $query = mysql_query("SELECT * FROM 'compromiso' WHERE login LIKE 'alanps' AND agenda LIKE '$agenda' AND ano LIKE '$ano' AND mes LIKE '$mes' AND dia LIKE '$list_day'") or die(mysql_error());

urldecode Is the specific function to remove html spaces from url.

Jquery of the post:

$.post('calendario.php', {
        mes: mes,
        ano: ano,
        agenda: $("#agendanome").html()
    }, function(resposta) {
            $("#calendario").html(resposta);
    }, 'html');

strip_tags It is a function to remove HTML from strings, it can also be used.

[espaço] in URL , it inserts %20 instead of spaces.

    
25.06.2015 / 13:34