简体   繁体   English

使用带有SET的PHP UPDATE作为设置为另一个变量的变量?

[英]Use PHP UPDATE with SET as a variable set to another variable?

Im wondering if something like this is possible? 我想知道这样的事情是否可能?

$joinguild = "UPDATE guild SET '.$rank.'='.$receiver.' WHERE name ='"$dupecheckinfo["guild"]"'";

Im trying to SET '.$rank.'='.$receiver.', but I dont know if I can use a variable where $rank is. 我试图设置'。$ rank。'='。$ receiver。',但是我不知道我是否可以在$ rank所在的位置使用变量。 Is there a proper way to write this. 是否有编写此内容的正确方法。 Is it even possible? 可能吗 If not how would you approach it? 如果没有,您将如何处理? Thanks! 谢谢!

Here is my SQL table im working with 这是我正在使用的SQL表

Edit: See how my table has Rank1 Rank2 Rank3 etc. Well I am passing the rank value that I want to set so for example 编辑:看我的表如何具有Rank1 Rank2 Rank3等。那么我正在传递要设置的等级值,例如

$rank = $_POST["rank"];

$joinguild = "UPDATE guild SET '.$rank.'='.$username.' WHERE name ='"$dupecheckinfo["guild"]"'";

Your question in not clear but you have some problems in your PHP statement. 您的问题尚不清楚,但是您的PHP语句中有一些问题。 I think you are trying to create your SQL UPDATE query using PHP variables. 我认为您正在尝试使用PHP变量创建SQL UPDATE查询。

Try this: 尝试这个:

$joinguild = "UPDATE guild SET $rank='$receiver' WHERE name='" . $dupecheckinfo["guild"] . "'";

Here $rank should have valid column name in your table. 在这里$rank在表中应具有有效的列名。 Also read about SQL injection. 另请阅读有关SQL注入的信息。

Your question is quite unclear but to update records from a table you can use this line of code: 您的问题尚不清楚,但是要更新表中的记录,您可以使用以下代码行:

  $sql=mysqli_query($conn, "UPDATE `table` SET option1='$op1', option2='$op2', option3='$op3', option4='$op4' where id='$id'");      

If this is unclear please let me know. 如果不清楚,请告诉我。

Yes, you can use variables for table and field names in your queries. 是的,您可以在查询中为表名和字段名使用变量。 However, you should avoid it whenever possible, because it generally leads to SQL injection vulnerabilities. 但是,应尽可能避免使用它,因为它通常会导致SQL注入漏洞。 Instead of building queries with string concatenation, use prepared statements with bound parameters . 与其使用字符串连接构建查询,不如使用带有绑定参数的 预处理语句 See this page and this post for some good examples. 请参见此页面此帖子,以获取一些很好的示例。

Unfortunately, the bind mechanism works only for values and not for table names or field names, so it's best to try avoiding variable table/field names. 不幸的是,绑定机制仅适用于值,不适用于表名或字段名,因此最好避免使用可变的表/字段名。 If you find that you absolutely must, the best approach would be to ensure that the contents of the variable matches with a pre-set whitelist of allowed table/field names. 如果发现绝对必要,最好的方法是确保变量的内容与允许的表/字段名称的预设白名单匹配。

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

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