我在PHP方面的经验很少,但我认为您缺少mysqli\u stmt\u execute($defined\u chat)在您的代码中?所以我把它添加到我的代码中(检查代码输入和输出,两者仍然不起作用)。你也提到了,比如。查询'%{?}%'中的变量不应该是这

我在PHP方面的经验很少,但我认为您缺少mysqli\u stmt\u execute($defined\u chat)在您的代码中?所以我把它添加到我的代码中(检查代码输入和输出,两者仍然不起作用)。你也提到了,比如。查询'%{?}%'中的变量不应该是这,php,forms,Php,Forms,我在PHP方面的经验很少,但我认为您缺少mysqli\u stmt\u execute($defined\u chat)在您的代码中?所以我把它添加到我的代码中(检查代码输入和输出,两者仍然不起作用)。你也提到了,比如。查询'%{?}%'中的变量不应该是这样的吗?只是想获得一些知识:)mysqli\u stmt\u execute($defined\u chat)在答案中,您应该一直读到最后。通配符,是一个通配符,它匹配任何东西,当然可以与其他东西结合使用,但这不是这里的目的。在db上测试它:s


我在PHP方面的经验很少,但我认为您缺少
mysqli\u stmt\u execute($defined\u chat)在您的代码中?所以我把它添加到我的代码中(检查代码输入和输出,两者仍然不起作用)。你也提到了
,比如
。查询
'%{?}%'
中的变量不应该是这样的吗?只是想获得一些知识:)
mysqli\u stmt\u execute($defined\u chat)在答案中,您应该一直读到最后。通配符,是一个通配符,它匹配任何东西,当然可以与其他东西结合使用,但这不是这里的目的。在db上测试它:
select*来自性别类似“%”的用户。
@Freddy我的三元运算符分配确实异常。我更新了代码,所以您可以重试这一次-只是尝试了一下,仍然没有执行echo和header。我不明白为什么,这看起来像是基本的表单处理,但却一直把我送回同一页。你的说法是不真实的。OP有一个
order by rand()limit 1
在他的问答中,我很糟糕地解释了我想说的话。更新为正确。谢谢你的卡车,特拉维索^_^
 <form action="random_chat.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="age_from" id="age_from" value="0"/>
    <input type="hidden" name="age_to" id="age_to" value="50"/>
        <label for="amount">Age:</label>
        from:
        <select name="age_from" id="age_a" onchange="checkages_a()"> 
            <option value="none"></option>
            <?php
            for($i = 17; $i <= 50; ++$i) {
                echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
            }
            ?>
        </select>
        to: 
        <select name="age_to" id="age_b" onchange="checkages_b()"> 
            <option value="none"></option>
            <?php
            for($i = 18; $i <= 50; ++$i) {
                echo "\t", '<option value="', $i, '">', $i, '</option>', "\n";
            }
            ?>
        </select>

        <!-- I have input type submit above the radio buttons due to table layout -->
        <input type="submit" class="btn btn-info" name="submit" value="Click to start chat! " />

               <label for="amount">Gender:</label> 
                      <input type="radio" name="gender" value="male">Male</input> <br />
                      <input type="radio" name="gender" value="female">Female</input><br />
                      <input type="radio" name="gender" value="any">Any</input>     
</form>
<?php
$refined_gender = htmlentities (strip_tags(@$_POST['gender']));
$age_from       = htmlentities (strip_tags(@$_POST['age_from']));
$age_to         = htmlentities (strip_tags(@$_POST['age_to']));

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

if (isset($_POST['submit'])){

    // if age parameter used...
    $defined_chat = mysqli_prepare ($connect, "SELECT * FROM users WHERE gender =? AND age BETWEEN ? AND ? ORDER BY RAND() LIMIT 1");
    mysqli_stmt_bind_param($defined_chat, "sss", $refined_gender, $age_from, $age_to); 
    mysqli_stmt_execute ($defined_chat);

    while ($get_user = mysqli_fetch_assoc($defined_chat)){
        $rand_name = $get_user['username'];
        $acc_type = $get_user['account_type'];

        if ($acc_type != "admin" ){
            // if the name genereated by db is same as logged in users name, then run query again until name is found.  
            if ($rand_name == $username){
                $defined_chat;
                } else {
                    header ("Location: /messages.php?u=$rand_name");
                }
        } else {
            echo "No user found fitting those requirements.";
        }
    } // while closed
    mysqli_stmt_close($defined_chat);   
} 
?>
$sql = "SELECT 
          * 
        FROM 
          users 
        WHERE 
          gender like ? AND 
          age BETWEEN ? AND ? AND 
          # with this condition you do not need to test if the user logged is the queried one
          username != ? AND 
          # and with this one, you do not care about exclude adimn either
          account_type != 'admin'
        ORDER BY RAND() 
        LIMIT 1";
