• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

检索Textarea Box中每行的值

JavaScript 来源:Sweaver 14次浏览

所以我遇到了关于textarea的问题。我的目标是创建一个带有textarea的表单,用户可以逐行输入字母数字名称,然后从数据库中提取该信息并将其显示到表格中。检索Textarea Box中每行的值

例如:

tt1 
tt2 
tt3 

并提交,将返回所有与这些名字3相关的数据。

我可以得到textarea,解析它并获取要插入到sql查询中的原始值,但是我在输出结果时被卡住了。

我现在的代码如下:

的index.html

<form method="POST" action="getreport.php"> 



     <div class="form-group"> 
     <label for="textarea">Textarea</label> 
     <textarea class="form-control" name="textarea" id="textarea" class="textarea" rows="5" cols="50"></textarea> 
     </div> 



     <button type="submit" class="btn btn-primary" >Submit</button> 
    </form> 

getreport.php

<?php 

     $servername = "localhost"; 
     $username = "root"; 
     $password = ""; 
     $dbname = "server"; 

     // Create connection 
     $conn = new mysqli($servername, $username, $password, $dbname); 
     // Check connection 
     if ($conn->connect_error) { 
      die("Connection failed: " . $conn->connect_error); 
     } 

     $text = trim($_POST['textarea']); 
     $textAr = preg_split('/[\n\r]+/', $text); //<--- preg_split is where the magic happens 
     $textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind 

     foreach ($textAr as $line) { 
      // processing here. 
      $sql = "SELECT * from guestlist WHERE guestname='$line'"; 
      echo "$sql"; //just checking query output for now 
     } 



     $result = $conn->query($sql); 

     if ($result->num_rows > 0) { 
      // output data of each row 
      while($row = $result->fetch_assoc()) { //<---- take this while loop out 


       echo '<table class="table table-striped table-bordered table-hover">'; 
       echo "<tr><th>Hostname</th><th>Guestname:</th><th>date</th><th>owner</th></tr>"; 
       while($row = mysqli_fetch_array($result)) 
       { 
        echo "<tr><td>"; 
        echo $row['hostname']; 
        echo "</td><td>"; 
        echo $row['guestname']; 
        echo "</td><td>";  
        echo $row['date']; 
        echo "</td><td>"; 
        echo $row['owner']; 
        echo "</td></tr>"; 
       } 
       echo "</table>"; 
      } //<-----as well as the closing bracket 
     } else { 
      echo "0 results"; 
     } 
     $conn->close(); 



    ?> 

任何帮助或指导,这将不胜感激。

感谢


===========解决方案如下:

不知道你的解决问题的方法是正确的,也有人认为你应该担心SQL注入。说,这可能是一个解决办法:

$text = trim($_POST['textarea']); 
    $textAr = str_replace("/n",",", $text); 
    $sql = "SELECT * from guestlist WHERE FIND_IN_SET(guestname,'$line')>0"; 

    $result = $conn->query($sql); 

另一种方法可能只是iteratin每次都

<?php 

     $servername = "localhost"; 
     $username = "root"; 
     $password = ""; 
     $dbname = "server"; 

     // Create connection 
     $conn = new mysqli($servername, $username, $password, $dbname); 
     // Check connection 
     if ($conn->connect_error) { 
      die("Connection failed: " . $conn->connect_error); 
     } 

     $text = trim($_POST['textarea']); 
     $textAr = explode("/n", $text); 
     $textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind 

     foreach ($textAr as $line) { 
      // processing here. 
      $sql = "SELECT * from guestlist WHERE guestname='$line'"; 
      echo "$sql"; //just checking query output for now 

     $result = $conn->query($sql); 
    // <---------------- ITERATE INSIDE THE FOREACH 
     if ($result->num_rows > 0) { 
      // output data of each row 
      while($row = $result->fetch_assoc()) { 


       echo '<table class="table table-striped table-bordered table-hover">'; 
       echo "<tr><th>Hostname</th><th>Guestname:</th><th>date</th><th>owner</th></tr>"; 
       while($row = mysqli_fetch_array($result)) 
       { 
        echo "<tr><td>"; 
        echo $row['hostname']; 
        echo "</td><td>"; 
        echo $row['guestname']; 
        echo "</td><td>";  
        echo $row['date']; 
        echo "</td><td>"; 
        echo $row['owner']; 
        echo "</td></tr>"; 
       } 
       echo "</table>"; 
      } 
     } else { 
      echo "0 results"; 
     } 

     } // Close the foreach 



     $conn->close(); 



    ?> 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)