Php POST方法和数组

Php POST方法和数组,php,mysql,Php,Mysql,这是我的第一个php项目。我创建了一个网站,用户可以上传他们的图片,然后查看其他用户的图片,一次一个人(类似于旧的hotornot.com)。以下代码的工作原理如下: 我创建了一个数组(称为$alluser),其中包含除当前登录的用户($user)之外的所有成员 我创建了一个数组(称为$usersiviewed),其中包含$user以前喜欢(存储在likeprofile表中)或不喜欢(存储在dislikeprofile表中)的所有成员。likeprofile和dislikeprofile的第一列

这是我的第一个php项目。我创建了一个网站,用户可以上传他们的图片,然后查看其他用户的图片,一次一个人(类似于旧的hotornot.com)。以下代码的工作原理如下:

  • 我创建了一个数组(称为$alluser),其中包含除当前登录的用户($user)之外的所有成员
  • 我创建了一个数组(称为$usersiviewed),其中包含$user以前喜欢(存储在likeprofile表中)或不喜欢(存储在dislikeprofile表中)的所有成员。likeprofile和dislikeprofile的第一列包含喜欢/不喜欢的用户的名称,第二列包含他们喜欢/不喜欢的成员的名称
  • 我使用数组_diff从$alluser中去掉$usersiviewed。这是$user可以查看的用户列表(即,他们过去不喜欢或不喜欢的人)。 现在的问题是,当我单击like按钮时,它会用数组中下一个人的名字更新likeprofile表(即,不是我当前正在查看的照片的人,而是下一个显示照片的人)。此外,如果我刷新当前页面,则出现在当前页面上的个人资料会自动被我“喜欢”。如果你能给我一些建议,我将不胜感激

    <?php 
    
    // viewprofiles.php
    include_once("header.php");
    
    echo $user.' is currently logged in<br><br>';
    
    echo <<<_END
    <form method="post" action="viewprofiles.php"><pre>
    <input type="submit" name ="choice" value="LIKE" />
    <input type="submit" name ="choice" value="NEXT PROFILE" />
    </pre></form>
    _END;
    
    
    $allusers = array();
    
    //Create the $allusers array, comprised of all users except me
    $result = queryMysql("SELECT * FROM members");
    $num = mysql_num_rows($result);
    for ($j = 0 ; $j < $num ; ++$j)
    {
    $row = mysql_fetch_row($result);
    if ($row[0] == $user) continue;
    $allusers[$j] = $row[0];
    }
    
    
    
    //Create the $i_like_these_users array, comprised of all users i liked
    $result = queryMysql("SELECT * FROM likeprofile WHERE user='$user'");
    $num = mysql_num_rows($result);
    for ($j = 0 ; $j < $num ; ++$j)
    {
    $row = mysql_fetch_row($result);
    $i_like_these_users[$j] = $row[1];
    }
    
    //Create the $i_dislike_these_users array, comprised of all users i disliked
    $result = queryMysql("SELECT * FROM dislikeprofile WHERE user='$user'");
    $num = mysql_num_rows($result);
    for ($j = 0 ; $j < $num ; ++$j)
    {
    $row = mysql_fetch_row($result);
    $i_dislike_these_users[$j] = $row[1];
    }
    
    //Create the $usersiviewed array, comprised of all users i have either liked or disliked
    if (is_array($i_like_these_users) && is_array($i_dislike_these_users))
    {
    $usersiviewed = array_merge($i_like_these_users,$i_dislike_these_users);
    }
    elseif(is_array($i_like_these_users))
    {
    $usersiviewed = $i_like_these_users;
    }
    else
    {
    $usersiviewed = $i_dislike_these_users;
    }
    
    // this removes from the array $allusers (i.e., profiles i can view) all $usersviewed (i.e., all the profiles i have already either liked/disliked)
    if (is_array($usersiviewed))
    {
    $peopleicanview = array_diff($allusers, $usersiviewed);
    $peopleicanview = array_values($peopleicanview); // this re-indexes the array
    }
    else {
    $peopleicanview = $allusers;
    $peopleicanview = array_values($peopleicanview); // this re-indexes the array
    }
    
    
    $current_user_profile = $peopleicanview[0];
    echo 'check out '.$current_user_profile.'s picture <br />';
    if (file_exists("$current_user_profile.jpg"))
    {echo "<img src='$current_user_profile.jpg' align='left' />";}
    
    // if i like or dislike this person, the likeprofile or dislikeprofile table is updated with my name and the name of the person who liked or disliked
    if (isset($_POST['choice']) && $_POST['choice'] == 'LIKE')
    {
    $ilike = $current_user_profile;
    $query = "INSERT INTO likeprofile VALUES" . "('$user', '$ilike')";
    if (!queryMysql($query)) echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />";
    }
    
    if (isset($_POST['choice']) && $_POST['choice'] == 'NEXT PROFILE')
    {
    $idontlike = $current_user_profile;
    $query = "INSERT INTO dislikeprofile VALUES" . "('$user', '$idontlike')";
    if (!queryMysql($query)) echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />";
    }
    
    ?> 
    

    因为当您刷新页面时,它会发送
    
    当你喜欢一个用户时,下一个用户会喜欢它。。在获取行时,您的for循环中有一些内容…而不是for循环在循环时尝试一次…我希望它将解决您的问题

    您正在使用当前加载的用户计算
    $iLike
    变量,然后使用该用户更新数据库

    您可能应该稍微更改一下应用程序逻辑:

    • 除了like/not like变量外,还将您喜欢或不喜欢的用户的用户ID作为POST参数传递
    • 将表单处理逻辑移到页面顶部(或者更好地将表单处理与HTML显示分开)
    另外,最好不要在PHP中使用
    mysql.*
    扩展。使用
    mysqli
    PDO。

  • 试着做两种不同的形式。一个用“喜欢”,另一个用“下一个”,以避免从同一形式喜欢

  • 提交表单时,页面会刷新,因此在字符串
    $current\u user\u profile=$peopleicanview[0]中
    array
    $peopleicanview
    没有来自previuos页面的用户(提交前),您必须附加它,例如在隐藏字段中

    "INSERT INTO likeprofile VALUES" . "('$user', '".$_POST['current_user']."')"
    
    ps从表单中删除


  • 让我们从简化和组织代码开始


    谢谢!!这太完美了!
    
    "INSERT INTO likeprofile VALUES" . "('$user', '".$_POST['current_user']."')"
    
    <?php 
     // viewprofiles.php
    include_once("header.php");
    
    //if form is sent, process the vote.
    //Do this first so that the user voted on wont be in results later(view same user again)
    //use the user from hidden form field, see below
    $userToVoteOn = isset($_POST['user-to-vote-on']) ? $_POST['user-to-vote-on'] : '';
    
        // if i like or dislike this person, the likeprofile or dislikeprofile table is updated with my name and the name of the person who liked or disliked
    if (isset($_POST['like']))
    {
        $query = "INSERT INTO likeprofile VALUES" . "('$user', '$userToVoteOn ')";
        if (!queryMysql($query)) 
            echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />";
    }
    
    if (isset($_POST['dislike']))
    {
        $query = "INSERT INTO dislikeprofile VALUES" . "('$user', '$userToVoteOn ')";
        if (!queryMysql($query))
            echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />";
    }
    
    //now we can create array of available users.
    $currentProfileUser = array();
    
    //Create the $currentProfileUser array,contains data for next user.
    //join the 2 other tables here to save php processing later.
    $result = queryMysql("SELECT `user` FROM `members`
                          WHERE `user` NOT IN(SELECT * FROM `likeprofile` WHERE user='$user')
                          AND `user` NOT IN(SELECT * FROM `dislikeprofile` WHERE user='$user')
                          and `user` <> '$user'
                          LIMIT 1");
    //no need for a counter or loop, you only need the first result.
    if(mysql_num_rows > 0)
    {
        $row = mysql_fetch_assoc($result);
        $current_user_profile = $row['user'];
    }
    else
     $current_user_profile = false;
    
    
    
    echo $user.' is currently logged in<br><br>';  
    
    //make sure you have a user
    if($current_user_profile !== false): ?>
    <form method="post" action="viewprofiles.php">
        <input type="hidden" name="user-to-vote-on" value="<?=$current_user_profile?>" />
        <input type="submit" name ="like" value="LIKE" />
    </form>
    <form method="post" action="viewprofiles.php">
        <input type="hidden" name="user-to-vote-on" value="<?=$current_user_profile?>" />
        <input type="submit" name ="dislike" value="NEXT PROFILE" />
    </form>
    
    check out <?=$current_user_profile?>'s picture <br />
        <?php    if (file_exists("$current_user_profile.jpg")): ?>
           <img src='<?=$current_user_profile.jpg?>' align='left' />
        <?php    endif; //end check if image exists ?>
    <?php else: //no users found ?>
     Sorry, there are no new users to view
    <?php endif; //end check if users exists. ?>