php—如何在函数中传递空变量并更改其值

bxgwgixi  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(425)

我想传递一个包含空值的字符串,然后在函数中填充它们(我知道sql注入)。另一方面,我想有一个pdo语句的好例子

  1. function foo(&$var) {
  2. $variable1="test";
  3. $variable2="test";
  4. echo $var;
  5. }
  6. $string="UPDATE table SET column1=$variable1 WHERE column2=$variable2";
  7. foo($string);

我还知道另一种方法来实现这一点。但这不是个好办法

  1. function test($Query) {
  2. $variable1="test";
  3. $variable2="test";
  4. $finalQuery = vsprintf($Query, array($variable1, $variable2));
  5. print_r($finalQuery);
  6. }
  7. $Query = "UPDATE table SET column1='%s' WHERE column2='%s'";
  8. test($Query);
eh57zj3b

eh57zj3b1#

我相信你正在努力实现这一目标。

  1. <?php
  2. /* On update l'historique côté vets; ici on controle et on dit KESSKONFAI*/
  3. include('../Models/db_connect.php');
  4. $a = explode('-',$_GET['a']);
  5. $o = $_GET['o'];
  6. switch($a):
  7. case($a[1] === 'breed'):
  8. if($a[0] === 'desc'){
  9. $query =
  10. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  11. FROM patients
  12. WHERE
  13. owner_ID = :ID
  14. ORDER BY breed DESC";
  15. } else if ($a[0] === 'asc'){
  16. $query =
  17. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  18. FROM patients
  19. WHERE
  20. owner_ID = :ID
  21. ORDER BY breed ASC";
  22. }
  23. break;
  24. case($a[1] === 'name'):
  25. if($a[0] === 'desc'){
  26. $query =
  27. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  28. FROM patients
  29. WHERE
  30. owner_ID = :ID
  31. ORDER BY pet_name DESC";
  32. } else if ($a[0] === 'asc'){
  33. $query =
  34. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  35. FROM patients
  36. WHERE
  37. owner_ID = :ID
  38. ORDER BY pet_name ASC";
  39. }
  40. break;
  41. case($a[1] === 'color'):
  42. if($a[0] === 'desc'){
  43. $query =
  44. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  45. FROM patients
  46. WHERE
  47. owner_ID = :ID
  48. ORDER BY colour DESC";
  49. } else if ($a[0] === 'asc'){
  50. $query =
  51. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  52. FROM patients
  53. WHERE
  54. owner_ID = :ID
  55. ORDER BY colour ASC";
  56. }
  57. break;
  58. case($a[1] === 'sex'):
  59. if($a[0] === 'desc'){
  60. $query =
  61. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  62. FROM patients
  63. WHERE
  64. owner_ID = :ID
  65. ORDER BY sex DESC";
  66. } else if ($a[0] === 'asc'){
  67. $query =
  68. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  69. FROM patients
  70. WHERE
  71. owner_ID = :ID
  72. ORDER BY sex ASC";
  73. }
  74. break;
  75. case($a[1] === 'date'):
  76. if($a[0] === 'desc'){
  77. $query =
  78. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  79. FROM patients
  80. WHERE
  81. owner_ID = :ID
  82. ORDER BY date_of_birth DESC";
  83. } else if ($a[0] === 'asc'){
  84. $query =
  85. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  86. FROM patients
  87. WHERE
  88. owner_ID = :ID
  89. ORDER BY date_of_birth ASC";
  90. }
  91. break;
  92. case($a[1] === 'chip'):
  93. if($a[0] === 'desc'){
  94. $query =
  95. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  96. FROM patients
  97. WHERE
  98. owner_ID = :ID
  99. ORDER BY microchip_tatoo DESC";
  100. } else if ($a[0] === 'asc'){
  101. $query =
  102. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  103. FROM patients
  104. WHERE
  105. owner_ID = :ID
  106. ORDER BY microchip_tatoo ASC";
  107. }
  108. break;
  109. case($a[1] === 'hist'):
  110. if($a[0] === 'desc'){
  111. $query =
  112. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  113. FROM patients
  114. WHERE
  115. owner_ID = :ID
  116. ORDER BY history DESC";
  117. } else if ($a[0] === 'asc'){
  118. $query =
  119. "SELECT pet_name, ID, breed, colour, sex, date_of_birth,microchip_tatoo, history
  120. FROM patients
  121. WHERE
  122. owner_ID = :ID
  123. ORDER BY history ASC";
  124. }
  125. break;
  126. default:
  127. endswitch;
  128. if(isset($query)){
  129. include('../Models/order_by_clients.php');
  130. $patients_rows = order_by($query,$o,$db);
  131. }
  132. ?>

