Javascript 基于php变量的mysql查询

Javascript 基于php变量的mysql查询,javascript,php,mysql,Javascript,Php,Mysql,这是我的密码 $query = "select ((recipients.maennlichDeutsch+recipients.maennlichAuslaender+recipients.weiblichDeutsch+recipients.weiblichAuslaender)/inhab.Einwohner) as Sozialhilfeempfaenger,jahr from recipients left join education on recipients.Bundesla

这是我的密码

  $query = "select ((recipients.maennlichDeutsch+recipients.maennlichAuslaender+recipients.weiblichDeutsch+recipients.weiblichAuslaender)/inhab.Einwohner) as Sozialhilfeempfaenger,jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr where education.Abschluss in ('Hauptschulabschluss') and recipients.Bundesland = '.$_REQUEST['land'].'";
$result=mysqli_query($db, $query) or die('Error querying database.');

$q = "select ((education.weiblich+education.maennlich)/inhab.Einwohner) as 'niedriger Bildungsstand',Jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr WHERE education.Abschluss in ('Ohne Haupschulabschluss','Hauptschulabschluss') and recipients.Bundesland = '.$_REQUEST['land'].'";
$r=mysqli_query($db, $q) or die('Error querying database.');
使用$u请求['land']。我正在尝试获取下拉菜单的选定值。可变区域正在运行,我可以毫无问题地响应它。查询在没有$u请求['land']的情况下也可以正常工作


但现在我得到了500个错误。你知道我在这里做错了什么吗?

当你用双引号开始字符串时,你应该将变量转义为双引号连接,更改如下:

$q = "... and recipients.Bundesland = '.$_REQUEST['land'].'";
为此:

$q = "... and recipients.Bundesland = '".$_REQUEST['land']."'";

第一个查询也是如此。希望有帮助。

我想问题可能出在您的查询行上,您用双引号打开,然后用单引号结束

 $query = "select.... '.$_REQUEST['land'].'";
试着简单地使用这个:

 $query = "select.... ".$_REQUEST['land'];
试试看:

$query = "select ((recipients.maennlichDeutsch+recipients.maennlichAuslaender+recipients.weiblichDeutsch+recipients.weiblichAuslaender)/inhab.Einwohner) as Sozialhilfeempfaenger,jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr where education.Abschluss in ('Hauptschulabschluss') and recipients.Bundesland = '".$_REQUEST['land']."'";
$result=mysqli_query($db, $query) or die('Error querying database.');

$q = "select ((education.weiblich+education.maennlich)/inhab.Einwohner) as 'niedriger Bildungsstand',Jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr WHERE education.Abschluss in ('Ohne Haupschulabschluss','Hauptschulabschluss') and recipients.Bundesland = '".$_REQUEST['land']."'";
$r=mysqli_query($db, $q) or die('Error querying database.');

我认为它是有效的,我只是替换了“对于查询中的连接问题,始终倾向于使用花括号来避免这些问题:

花括号用于标记类、OOP术语中的函数方法、循环和控制结构体

它们还可以在字符串中用于将变量与周围的文本分开

一,。 $verb='add'; 2. 这个动词的现在时是$动词; 假设你想显示动词的过去式,而不只是通过添加“ed”来重新定义它

一,。 这个动词的echo过去时是$verbed; 若您尝试上述方法,那个么PHP将搜索变量$verbed并抛出一个错误,因为它并没有定义。要将动词与后缀“ed”分开,您可以使用下面的花括号

一,。 这个动词的呼应过去时是{$动词}; 如果$verb是一个数组,那么可以使用它的一个元素,如下所示

一,。 这个动词的呼应过去时是{$verb['pass_tense']}; 若$verb是一个对象,并且有一个名为getPastTense的方法返回动词的过去式,那个么可以像下面这样使用它

一,。 这个动词的呼应过去时是{$verb->getPastTense}

 $query = "select ((recipients.maennlichDeutsch+recipients.maennlichAuslaender+recipients.weiblichDeutsch+recipients.weiblichAuslaender)/inhab.Einwohner) as Sozialhilfeempfaenger,jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr where education.Abschluss in ('Hauptschulabschluss') and recipients.Bundesland = '{$_REQUEST['land']}'";
$result=mysqli_query($db, $query) or die('Error querying database.');

$q = "select ((education.weiblich+education.maennlich)/inhab.Einwohner) as 'niedriger Bildungsstand',Jahr from recipients left join education on recipients.Bundesland = education.FK_Land and recipients.Jahr = education.FK_Jahr left join inhab on recipients.Bundesland = inhab.FK_land and recipients.Jahr = inhab.FK_Jahr WHERE education.Abschluss in ('Ohne Haupschulabschluss','Hauptschulabschluss') and recipients.Bundesland = '{$_REQUEST['land']}'";
$r=mysqli_query($db, $q) or die('Error querying database.');

您可以双引号引字符串,并在双引号内使用{$var}。使它更具可读性

$query=。。。Bundesland='{$_REQUEST['land']}'

此外,我建议您尝试PDO或NotORM。至少让你的GET/POST/REQUEST通过一些可以净化它的东西