检索Textarea Box中每行的值

时间:2017-06-25 00:13:18

标签: javascript php html mysql

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

例如:

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();



    ?>

对此有任何帮助或指导将不胜感激。

由于

2 个答案:

答案 0 :(得分:1)

不确定您解决问题的方法是否正确,并且有人建议您应该担心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();



    ?>

答案 1 :(得分:1)

不确定你为什么使用2 while循环--fetch_assoc和mysqli_fetch_array?每次调用都会将指针移动到下一行。也许这就是为什么你的表没有显示正确的数据?看来你可以删除fetch_assoc循环。