PHP PDO发布字符串列表IN Where子句

时间:2016-07-11 01:00:01

标签: php mysql arrays post pdo

我几乎有以下代码可以使用,但不熟悉如何将我的列表从enduser发布到数组表单中的$ ids,就像我的测试一样。最终用户将列表发送给我“3,5,7,8 ......”

转换为POST语句时,如何模拟“$ ids = array(1,2,3)”?

<?php
//1. Create a database connection

require_once('config.php');
    $mysql_host = DB_HOST;
    $mysql_database = DB_NAME;
    $mysql_username = DB_USER;
    $mysql_password = DB_PASS;

$ids = $_POST["idsvar"]; //Doesn't return values
//$ids =  array(1,2,3); //This does work when used for testing purposes

$inQuery = implode(',', array_fill(0, count($ids), '?'));


try {
    $conn = new PDO("mysql:host=$mysql_host; dbname=$mysql_database", $mysql_username, $mysql_password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

    //2. Perform database query if Connected Successfully
    $stm = $conn ->prepare(
    'SELECT `schema`.`table`.`column1` AS `DiffNameA`,
    `schema`.`table`.`column2` AS `DiffNameB`
     FROM `schema`.`table`
     WHERE id IN(' . $inQuery . ')');

foreach ($ids as $k => $id)
    $stm->bindValue(($k+1), $id);
    $stm->execute();

$field = $stm->fetchAll();

foreach ($field as $row) {
print $row["DiffNameA"] . "|" .$row["DiffNameB"] ."\n\r"; //extra comma so can have notes hidden area
}   

  $conn = null;        // Disconnect    
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage().'<br />';
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
    }
?>

感谢您的帮助。

=============================================== ================

如果它对任何人有帮助,这就是完整的脚本:

<?php
//1. Create a database connection

require_once('config.php');
    $mysql_host = DB_HOST;
    $mysql_database = DB_NAME;
    $mysql_username = DB_USER;
    $mysql_password = DB_PASS;



$ids = explode(',', $_POST["idsvar"]);
$inQuery = implode(',', array_fill(0, count($ids), '?'));


try {


    $conn = new PDO("mysql:host=$mysql_host; dbname=$mysql_database", $mysql_username, $mysql_password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

    //2. Perform database query if Connected Successfully
    $stm = $conn ->prepare(
    'SELECT `schema`.`table`.`column1` AS `DiffNameA`,
    `schema`.`table`.`column2` AS `DiffNameB`
     FROM `schema`.`table`
     WHERE id IN(' . $inQuery . ')');

foreach ($ids as $k => $id)
    $stm->bindValue(($k+1), $id);
    $stm->execute();

$field = $stm->fetchAll();

foreach ($field as $row) {
print $row["DiffNameA"] . "|" .$row["DiffNameB"] ."\n\r"; //extra comma so can have notes hidden area
}


  $conn = null;        // Disconnect    
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage().'<br />';
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
    }
?>

1 个答案:

答案 0 :(得分:1)

您的$_POST["idsvar"]是一个字符串,而不是一个数组。使用explode将其拆分为每个逗号的数组。

$test = explode(',', '1,2,3');

演示:https://eval.in/603133

在你的情况下:

$ids = explode(',', $_POST["idsvar"]);

count($ids)适用于您的静态示例,因为您将$ids定义为数组($ids = array(1,2,3))。

或者如果您想验证输入,也可以使用正则表达式。

(\d+)(?:,|\z)

https://regex101.com/r/dI5fN4/1