2017-12-22 10 views
-1

私は2つの列を持つデータベースからユーザーの投稿を取得しようとしています。 1つはアーティスト用、もう1つはタイトル用です。私は単純なフォームから入力を受け取り、すべての同様の結果を次のページの表に出力したいと考えています。これまでに書いたスクリプト全体が含まれています。私はそのページに何のエラーも出ていないが、結果も得られていない。私は数日間、オンラインで自分自身でこれをクリアできるかどうかを見てきましたが、私はそのような運がなかったのです。申し訳ありませんが、私はこのサイトを初めて知り、できるだけ詳細を提供したいと思っています。エラーは発生していませんが、結果が得られません - mysql php db

<?php 
include("db_connect.php"); 
// - get form data from "searchform.php" 
$song_query = $_GET['song_query']; 
// - column 1 and column 2 are all we're looking for in the db 
// - title and artist are currently the two cols. Table is named 
"song_list" 
$col1 = "title"; 
$col2 = "artist"; 
$tablename = "song_list"; 
echo "<H1>Search results</H1>"; 
if ($song_query == "") { 
echo "What are you going to sing? You didn't enter a search term! Please 
try again."; 
exit; 
} 
// - pick the db connection out of the included file and give error if 
failed. 
mysqli_select_db($conn, $db_name) or die(mysqli_error()); 
// - cleans up string inputted by user to avoid harmful code insertion 
into form 
$song_query = strtoupper($song_query); 
$song_query = strip_tags($song_query); 
$song_query = trim($song_query); 
// - set up parameters for accessing the query of the db 
$sql = "SELECT $col1, $col2 FROM $tablename WHERE $col1, $col2 LIKE 
'%$song_query%'"; 
$result = mysqli_query($conn, $sql); 
if (isset($_GET['$result'])){ 
if (mysqli_num_rows($result) > 0){ 
    echo "<table><tr>"; 
    echo "<th>Artist</th>"; 
    echo "</tr>"; 

while($row = mysqli_fetch_array($result)){ 
    echo "<tr>"; 
    echo "<td>" . $row['$result'] . "</td>"; 
    echo "</tr>"; 
    echo "</table>"; 
    } 
    } 
} 
?> 
+0

'$ _GET ['$ result']'は何をしているのですか... –

+0

'' $ col1、$ col2 LIKE '%$ song_query%' ";'は何を期待しますか? –

+0

MySQLiデータベースの呼び出しが正しく機能することを確認するには、[mysqli errors](http://php.net/manual/en/mysqli.error.php)を常にチェックしてください。 – aynber

答えて

0

あなたは、実行時に構築されつつある間違ったSQL、持って

$sql = "SELECT title, artist FROM $tablename WHERE title, artist LIKE 
'%$song_query%'"; 

ルックここWHERE title, artist LIKE

$song_queryになり$_GET['song_query']、CHから値を取得します

$sql = "SELECT $col1, $col2 FROM $tablename WHERE $col1, $col2 LIKE 
'%$song_query%'"; 

ランタイムでの怒り。

0

このWHERE $col1, $col2 LIKE '%$song_query%'はあなたが

WHERE col1 LIKE '%something%' AND col2 LIKE '%something%' 

だから、これは問題に

$sql = "SELECT $col1, $col2 
     FROM $tablename 
     WHERE $col1 LIKE '%$song_query%' 
     AND $col2 LIKE '%$song_query%'"; 

Although this is wide open to SQL Injection Attack Even if you are escaping inputs, its not safe! Use prepared parameterized statements

$sql = "SELECT title, artist 
     FROM songlist 
     WHERE title LIKE ? AND artist LIKE ?"; 

$stmt = $conn->prepare($sql); 
$val = sprintf('%%%s%%', $song_query); 
$stmt->bind_param('ss',$val, $val); 

$stmt->execute(); 

$stmt->bind_result($title, $artist); 

echo "<table><thead><tr>"; 
echo "<th>Artist</th><td>Title</th>"; 
echo "</tr></thead><tbody>"; 

while ($stmt->fetch()) { 
    echo "<tr>"; 
     echo "<td>$artist</td>"; 
     echo "<td>$title</td>"; 
    echo "</tr>"; 
} 
echo "</tbody></table>"; 

を修正する必要があります。また、あなたのテーブルを構築ミスのカップルを作っ留意言う必要が無効な構文であります私は固定していると思う。

+0

ありがとうRiggsFolly!私はカラオケショーでローカルサーバー上でこれを使用していますので、私は注射の攻撃についてあまり心配していません。これをホストしているシステムにインターネットアクセスはありません。 – Isaac

+0

少なくとも、あなたが提供したコードスニペットでエラーが発生しています。 Lol ...警告:sprintf():36行目のC:\ xampp \ htdocs \ bluesky \ runit.phpの引数が少なすぎます 致命的なエラー:キャッチされていないエラー:Cのbooleanのメンバー関数bind_param() \ xampp \ htdocs \ bluesky \ runit.php:37スタックトレース:#0 {main}はC:\ xampp \ htdocs \ bluesky \ runit.phpで37行目にスローされました。 – Isaac

+0

Ahhはい。私の間違い。修正された 'sprintf()'を参照してください – RiggsFolly

関連する問題