id
複数のフィールド ( 、firstname
、surname
、)で構成されるテーブルがありますusername
。search_count
テーブルを検索して一致するものを検索する小さな検索エンジンを構築しましたfirstname
または に存在surname
し、問題なく結果を取得しています。
今、私がやろうとしているsearch_count
のは、一致するたびにフィールドを 1 ずつ増やすことです!
たとえば、次の表があるとしますusers
。
id | firstname | surname | username | search_count
1 | John | Mike | un1 | 0
2 | John | Jeff | un2 | 0
3 | Dale | John | un3 | 0
4 | Mike | Gorge | un4 | 0
Jeff
キーワードとして検索しているとしましょう
したがって、クエリは 1 つのレコードを返します
私がやりたいのは、一致レコードのsearch_count
byをインクリメントすることです1
したがって、結果は次のようになります。
id | firstname | surname | username | search_count
2 | John | Jeff | un2 | `1`
新しい検索を行うと (例: John
)、結果は次のようになります。
id | firstname | surname | username | search_count
1 | John | Mike | un1 | 1
2 | John | Jeff | un2 | 2
3 | Dale | John | un3 | 1
私はいくつかのアプローチを試みましたが、運がありません..だから、ヒントと助けに感謝します
これが私のコードです...
<?php
// open the HTML page
include 'html_open.php';
// require the db connection
require '/inc/db.inc.php';
// require the error messages
require '/inc/echo.inc.php';
if (isset($_GET['keyword'])) {
$keyword = $_GET['keyword'];
if (!empty($keyword)) {
// build our search query
$search_query = "SELECT * FROM `users` WHERE `firstname` = '".mysql_real_escape_string($keyword)."' OR `surname` = '".mysql_real_escape_string($keyword)."' ORDER BY `search_count` DESC";
// run the search query
$search_query_run = mysql_query($search_query);
// search results
$search_results_num = mysql_num_rows($search_query_run);
// check query return results
if ($search_results_num>0) {
echo 'Search engine returns <strong>[ '.$search_results_num.' ]</strong> result(s) for <strong>[ '.$keyword.' ]</strong>:<br>';
// retrieving the information found
echo '<ol>';
while ($search_result_information = mysql_fetch_assoc($search_query_run)) {
//$current_search_count = ;
echo '<li>'.$search_result_information['username'].'. This user has been searched: '.$search_result_information['search_count'].' times before.</li>';
}
echo '</ol><hr>';
include 'search_form.php';
} else {
echo '<hr>Search engine returns no result for <strong>[ '.$keyword.' ]</strong>, please try another keyword.<hr>'; // hint: no result found
include 'search_form.php';
}
} else {
echo $err20_002; // hint: must insert input
include 'search_form.php';
}
} else {
echo $err20_001; // hint: form has not been submitted
include 'search_form.php';
}
// close the HTML page
include 'html_close.php';
?>
PS私はPHP/MySQLが初めてで、これが私の最初のコードです:)