English 中文(简体)
Getting list of items in mysqli query
原标题:
  • 时间:2009-11-09 20:30:42
  •  标签:
  • php
  • mysqli

I currently have a list of things to update in the given mysqli query:

$insert = mysqli_query($db,"UPDATE `tbl_perms` SET
 `1` = " . mysqli_real_escape_string($db,$_POST[ permsA_1 ]) . " ,    
 `2` = " . mysqli_real_escape_string($db,$_POST[ permsA_2 ]) . " ,    
 `3` = " . mysqli_real_escape_string($db,$_POST[ permsA_3 ]) . " ,    
 `4` = " . mysqli_real_escape_string($db,$_POST[ permsA_4 ]) . " ,    
 `5` = " . mysqli_real_escape_string($db,$_POST[ permsA_5 ]) . " , 
 `6` = " . mysqli_real_escape_string($db,$_POST[ permsA_6 ]) . " ,
 `7` = " . mysqli_real_escape_string($db,$_POST[ permsA_7 ]) . " ,
 `8` = " . mysqli_real_escape_string($db,$_POST[ permsA_8 ]) . " ,
 `9` = " . mysqli_real_escape_string($db,$_POST[ permsA_9 ]) . " ,
`10` = " . mysqli_real_escape_string($db,$_POST[ permsA_10 ]) . " 
 WHERE `userid` =  $id  ")or die(mysqli_error($db));

How would I rewrite this so that I don t have to manually code the sql in so that when "permissions" are added, this automatically generates the sql needed to do the sql query?

Thanks.

最佳回答

Here you go:

$updaters = array();

for ($i = 1; $i <= 10; $i++)
{
    if (isset($_POST[ permsA_ .$i]))
        $updaters[] =  ` .$i. ` =   .mysqli_real_escape_string($db, $_POST[ permsA_ .$i]).   ;
}

$insert = mysqli_query($db, UPDATE `tbl_perms` SET  .implode( , , $updaters).
     WHERE `userid` =  .$id)or die(mysqli_error($db));
问题回答
foreach ($_POST as $key=>$value) {
  if (preg_match( /^permsA/ ,$key)) {
    list($tmp,$num)=explode( _ ,$key);
    $perms[]="`$num` = " . (int)$value; //or some other method of sanitizing the $value
  }
}

$sql="UPDATE tbl_perms SET " . implode( , $perms) . "WHERE userid =  $id " ;

What the others said, except if possible I d do it a bit differently - rather than having to use a $i to control the loop, I d rename the form so that the fields were called something like:

<input type="checkbox" value="1" name="permsA[1]">
<input type="checkbox" value="1" name="permsA[2]">

etc etc.

You d then get a post array you could reference like this like this:

$_POST[ permsA ][1];
$_POST[ permsA ][2];

Advantage of this is that you can do:

$bits = array();
foreach ($_POST[ permsA ] as $key=>$value) {
  $bits[] = $key . " =  " . mysqli_real_escape_string($db, $value) . " ";
}

$sql = "UPDATE permissions SET " . implode( ,  , $bits) . " WHERE userid =  $id  ") 
 or die(mysqli_error($db));

And the advantage of doing that is that you won t one day get a random bug when you add more permissions to the system and go past the max you are using for $1 :)

Appreciate you may not be able to change the form though, or may not ever add more permissions, in which case this solution is no better.

Just a draft:

$sql = "UPDATE `tbl_perms` SET ";
for ($i = 1; $i < 100; i++) {
  if (isset($_POST[ permsA_  . $i])) {
    if ($i > 1) $sql .= ",";
    $sql .= "`$i` = " . mysqli_real_escape_string($db,$_POST[ permsA_1 ]) . " ";
  }
}
$sql .= " WHERE `userid` =  $id ";

Please note that the 100 is just an arbitrary number





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签