POST方法和数组

时间:2014-03-10 04:04:05

标签: php mysql

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

  1. 我创建了一个数组(称为$ allusers),其中包含除当前登录用户($ user)以外的所有成员。
  2. 我创建了一个数组(称为$ usersiviewed),其中包含$ user之前喜欢(存储在likeprofile表中)或不喜欢(存储在dislikeprofile表中)的所有成员。 likeprofile和dislikeprofile的第一列有喜欢/不喜欢的用户的名字,第二列包含他们喜欢/不喜欢的成员的名字。
  3. 我使用array_diff从$ allusers中删除$ usersiviewed。这是$ user可以查看的用户列表(即他们过去从未喜欢或不喜欢的人)。 现在的问题是,当我点击“赞”按钮时,它会使用阵列中NEXT人员的姓名更新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 />";
    }
    
    ?> 
    

4 个答案:

答案 0 :(得分:0)

因为当你刷新页面时它会发送普通值 再次形成...并且问题当你喜欢用户它被下一个用户喜欢..在获取循环时有一些东西在循环中获取循环尝试一次循环...我希望它将解决你的问题问题

答案 1 :(得分:0)

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

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

  • 除了喜欢/不喜欢变量之外,还将您喜欢或不喜欢的用户的用户ID作为POST参数传递
  • 将表单处理逻辑移到页面顶部(或者更好地将表单处理与HTML显示分开)

此外,最好不要在PHP中使用mysql_*扩展名。使用mysqliPDO.

答案 2 :(得分:0)

  1. 尝试制作两种不同的形式。一个是“LIKE”,另一个是“NEXT”,以避免喜欢同一种形式

  2. 当您提交表单时 - 您的页面会刷新,因此在字符串$current_user_profile = $peopleicanview[0];数组$peopleicanview中没有来自previuos页面的用户(在提交之前),您必须附加它,例如在隐藏的领域

    <form method="post" action="viewprofiles.php">
    <input type="hidden" name="current_user" value="$current_user_profile" />
    <input type="submit" name ="choice" value="like" />
    </form>
    <form method="post" action="viewprofiles.php">
    <input type="submit" name ="go" value="next" />
    </form>
    

    稍后再插入

    "INSERT INTO likeprofile VALUES" . "('$user', '".$_POST['current_user']."')"
    

    ps从表单中删除<pre>

答案 3 :(得分:0)

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

<?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. ?>

你会注意到我改变了很多代码。您检查投票的顺序是问题的主要原因。但是过度复杂的代码使得很难看到发生了什么以及为什么。努力按照您希望它们运行的​​顺序组织代码,而不是投票或不投票,我还努力将标记与逻辑分开。这样可以减少在查找错误时需要挖掘的代码。

我还在原始查询中使用了sub queries来避免一堆不必要的PHP代码。您可以很容易地使用JOIN来获得相同的结果,但我认为这可以更清楚地表示正在发生的事情。此外,请在将来使用mysqli而不是deprecaded mysql,并注意SQL注入攻击,并至少使用real_escape_string。

希望它适合你。我也没有测试这段代码。可能是一些错误。