0

自分のサイト用に Twitter スタイルのフォロー システムを作成しようとしています。以下のスクリプトが実行されるたびに、フォローしようとしている人のユーザー ID だけでなく、データベース内に「配列」が追加されます。それを修正する方法がわからない。

<?
session_start();
# Connect to the mysql database
include_once "library/connect_to_mysql.php"; 

if(isSet($_POST['mem'])){

#filter everything but numbers for security
$mem1 = preg_replace('#[^0-9]#i', '', $_POST['mem']); 
$mem2 = $mem1;

#Decode the Session IDX variable and extract the user's ID from it
$decryptedID = base64_decode($_SESSION['idx']);
$id_array = explode("p3h9xfn8sq03hs2234", $decryptedID);
$my_id = $id_array[1];

$sql = mysql_query("SELECT following_array FROM Members WHERE id='$my_id' LIMIT 1"); 

while($row = mysql_fetch_array($sql)) { 
$following = $row["following_array"]; 
}

$followArry1 = explode(',', $following);

if (in_array($mem1, $followArry1)) {

exit(); 
}

if ($followArry1 != "") { 

$followArry2 = "$followArry1,$mem2"; 

} else { 

$followArry2 = "$mem2"; 
}

$UpdateArray = mysql_query("UPDATE Members SET following_array ='$followArry2' WHERE id='$my_id'") or die (mysql_error());

exit();

}else{

exit();
}

?>

爆発 (配列,2)

どんな助けでも大歓迎

/////// Updated Code ////////////////////

$followArry1 = explode(",", $following);

if (in_array($mem1, $followArry1)) { exit(); }

if ($followArry1 != "") { 

    $followArry1 = implode(',', $followArry1 + array($mem1)); 

} else { 

    $followArry1 = $mem1; 
}

$UpdateArray = mysql_query("UPDATE myMembers SET following_array ='$followArry1' WHERE id='$my_id'") or die (mysql_error());
4

2 に答える 2

2

$followArry1文字列に変換された配列は、" Array" を示します。

ここで証拠を参照してください (ライブデモ: http://ideone.com/tMndH ):

$followArry1 = array('a', 'b', 'c'); // just an array
$result = "$followArry1"; // it is now string containing "Array"

以下を試してください (例: http://ideone.com/h5ilz ):

$followArry1 = array('a', 'b', 'c');
$mem = 'd';
$followArry2 = implode(',', $followArry1 + array($mem));
于 2012-05-23T01:14:25.737 に答える
-2

また、問題は $followyArry1 配列に $mem 変数を追加することにあると思います。

このコードを試してください...

if ($followArry1 != "") { 
    $followArry1[] = $mem1;
    $followArry1 = implode(',', $followArry1); 

} else { 

    $followArry1 = $mem1; 
}
于 2012-05-23T02:13:27.877 に答える