2017-04-25 4 views
-1
anzuzeigen

Ich möchte, dass mein HTML-Code den PHP-Code enthält, der von einer externen Datei ausgeführt wird. Ich bekomme Fehler zu viele Weiterleitungen und ich weiß nicht warum. Wenn ich es ohne den Include-PHP-Code in der HTML-Datei ausführe, funktioniert es, aber ich möchte PHP-Code unter meinem HTML und CSS laufen lassen.Stellen Sie HTML, um PHP

Das ist mein HTML-Code:

<nav id="search"> 
     <form action="./results.php" method="get"> 
      <input type="text" name="q" id="search_bar" placeholder="" value="Search..." maxlength="30" autocomplete="off" onMouseDown="active();" onBlur="inactive();" /> 
      <input type="submit" id="search_button" value="Compare!" /> 
     </form> 
    </nav> 

    <section> 

     <?php include("results.php");?> 

    </section> 

PHP-Code für searchbar:

$conn = mysqli_connect("localhost", "root", "project", "videogames"); 

if(mysqli_connect_errno()){ 
    echo "failed to connect: " .mysqli_connect_error(); 
} 

$output = ''; 

if(isset($_GET['q']) && $_GET['q'] !== ' '){ 
    $searchq = $_GET['q']; 


    $q = mysqli_query($conn, "SELECT * FROM games WHERE name LIKE '%$searchq%'") or die(mysqli_error()); 

    $c = mysqli_num_rows($q); 
    if($c == 0){ 
     $output = 'No Search Results for <b>"' . $searchq . '"</b>'; 
    } else { 
     while($row = mysqli_fetch_array($q)){ 
      $name = $row['name']; 
      $image_path = $row['image_path']; 
      $developer_name = $row['developer_name']; 
      $platform = $row['platform']; 
      $store = $row['store']; 
      $price = $row['price']; 

      $output .= '<br><table class="tg"> 
         <tr> 
          <th class="tg-031e colspan="4" rowspan="4"><img src= ' . $image_path . ' width=150 height=200/></th> 
          <th class="tg-031e" colspan="4">' . $name . '</th> 
          <th class="tg-031e" colspan="2">' . $platform . '</th> 
          </tr> 
          <tr> 
          <td class="tg-031e" colspan="4">' . $developer_name . '</td> 
          <td class="tg-031e"></td> 
          <td class="tg-031e"></td> 
          </tr> 
          <tr> 
          <td class="tg-031e" colspan="4">£' . $price . '</td> 
          <td class="tg-031e" colspan="2">' . $store . '</td> 
          </tr> 
          <tr> 
          <td class="tg-031e"></td> 
          <td class="tg-031e"></td> 
          <td class="tg-031e"></td> 
          <td class="tg-031e"></td> 
          <td class="tg-031e" colspan="2">Button</td> 

         </tr> 
         <br> 
         </table>'; 

     } 
    } 
} else { 
    header("location: ./"); 
} 
print("$output"); 
mysqli_close($conn); 
+8

Ihr Code ist anfällig für [** SQL-Injection-Angriffe **] (https://en.wikipedia.org/wiki/SQL_injection). Sie sollten [** mysqli **] (https://secure.php.net/manual/en/mysqli.prepare.php) oder [** PDO **] (https://secure.php.net/) verwenden. manual/de/pdo.prepared-statements.php) vorbereitete Anweisungen mit gebundenen Parametern wie in [** dieser Beitrag **] beschrieben (https://stackoverflow.com/questions/60174/how-cani-i-prevent-sql -injektion-in-php). –

+0

Das bedeutet, dass Sie für immer umleiten, tun Sie das an der 'else {header (" Location: ./ "); } '. Die 'if'-Anweisung wird in einen Zustand versetzt, in dem sie * immer * false zurückgibt und in den 'else'-Zustand wechselt. – Nytrix

+0

Nur möglich mit SSI https://httpd.apache.org/docs/current/howto/ssi.html – JustOnUnderMillions

Antwort

0

Sie brauchen nicht wirklich eine header('Location: ...') drin.

Probieren Sie es wie folgt aus:

$conn = mysqli_connect("localhost", "root", "project", "videogames"); 

if(mysqli_connect_errno()){ 
    echo "failed to connect: " .mysqli_connect_error(); 
} 

$output = ''; 

if(isset($_GET['q']) && $_GET['q'] !== ' '){ 
    $searchq = $_GET['q']; 


    $q = mysqli_query($conn, "SELECT * FROM games WHERE name LIKE '%$searchq%'") or die(mysqli_error()); 

    $c = mysqli_num_rows($q); 
    if($c == 0){ 
     $output = 'No Search Results for <b>"' . $searchq . '"</b>'; 
    } else { 
     while($row = mysqli_fetch_array($q)){ 
      $name = $row['name']; 
      $image_path = $row['image_path']; 
      $developer_name = $row['developer_name']; 
      $platform = $row['platform']; 
      $store = $row['store']; 
      $price = $row['price']; 

      $output .= '<br><table class="tg"> 
         <tr> 
          <th class="tg-031e colspan="4" rowspan="4"><img src= ' . $image_path . ' width=150 height=200/></th> 
          <th class="tg-031e" colspan="4">' . $name . '</th> 
          <th class="tg-031e" colspan="2">' . $platform . '</th> 
          </tr> 
          <tr> 
          <td class="tg-031e" colspan="4">' . $developer_name . '</td> 
          <td class="tg-031e"></td> 
          <td class="tg-031e"></td> 
          </tr> 
          <tr> 
          <td class="tg-031e" colspan="4">£' . $price . '</td> 
          <td class="tg-031e" colspan="2">' . $store . '</td> 
          </tr> 
          <tr> 
          <td class="tg-031e"></td> 
          <td class="tg-031e"></td> 
          <td class="tg-031e"></td> 
          <td class="tg-031e"></td> 
          <td class="tg-031e" colspan="2">Button</td> 

         </tr> 
         <br> 
         </table>'; 

     } 

     echo $output; 
    } 

    mysqli_close($conn); 
} 

Sie den Browser umleitet, wenn noch nichts gesucht worden ist; was bedeutet, dass es weiter leitet, was zu einer Auszeit führt.

Viel Glück!

HINWEIS: Abgesehen davon, bitte schauen Sie, was Alex Howansky in den Kommentaren schrieb. Ihr Code ist in Bezug auf die Sicherheit nicht sicher.

Verwandte Themen