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


我创建一个数组(称为$ allusers),其中包含除当前登录的用户($ user)以外的所有成员。
我创建了一个数组(称为$ usersiviewed),该数组包含$ user以前喜欢(存储在likeprofile表中)或不喜欢(存储在dislikeprofile表中)的所有成员。 likeprofile和dislikeprofile的第一列包含执行过/不喜欢的用户的名称,第二列包含了他们喜欢/不喜欢的成员的名称。
我使用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 />";
}

?>

最佳答案

因为当您刷新页面时,它会发送previus的值
再次形成...以及当您喜欢某个用户时它又被下一个用户所喜欢的问题。.在获取行时,您在for循环中有一些东西...插入for循环尝试一次while循环...我希望它将解决您的问题问题

09-26 16:11