$defined_chat = mysqli_prepare (
    $connect, $sql
);
mysqli_stmt_bind_param(
    $defined_chat,
    "ssss",
    $refined_gender,
    $age_from,
    $age_to,
    $username
);
// Means if gender is different than 'any', it will assign the posted value to the variable, otherwise, it will assign the sql wildcard %
$refined_gender = (htmlentities (strip_tags(@$_POST['gender'])) != 'any' ? htmlentities (strip_tags(@$_POST['gender'])) : '%');
// Means if age is different than 'none', it will assign the posted value to the variable, otherwise, it will assign the lowest possible age, 0
$age_from       = (htmlentities (strip_tags(@$_POST['age_from'])) != 'none' ? htmlentities (strip_tags(@$_POST['age_from'])) : '0');
// Means if age is different than 'none', it will assign the posted value to the variable, otherwise, it will assign an age bigger than anyone could attain, 9999
$age_to         = (htmlentities (strip_tags(@$_POST['age_to'])) != 'none' ? htmlentities (strip_tags(@$_POST['age_to'])) : '9999');
$refined_gender = (htmlentities (strip_tags(@$_POST['gender'])) != 'any' ? htmlentities (strip_tags(@$_POST['gender'])) : '%');
$age_from       = (htmlentities (strip_tags(@$_POST['age_from'])) != 'none' ? htmlentities (strip_tags(@$_POST['age_from'])) : '0');
$age_to         = (htmlentities (strip_tags(@$_POST['age_to'])) != 'none' ? htmlentities (strip_tags(@$_POST['age_to'])) : '9999');

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

if (isset($_POST['submit'])){

    $sql = "SELECT 
              * 
            FROM 
              users 
            WHERE 
              gender like ? AND 
              age BETWEEN ? AND ? AND 
              # with this condition you do not need to test if the user logged is the queried one
              username != ? AND 
              # and with this one, you do not care about exclude adimn either
              account_type != 'admin'
            ORDER BY RAND() 
            LIMIT 1";
    $defined_chat = mysqli_prepare (
        $connect, $sql
    );
    mysqli_stmt_bind_param(
        $defined_chat,
        "ssss",
        $refined_gender,
        $age_from,
        $age_to,
        $username
    );
    mysqli_stmt_execute ($defined_chat);

    while ($get_user = mysqli_fetch_assoc($defined_chat)){
        $rand_name = $get_user['username'];
        header ("Location: /messages.php?u=$rand_name");
    } // while closed
    echo "No user found fitting those requirements.";
    mysqli_stmt_close($defined_chat);
}
set a tally count to zero
while we have some rows coming from the db {
    if that row is not admin {
        if that row does not match the current user {
            show the result
            increase tally count
        }
    }
}
if tally count is zero {
    say "no entries found"
}
$foundUsers = 0;
while ($get_user = mysqli_fetch_assoc($defined_chat)){
    $rand_name = $get_user['username'];
    $acc_type = $get_user['account_type'];

    if ($acc_type !== "admin" ){
        // if the name genereated by db is same as logged in users name, then run query again until name is found.  
        if ($rand_name !== $username) {
            $foundUsers = $foundUsers + 1;   // Or $foundUsers++ for short
            echo 'Matched User: ' . $rand_name . '<br>';
        }
    }
} // while closed
if ($foundUsers == 0) {
    echo "No user found fitting those requirements.";
}
<select name="age_from" id="age_a" onchange="checkages_a()"> 
    <option value="-1"></option>
    <?php
    for($i = 17; $i <= 50; ++$i) {
        echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
    }
    ?>
</select>
to: 
<select name="age_to" id="age_b" onchange="checkages_b()"> 
    <option value="999"></option>
    <?php
    for($i = 18; $i <= 50; ++$i) {
        echo "\t", '<option value="', $i, '">', $i, '</option>', "\n";
    }
    ?>
</select>
// If gender is specified, query gender
if($refined_gender !== "any"){
    $defined_chat = mysqli_prepare ($connect, "SELECT * FROM users WHERE gender =? AND age BETWEEN ? AND ? AND account_type != 'admin' ORDER BY RAND() LIMIT 1");
    mysqli_stmt_bind_param($defined_chat, "sii", $refined_gender, $age_from, $age_to); 
} else {
    $defined_chat = mysqli_prepare ($connect, "SELECT * FROM users WHERE age BETWEEN ? AND ? AND account_type != 'admin' ORDER BY RAND() LIMIT 1");
    mysqli_stmt_bind_param($defined_chat, "ii", $age_from, $age_to); 
}

mysqli_stmt_execute ($defined_chat);
   if ($acc_type != "admin" ){
        // if the name genereated by db is same as logged in users name, then run query again until name is found.  
        if ($rand_name == $username){
            $defined_chat;<-- don't you need to re-execute this?  Seems like you are hitting a race condition since the statement result will never change

            } else {
                header ("Location: /messages.php?u=$rand_name");
            }
    } else {
        echo "No user found fitting those requirements.";
    }