1

我需要收集表单输入并将其存储到数据库中。这很容易。困难的部分是我无法在长文本字段上创建唯一键。

在这种情况下,通常会做些什么来帮助防止用户输入重复条目?

谢谢,亚历克斯

4

4 回答 4

4

更新

直接问题:

您应该像在此示例中一样创建一个附加字段

myId int(11) NOT NULL AUTO_INCREMENT,
myText TEXT,
myUnique VARCHAR(255) NOT NULL DEFAULT '0',
UNIQUE KEY myUnique (myUnique)

<form>
<textarea name="myText" ></textarea>
<input type="hidden" value="UNIQUE-ID" name="myUnique" />
</form>

笔记:

例子:

$myUnique = crc32("The quick brown fox jumped over the lazy dog.");

进一步的 SO 阅读:

于 2011-03-15T15:30:26.080 回答
3

Create a separate MD5 hash of the LONGTEXT entry to be triggered on INSERT and UPDATE. That hash column should have a UNIQUE key and should be of CHAR type (since it will always be the same size). This will allow for the unique key.

于 2011-03-15T15:32:41.933 回答
1

在长文本字段上创建索引。然后在保存记录之前,在现有记录中搜索用户刚刚输入的测试。如果找到,则不允许保存。

于 2011-03-15T15:28:54.667 回答
0
 $text='the long text to be inserted the long text to be inserted the long text to be inserted the long text to be inserted';
 //assuming field name (mylongtextfield)
 $result=mysql_query("SELECT id FROM mytable WHERE md5(mylongtextfield)='".md5($text)."'";
 if(!$result){
      //do your insert query
 }else{
      echo "the text already exist!";   
 }
于 2012-12-26T09:30:17.267 回答