Why does a query with localStorage variable not work?

Asked

Viewed 105 times

0

I am making a system in PHP where the user should choose some parameters and then get results as restricted as possible.

For this I store the first variable using the LocalStorage in Javascript and everything works perfectly well, however, only when I literally write the variable by hand. But both query are identical.

What could be the problem?

SELECT * FROM tb_operadora WHERE FIND_IN_SET( '1', id_produto ) AND FIND_IN_SET('1', id_regioes )

The variable in question is the second, from id_regioes. When it is mounted automatically, no printa nothing, but if I write the variable in the hand, it works.

<?php require_once('Connections/local.php'); ?>
<?php
if (isset ($_GET['A'])) {
$A = $_GET['A'];
mysql_select_db($database_local, $local);
$query_porRegiao = "SELECT * FROM tb_operadora WHERE FIND_IN_SET( '" . $A . "', id_regioes )" ;
$porRegiao = mysql_query($query_porRegiao, $local) or die(mysql_error());
$row_porRegiao = mysql_fetch_assoc($porRegiao);
$totalRows_porRegiao = mysql_num_rows($porRegiao);

echo $query_porRegiao;
?>
<?php
do {  
?>
<option value="<?php echo $row_porRegiao['id_operadora']?>"><?php echo $row_porRegiao['nome_operadora']?></option>
<?php
} while ($row_porRegiao = mysql_fetch_assoc($porRegiao));
  $rows = mysql_num_rows($porRegiao);
  if($rows > 0) {
mysql_data_seek($porRegiao, 0);
$row_porRegiao = mysql_fetch_assoc($porRegiao);
mysql_free_result($porRegiao);
} 
}
//showProduto
if (isset ($_GET['B'])) {
$B = $_GET['B'];
?>
<script>var regioes = window.localStorage.getItem('regiao');</script>
<?php $A = "<script>document.write(regioes)</script>";
//echo $A;
mysql_select_db($database_local, $local);
$query_Produto = "SELECT * FROM tb_operadora WHERE FIND_IN_SET( '" . $B . "', id_produto ) AND FIND_IN_SET('" . $A . "', id_regioes )";
$Produto = mysql_query($query_Produto, $local) or die(mysql_error());
$row_Produto = mysql_fetch_assoc($Produto);
$totalRows_Produto = mysql_num_rows($Produto);

echo $query_Produto;
?>
<?php
do {  
?>
<option value="<?php echo $row_Produto['id_operadora']?>"><?php echo $row_Produto['nome_operadora']?></option>
<?php
} while ($row_Produto = mysql_fetch_assoc($Produto));
$rows = mysql_num_rows($Produto);
if($rows > 0) {
mysql_data_seek($Produto, 0);
$row_Produto = mysql_fetch_assoc($Produto);
mysql_free_result($Produto);
}  

}
?>
  • 2

    I don’t understand where the variable is. The example you gave is what was written in your hand? If it is, post an example where you try to concatenate the variable. I also did not understand well where JS enters and where PHP enters the question.

  • 2

    can you give an example of code that defines these variables? and what does the code look like when you "write the variable in your hand"? you won’t be mixing concepts server vs client side?

  • I don’t know if I can do this, but I’ll edit the question and add the code.

  • Thanks for editing Sergio, I don’t know how to leave so well formatted and I hope I’m not breaking rules of the site with this

  • @Marcellopatto you don’t have to thank, we’re here to help. I think you’re mixing concepts. When you do <?php $A = "<script>document.write(regioes)</script>"; this will not give any other value in the variable $A than this string is. You have to use ajax or url to pass this info to a PHP variable. What’s in $_GET['A'];? how is the application on the client side?

  • The value is passed yes Sergio, because I see it in Query when I command echo $query_Produto, So I guess if php can print the query exactly like the one I write, it should function, no?

  • 1

    @Sergio Sorry, the edition occurred almost simultaneously. My error.

  • As I am still programming, I see no problem in passing the url: [link]http://www.we-broker.com.br/simulador/beta/getOper.php?B=1[/link] login: guest password: ppp123

  • If you inspect the page I sent, you will see that in localStorage there is stored the value regia = 1

  • @Sergio, managed to access?

  • @Marcellopatto does not ask for any login, but I have other questions that you should add to the question. Do you know what AJAX? wouldn’t be a case of using it? pointing to this link ?

  • Yes, I know what AJAX is, but I’m still a beginner on this subject... as for not logging in, I don’t think I’ve closed the page yet. This page is already the result of a note made by AJAX of the requesting page. [link]http://www.we-broker.com.br/simulador/beta/simulador[/link] <- this will ask for login and password

Show 7 more comments
No answers

Browser other questions tagged

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