简体   繁体   English

在php中使用get变量获取链接的值

[英]Getting the values of the link using get variable in php

I have a link in a php file which takes mke to another page.我在一个 php 文件中有一个链接,它将 mke 带到另一个页面。 Here is the link:链接在这里:

 <?php  echo ' <a href="product_detail.php?marca='.$row["maname"].'">'?>

So I know that I can take the marca from this link using $_GET["marca"] in php.所以我知道我可以在 php 中使用 $_GET["marca"] 从这个链接中获取 marca。 Now I want to output some categories but only them which marca is the same as $_GET["marca"].现在我想输出一些类别,但只输出与 $_GET["marca"] 相同的类别。 I create this sql query but it outputs an error in the last row:我创建了这个 sql 查询,但它在最后一行输出错误:

 $sql0="SELECT marche.marca as maname FROM marche WHERE maname=$_GET["marca"]; ";
Have I done something wrong? 我做错了什么吗? Can someone tell me which is the right syntax to include get variable inside sql queries?Thanks! 有人能告诉我在 sql 查询中包含 get 变量的正确语法是什么吗?谢谢!

Your query seems to be okay except at one place.除了在一处之外,您的查询似乎没问题。

$sql0="SELECT marche.marca as maname FROM marche WHERE maname=".$_GET["marca"]; 

But you should not depend on input ,you need to first sanitize it.但是您不应该依赖输入,您需要先对其进行消毒。 eg,例如,

$input = mysql_real_escape_string(trim($_GET["marca"]));

$sql0="SELECT marche.marca as maname FROM marche WHERE maname='$input'";

Since you try de put php in your request, you need to close the SQL request, do your action in php, then open again your SQL request, like that :由于您尝试将 php 放入您的请求中,因此您需要关闭 SQL 请求,在 php 中执行您的操作,然后再次打开您的 SQL 请求,如下所示:

$sql0="SELECT marche.marca as maname 
FROM marche 
WHERE maname=" . $_GET["marca"] .";";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM