Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将javascript变量传入PHP并进行mysql查询_Php_Javascript_Mysql - Fatal编程技术网

将javascript变量传入PHP并进行mysql查询

将javascript变量传入PHP并进行mysql查询,php,javascript,mysql,Php,Javascript,Mysql,以下脚本取自保存为example3.php的大型脚本: <script type="text/javascript"> var layer; //where layer is a table like ------------------------------------ BUSNAME + Category + ------------------------------------ 200 Bay + Restaurant + 2

以下脚本取自保存为example3.php的大型脚本:

<script type="text/javascript">
var layer;

//where layer is a table like 
------------------------------------
BUSNAME       +    Category    +
------------------------------------
200 Bay       +   Restaurant   +
201 Bay       +   Bank         +
202 Bay       +   School       +
203 Bay       +   Restaurant   +
204 Bay       +   School       +
205 Bay       +   Restaurant   +
206 Bay       +   Restaurant   +
207 Bay       +   School       +
208 Bay       +   Restaurant   +
209 Bay       +   Restaurant   +
------------------------------------

window.location.href = "example3.php?layer="+ layer;

<?php
  //Make a MySQL Connection
  $query = "SELECT Category, COUNT(BUSNAME) 
    FROM ".$_GET['layer']." GROUP BY Category"; 
  $result = mysql_query($query) or die(mysql_error());
  //Print out result
  while($row = mysql_fetch_array($result)){
    echo "There are ".$row['COUNT(BUSNAME)']. " " .$row['Category']. "items.";
  echo "<br/>";
  }
?>

</script>

不知道为什么它不起作用。任何建议都必须得到赞赏

正如Balus指出的,要注意SQL注入。当您访问php页面时,$\u GET['layer']是否等于什么

javascript变量是否等于某个值? 变量层=层

层是数据库中表的实际名称。

编辑

在此上下文中,mysql\u real\u escape\u字符串不起作用

$layer = mysql_real_escape_string($_GET['layer']);
$query = "SELECT Category, COUNT(BUSNAME) 
FROM `".$layer."` GROUP BY Category"; <<-- this doesn't work.
链接: SQL注入攻击: PDO的:


一旦我找到解决特定SQL注入问题的方法,将立即更新。

如果您有动态表名,则说明您的数据库设计错误

至于你的剧本,它就是没有意义


分成两部分:JS页面和PHP页面。并使用window.location.href调用此PHP页面,我建议使用JSON对客户端的表进行JS编码。使用POST发送,然后在服务器端用PHP解码


大多数流行的JS库都有JSON编码函数,PHP函数被调用。

给层变量赋值。@teresko和David,来吧,不要只是指出它,告诉这个人如何修复它!你自己使用PDO吗?是的,但我不是php人。我是一名delphi程序员,delphi是强类型的,所以插入整数有时会像query1.SQL.text:=“从b中选择a,其中a=”+IntToStri;。如果我放入字符串,我总是查询1.ParamByName'x'。AsString:=MyString;评论是:-无用-粗鲁-不正确-不完整。@Col除此之外,我回答MySQL部分,用少量php将东西粘在一起。但是如果这篇文章有事实上的错误,请随意删掉。是的。把答案标记为质量极差。我应该去做而不是评论。道歉。
$value = mysql_real_escape_string($_GET['fieldname']);
$query = "SELECT * FROM test WHERE f1 = '".$value."'"; <<-- this works
                                        ^          ^