无法将输入数据和数据库查询结果更改为数组以进行模式识别

时间:2018-07-18 22:34:24

标签: php mysql regex

我无法将输入字符串($ prima = $ _POST ['id'];)和数据库查询结果($ systemuser ['id'];)更改为数组 它将用于一次只使用一个字符的模式匹配,然后尝试 在查询的数据库“ id”行中找到匹配项。关键是要使用ID的三分之二(可能是系统用户的ID不完整)来查询和查找完整的ID。请看我的代码。我将不胜感激。谢谢 我收到“未定义的偏移量0到9”错误。

<?php
session_start();
include_once('server.php');

$error = false;
$gat = "";
$get = "";
$rt1 = "";
$rt2 = "";
$rt3 = "";
$rt4 = "";
$rt5 = "";
$rt6 = "";
$rt7 = "";
$rt8 = "";
$rt9 = "";
$rt0 = "";


if(isset($_POST['btn-login'])){

$firstname = $_POST['firstname'];
$firstname = trim($firstname);
$firstname = trim($_POST['firstname']);
$firstname = htmlspecialchars(strip_tags($firstname));  

$lastname = $_POST['lastname'];
$lastname = trim($lastname);
$lastname = trim($_POST['lastname']);
$lastname = htmlspecialchars(strip_tags($lastname));

$id = $_POST['id'];
$id = trim($id);
$id = trim($_POST['id']);
$id = htmlspecialchars(strip_tags($id));

$gender = $_POST['gender'];


   if(!$error) {
    //search data if no errors 

    $query = "select * from subscribers";
    $conditions = array();

    if(! empty($firstname)){
        $conditions[] = "firstname='$firstname'";
    }

    if(! empty($lastname)){
        $conditions[] = "lastname='$lastname'";
    }

    if(! empty($gender)){
        $conditions[] = "gender='$gender'";
    }


   $sql = $query;
    if(count($conditions) > 0){
        $sql .= " WHERE " . implode(' AND ', $conditions);
    }


    $result = mysqli_query($conn, $sql);


    while($systemuser = mysqli_fetch_array($result, MYSQLI_ASSOC)){

      $systemuser['id'];
      $gat = $systemuser['id'];

    }       


  //convert user input to array
    $prima = $_POST['id'];
    $prima = array();
    $rt1 = $prima[0];
    $rt2 = $prima[1];
    $rt3 = $prima[2];
    $rt4 = $prima[3];
    $rt5 = $prima[4];
    $rt6 = $prima[5];
    $rt7 = $prima[6];
    $rt8 = $prima[7];
    $rt9 = $prima[8];
    $rt0 = $prima[9];

    //retrieve and convert db data into array
    $gat = array(); 




    foreach( $gat as $get ){ 

    $rt1 = $prima[0];      
    if (preg_match("/[$rt1]+/", $gat));{
        $get += 1;
     }

    $rt2 = $prima[1];       
    if (preg_match("/[$rt2]+/", $gat)){ 
        $get += 1;
    }

    $rt3 = $prima[2];
    if (preg_match("/[$rt3]+/", $gat)){ 
        $get += 1;
    }

    $rt4 = $prima[3];
    if (preg_match("/[$rt4]+/", $gat)){ 
        $get += 1;
    }

    $rt5 = $prima[4];
    if (preg_match("/[$rt5]+/", $gat)){ 
        $get += 1;
    }

    $rt6 = $prima[5];
    if (preg_match("/[$rt6]+/", $gat)){ 
        $get += 1;
    }

    $rt7 = $prima[6];
    if (preg_match("/[$rt7]+/", $gat)){ 
        $get += 1;
    }

    $rt8 = $prima[7];
    if (preg_match("/[$rt8]+/", $gat)){ 
        $get += 1;
    }

    $rt9 = $prima[8];
    if (preg_match("/[$rt9]+/", $gat)){
        $get += 1;
    }

    $rt0 = $prima[9];
    if (preg_match("/[$rt0]+/", $gat)){ 
        $get += 1;
    }

    if ($get > 9){
        echo 'match found!';

    }
    else{
        echo 'match not found!';
    }


    }




   }

}

?>

1 个答案:

答案 0 :(得分:0)

我开始重构,但是有太多错误了。

您收到Undefined offset: 0通知是因为...

<?php
$prima = array();
$rt1 = $prima[0];

在一个空数组上引用零偏移量。该代码没有任何意义。您似乎经常分配一个值,然后在下一行覆盖。

FWIW:PHP中的字符串可以behave much like an array ...

更新:

老实说,我对症结有点困惑。如果我想引用字符串中的第一个和第二个字母,我会这样做:

$str = "I like PHP";
$firstLetter = (isset($str[0])) ? $str[0] : '';
$secondLetter = (isset($str[1])) ? $str[1] : '';

看看您是否可以按照以下示例进行操作:

$str = 'I like PHP';
for ($i=0; $i<strlen($str); $i++) {
    echo $str[$i] . "|"; 
}

然后...您是否要进行WHERE foo LIKE 'a%'类型的查询? “通配符”搜索?