如何随机选择两个字符串?

时间:2019-01-15 10:03:45

标签: php

我决定将获胜者存储到另一个数据库表中,然后使用它来将获胜者输出到另一页上。我有以下代码,但是由于某种原因,我的第二个查询无法正常工作,我已经检查了它通过...所有变量都是正确的,并且我的数据库表应该正确设置:

<?php
include_once __DIR__.'/header2.php';
if(!isset($_SESSION['u_uid'])) {
    echo "<meta http-equiv='refresh' content='0;url=index.php?level1promo_competitions_winner=notlogin'>"; 
    exit();
} else {
    if($_SESSION['u_permission'] == 0) {
        echo "<meta http-equiv='refresh' content='0;url=header2.php?level1promo_competitions_winner=nopermission'>"; 
       exit();
    } else {
        include_once __DIR__.'/includes/dbh.php';
        $level1promo_competitions_winner_form = strip_tags($_POST['level1promo_competitions_winner_form']);

        if ($level1promo_competitions_winner_form == $level1promo_competitions_winner_form) {


    $limit = 1;
    $sql = "SELECT * FROM level1promo_participants WHERE entry_id = ? ORDER BY RAND() LIMIT ?;";

     $stmt = mysqli_stmt_init($conn);
    if(!mysqli_stmt_prepare($stmt, $sql)) {
           echo "SQL error";
        } else {
          mysqli_stmt_bind_param($stmt, "si", $level1promo_competitions_winner_form, $limit);
          mysqli_stmt_execute($stmt);
          $result = mysqli_stmt_get_result($stmt);
          $resultCheck = mysqli_num_rows($result);


          if($resultCheck < 1) {
              echo '<div class="nolevel1promo_competitions_winner">There are no Level 1 Promo Competition\'s Winner yet for that entry id</div>';
              exit();
          } else {
                $row = mysqli_fetch_assoc($result);
                $useruid = $row['user_uid'];
                $email = $row['user_email'];

                echo $level1promo_competitions_winner_form;
                echo $useruid;
                echo $email;

                $sql2 = "INSERT INTO level1promo_winner (entry_id, user_uid, user_email) VALUES (?,?,?);";

                 $stmt = mysqli_stmt_init($conn);
                if (!mysqli_stmt_prepare($stmt, $sql2)) {
                         echo "SQL statement failed";

                      } else {
                           //Bind parameters to the placeholder
                        mysqli_stmt_bind_param($stmt, "sss", $levelpromo_competitions_winner_form, $useruid, $email);
                        mysqli_stmt_execute($stmt);


                       echo "<meta http-equiv='refresh' content='0;url=header2.php?level1promo_competitions_winner=success'>"; 
    }
}
}
}
}
}

?>

1 个答案:

答案 0 :(得分:0)

您可以使用以下查询轻松完成此操作。您无需为此投入太多逻辑。

select column_name from table_name order by RAND() limit 1