2017-12-22 5 views
-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'], 채널에서 값을 가져옵니다

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

런타임시 화가.

0

WHERE $col1, $col2 LIKE '%$song_query%'는 그래서이 또한 당신이 당신의 테이블을 구축 실수의 몇 가지를 만들어주의 문제를

$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>"; 

을 수정해야

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

을 말할 필요가 유효하지 않은 구문입니다 내가 고친 것 같아.

+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

관련 문제