我的想法: 我有一个用户数据库,每个用户都有一个自动递增的 ID 号字段,以及其他各种包含其详细信息的字段。我想在用户记录中存储一个包含一系列 friend 的字段。我想要最有效的方式来存储并能够搜索每个用户的“ friend ID 数组”字段并列出每个用户搜索的 friend ID。假设在某个时候可能有一百万个用户,每个用户在他们的数组中都有一百万个“ friend ID”,我认为用逗号分隔它们不会有效,有什么解决方案吗?它可以存储在一个 blob 中并按位搜索吗? (例如,每人一点,所以一个 1k 的 blob 可以存储多达 1024 个可能的 friend 组合)我认为另一个表是最好的解决方案。是多对多的关系,我为用户 friend 单独建了一张表:usrfrnd_tbl 包含列 UserFriendID(唯一键)、UserID、FriendID 在我将 Jquery 自动完成应用于注册表的 friend 字段时:当访问者在此输入字段中键入现有用户的名称时,首先,脚本搜索名称是否存在,完成它(如果存在),添加逗号。用户可以在此字段中输入第二个、第三个……现有的用户名,每次脚本都会自动完成。现在我想创建 php 文件来搜索这些用户名的 id,创建 id 数组,将它添加到 db 表中的表“usrfrnd_tbl”新用户“FriendID”字段中。 问题: 1.怎么做?现在,如何获取 id 的书面姓名数组,并在提交注册表后放入 usr_table 的“ friend ”字段? 2.我只需要书面姓名和全名的ID。我不需要值。如何从 jquery 代码中删除它? 对不起,我的英语不好 我的代码:
HTML
<form action="index.php" method="post">
<input class="std" type="text" name="friends" id="friends"/>
<input type="submit" name="submit">
</form>
Jquery
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#friends" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "autocomplete" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "search.php", {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
search.php
$db = new mysqli('localhost', 'user' ,'pass', 'db') or die(mysqli_errno());
$q = strtolower($_GET["term"]);
if (!$q) return;
$query = $db->query("select id, fullname from usr_table where fullname like '$q%'") or die(mysqli_errno());
$results = array();
while ($row = mysqli_fetch_array($query)) {$results[] = array ( "id" => $row[0] , "label" => $row[1], "value" => $row[1] );}
echo json_encode($results);
最佳答案
您需要做的是创建另一个表来存储 friend ID,您不应该在用户表中以逗号分隔值的形式执行此操作。例如,
表:用户
用户 ID、名字、姓氏
1、约翰·史密斯
2、苏珊·卡特
3、詹姆斯·布莱克
表: friend
ID, UserID, FriendID
1, 1, 2
2, 1, 3
3, 3, 2
上面的 Friends 表包含 3 行。它显示用户 ID 1 (John) 有两行,但两行的 friendID 不同。现在您需要将两个表连接在一起。示例
SELECT * FROM 用户 u INNER JOIN Friends f On f.UserID = u.UserID WHERE u.UserID =1
上面的sql会返回两行,就是John的两个 friend 。
关于php - 帮助!用户注册问题!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6378328/