2

id複数のフィールド ( 、firstnamesurname、)で構成されるテーブルがありますusernamesearch_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_countbyをインクリメントすることです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が初めてで、これが私の最初のコードです:)

4

2 に答える 2

0
...
while ($search_result_information = mysql_fetch_assoc($search_query_run)) {
    # add this following line
    mysql_query('UPDATE `users` SET search_count=search_count+1 WHERE id='.$search_result_information['id']);
    # Edit. Change result to show new number
    $search_result_information['search_count']++; # this adds 1 to the value (does not affect stored data in database)
    echo '<li>'.$search_result_information['username'].'.  This user has been searched: '.$search_result_information['search_count'].' times before.</li>';
}
...
于 2013-10-20T13:12:06.417 に答える
0

あなたの場合、見つかった値をフェッチし、更新ステートメントを実行して +1 をsearch_count列に追加する必要があります

$search_query = "SELECT id, firstname, surname, username, search_count FROM `users` WHERE `firstname` = '".mysql_real_escape_string($keyword)."' OR `surname` = '".mysql_real_escape_string($keyword)."' ORDER BY `search_count` DESC";
$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 = ;
                $update_search = "UPDATE users SET search_count = search_count + 1 WHERE id = {$search_result_information['id']}"; // so every `id` will increment its search_count with 1. You will need to select the rows once again, to take this count, or to manually increment in PHP
                mysql_query($update_search);
                echo '<li>'.$search_result_information['username'].'.  This user has been searched: '.$search_result_information['search_count'].' times before.</li>'; 

PS: mysql_* lib の使用は強くお勧めしません。公式ドキュメントhttp://www.php.net/manual/en/function.mysql-query.phpの赤いボックスからわかるように、現在実際にサポートされている API のいずれかを選択する必要があります。

于 2013-10-20T13:12:08.377 に答える