Query mysql does not work with variable - PHP

Asked

Viewed 497 times

2

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

DOESN’T 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 of the post:

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

if I give a print $agenda; appears "Agenda 1" both ways!
tried as well $agenda=html_entity_decode($agenda); and it didn’t work!

  • Does not work means any error or no result is returned?

  • Probably the mistake is in: $("#agendanome").html(), post HTML to see what you’re sending in var agenda

  • if I print $agenda; "Agenda 1" appears in both ways!

  • tried also $agenda=html_entity_decode($schedule); and had no result!

  • 1

    That one $("#agendanome").html() not sending together HTML tags or blank spaces/lines? try passing a trim() and a strip_tags()...

  • @Jader, it worked with Trim(), Valew!!!

  • 2

    use: $("#agendanome").text(), so do not pass HTML together

  • @Jader Since you helped him, post your answer. So this question comes out of the unanswered session.

Show 3 more comments

2 answers

2


Grouping the answers that are in the comments section, credits @Jader and @papa-charlie:

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

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

Jquery uploading is best done only with field content, instead of HTML together, using $("#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');

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 It 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 function to remove HTML from strings, can also be used.

As there is no [espaço] in URL, he inserts %20 in place of spaces.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.