功能:

  1. <?php
  2. function order_by($query,$o,&$db){
  3. $query_params = array(':ID' => $o);
  4. try {
  5. $stmt = $db->prepare($query);
  6. $result = $stmt->execute($query_params);
  7. $patients_rows = $stmt -> fetchAll();
  8. for($i = 0;$i < count($patients_rows);$i++){
  9. $patients_rows[$i]['history'] = "\n".strtr($patients_rows[$i]['history'],array("."=>".\r\r","\S:"=>" :\r","-"=>" - "));
  10. }
  11. include '../Views/order_by_clients.php';
  12. }catch(PDOException $ex){
  13. die("Failed to run query: " . $ex->getMessage());
  14. }
  15. }
  16. ?>

但是当开关在功能中

展开查看全部
zte4gxcn

zte4gxcn2#

您可以使用mysqli实现以下目的:

  1. class Database {
  2. protected $con;
  3. public __construct(){
  4. $this->con=mysqli_connect("my_host","my_user","my_password","my_db");
  5. if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
  6. }
  7. public __destruct(){
  8. mysqli_close($this->con);
  9. }
  10. public query($sql){
  11. if (!mysqli_query($this->con,$sql)) { die('Error: ' . mysqli_error($this->con)); };
  12. }
  13. }
  14. class Table extends Database {
  15. public function update($var, $var2){
  16. $var = mysqli_real_escape_string($con,$var);
  17. $var2 = mysqli_real_escape_string($con,$var2);
  18. $sql = "UPDATE table SET column1=$var1 WHERE column2=$var2";
  19. $this->query($sql);
  20. }
  21. }

这样,就可以使用php了 mysqli_real_escape_string ,这将帮助您防止sql注入。
此外,您还可以选择“准备语句”选项。只需更改、更新方法:

  1. public function update($var, $var2){
  2. $smtp = mysqli_prepare($this->con,"UPDATE table SET column1=? WHERE column2=?");
  3. mysqli_stmt_bind_param($smtp,'ss', $var,$var2);
  4. mysqli_stmt_execute($stmt);
  5. }
展开查看全部
vsdwdz23

vsdwdz233#

假设 $variable1 以及 $variable2 不是像示例中所示的那样逐字定义的(我假设这是从您的注解“我需要用我将在函数中创建的值正确地填充$query”)我会说您的函数需要使用一个准备好的语句而不是字符串。

  1. function foo(PDOStatement $statement) {
  2. // stuff happens that creates $variable1 and $variable2
  3. $statement->bindValue(1, $variable1);
  4. $statement->bindValue(2, $variable2);
  5. return $statement;
  6. }

而不是定义 $string ,创建一个准备好的语句并将其传递给函数。

  1. $statement = $pdo->prepare('UPDATE table SET column1=? WHERE column2=?');
  2. foo($statement);

至于您在问题的第一个代码块中尝试的方式,有几个问题。
传递包含预定义变量的字符串,然后在函数中填充这些变量的唯一方法是先用单引号定义字符串。否则,使用双引号,php将查找 $variable1 以及 $variable2 在全局范围中,找不到它们,它们的未定义(null)值将替换为中的空字符串 $string 在传递到函数之前。

  1. $string='UPDATE table SET column1=$variable1 WHERE column2=$variable2';

然后,在函数中,我知道的唯一方法就是将字符串传递给 eval .

  1. function foo(&$var) {
  2. $variable1="test";
  3. $variable2="test";
  4. eval('$var = "' . $var . '";');
  5. echo $var;
  6. }

现在,对于这个例子,这是一个很好的主意。
当您这样编写字符串时,您依赖于函数中定义的某些变量,并且函数依赖于具有这些变量的输入。你永远无法改变这个功能。
这取决于 eval . 使用eval是危险的;它允许任何字符串作为php代码在系统上执行,您可能无法安全地限制该字符串的源代码。
如果这将被用来执行sql,那么这不是一个好的方法,不管它是否是一个构建字符串的好方法,我已经说过它不是。您应该将这些值绑定到一个准备好的语句。

展开查看全部

相关问题