-2

こんにちは、MySQL データベースから本のリストを表示する php 検索ページを作成しようとしています。次に、本の名前をクリックすると、それらとの関係テーブルにある本のリストが表示されます。私はコードに少し苦労していて、誰かが手を貸してくれることを望んでいました

-これは私のsearch.phpファイルです

  <?php
    $i=0;
    $column_name = 'title'; // column to search by
    $k =$_GET['k'];
    $terms = explode(" ",$k);
    //connect before calling mysql_real_escape_string
    mysql_connect("localhost","","");
    mysql_select_db("test");
    $query ="SELECT id,title,author 
    FROM  books WHERE";
    foreach ($terms as $each){
        $i++;
        $each = '%' . $each . '%'; // add wildcard
        $each = mysql_real_escape_string($each); // prevent sql injection
        if($i==1)
            $query .= " $column_name LIKE '$each' ";
        else
            $query .= " OR $column_name LIKE '$each' ";

    }

    echo 'QUERY: ' . $query;

    $query = mysql_query($query) OR DIE(mysql_error());

//Code below is for using the relationships table assuming you have a column name id that
//references to the relationships table. Also, you should add a index on the column id.

$results = "";

while($row = mysql_fetch_array($query)) {
  $results .= '<li>
                <a href="book-relationships.php?id='.$row['relationshipid'].'">'.$row['title'].'  author: '.$row['author'].'</a>
              </li>';
}

$results = '<ul>' . $results . '</ul>';

echo $results;
4

1 に答える 1

3

次の行から "; を削除します。

FROM books WHERE ";                 ";

$i を宣言する必要があります

$i = 0;

SQL インジェクションを防ぐには、次を使用できます。

foreach ($terms as $each){
        $i++;
        $each = '%' . $each . '%'; // add wildcard
        $each = mysql_real_escape_string($each); // prevent sql injection
        if($i==1)
            $query .= " $keywords LIKE '$each' ";
        else
            $query .= " OR $keywords LIKE '$each' ";

    }

また、ユーザーが存在しないテーブルに変数を設定できないことを確認してください

完全なコード

<?php
    $i=0;
    $column_name = 'title'; // column to search by
    $k =$_GET['k'];
    $terms = explode(" ",$k);
    //connect before calling mysql_real_escape_string
    mysql_connect("localhost","","");
    mysql_select_db("test");
    $query ="SELECT id,title,author 
    FROM  books WHERE";
    foreach ($terms as $each){
        $i++;
        $each = '%' . $each . '%'; // add wildcard
        $each = mysql_real_escape_string($each); // prevent sql injection
        if($i==1)
            $query .= " $column_name LIKE '$each' ";
        else
            $query .= " OR $column_name LIKE '$each' ";

    }

    echo 'QUERY: ' . $query;

    $query = mysql_query($query) OR DIE(mysql_error());

//Code below is for using the relationships table assuming you have a column name id that
//references to the relationships table. Also, you should add a index on the column id.

$results = "";

while($row = mysql_fetch_array($query)) {
  $results .= '<li>
                <a href="book-relationships.php?id='.$row['id'].'">'.$row['title'].'  author: '.$row['author'].'</a>
              </li>';
}

$results = '<ul>' . $ results . '</ul>';

echo $results;
于 2012-05-21T14:05:53.157 に答える