将表单内容中的字段与数据库中的表匹配(MySQL)
问题描述:
我在我的网站上设置了一个表单。我想知道,我怎么能匹配人们以某种形式在另一个表格中输入的内容。表单提交给一个表,并且我想检查表单提交的内容是否在另一个表中。将表单内容中的字段与数据库中的表匹配(MySQL)
在这种情况下,我有一个名为“Title”的字段,我想检查从“Form”下的表单输入的内容是否在另一个表中。
感谢您的帮助。
答
这里是一个粗略的轮廓:
步骤1:用户填写标题和命中提交
步骤2:页(代码),将插入所述用户输入运行下列
if (ctype_alnum($_POST['title']) { // Make sure the title is alphanumeric, not required but useful
$title = mysql_real_escape_string($_POST['title']); // This cleans the input if you don't use ctype_alnum
$sql = "SELECT * FROM table2 WHERE Title = $title"; // Query to find out if it exists in table 2
$result = mysql_query($sql); // Runs the query
if (mysql_num_rows($result) > 0) // Check to see if a row was found
$titleInTable2 = true; // Set a variable to true if a row was found
}
所以上述代码唯一的问题是,它只会查找完全匹配。如果你想有一个部分匹配,考虑使用LIKE %...%
或MATCH
像:http://dev.mysql.com/doc/refman/4.1/en/string-comparison-functions.html
比赛:http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
我只是不知道如何去这样做。呵呵。 – Nisto 2010-07-02 14:18:51