2015-11-19 41 views
1

我目前有一个带有textarea的网页设置,该文本应该用于输入变量,然后在随后的mysqli查询中使用。查询会在数据库中进行几次连接,然后将结果列在表中。我怎么能够在textarea中输入多个条目来多次执行查询并在表中列出所有结果?如何在php中使用多个变量进行mysqli连接查询

- 另外,是否有可能让textarea条目根据文本格式转到不同的变量:即如果条目是字母/数字 - > $ variable1,如果条目是dbname:(letters/numbers ) - > $ variable2所以然后我可以有多个“WHERE”子句由不同列中匹配的不同变量指定。

的代码如下:

<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 
    <title>Header</title> 
    <link rel="stylesheet" href="css/foundation.css" /> 
    <script src="js/vendor/modernizr.js"></script> 
</head> 
<style> 
    body { 
     background-color: ; 
    } 
</style> 
<body> 

    <style> 
     h1 { 
      text-align: center; 
     } 
     h3 { 
      text-align: center; 
     } 
     h5 { 
      text-align: center; 
     } 
     #panel { 
      min-height: 800px; 
     } 
     #search { 
      display: inline-block; 
     } 
    </style> 

    <div class="row"> 
     <div class="large-12 columns"> 
      <h1>Header</h1> 
      <hr/> 
     </div> 
    </div> 

    <div class="row"> 

     <div class="large-12 columns"> 
      <div id="panel" class="panel"> 
       <div id="search" class="large-4 columns"> 

        <form align="center" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post"> 

         <label>Insert Data 
          <textarea rows="25" name="variable"><?php echo $variable ?></textarea> 
         </label> 

         <input align="left" type="submit" name="query" value="Results" class="small radius button"></input> 

        </form> 
       </div> 

       <table align="center" role="grid"> 
        <thead> 
         <tr> 
          <th>1</th> 
          <th>2</th> 
          <th>3</th> 
          <th>4</th> 
         </tr> 
        </thead> 
        <tbody> 

         <?php 

          $variable = test_input($_POST['variable']); 

          function test_input($data) { 
           $data = trim($data); 
           $data = stripslashes($data); 
           $data = htmlspecialchars($data); 
           return $data; 
          } 

          if(isset($_POST['query'])){ 

           $mysqli = new mysqli("127.0.0.1", "root", "password", "dbname"); 
           if ($mysqli->connect_errno) { 
            echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
           } 

           echo $mysqli->host_info . "\n"; 

           $result = $mysqli->query("SELECT a.1, a.2, b.3, g.4 FROM alpha a LEFT OUTER JOIN beta b ON a.1 = b.1 LEFT OUTER JOIN gamma g ON a.1 = g.1 WHERE a.1='$variable';"); 
           if (!$result) { 
            echo "Database Query Failed: (" . $mysqli->errno . ") " . $mysqli->error; 
           } 
           $i = 0; 
           while ($row = $result->fetch_array()) { 

            $class = ($i == 0) ? "" : "alt"; 
            echo "<tr class=\"".$class."\">"; 
            echo "<tr>"; 
             echo "<td>" .$row["1"]. "</td>"; 
             echo "<td>" .$row["2"]. "</td>"; 
             echo "<td>" .$row["3"]. "</td>"; 
             echo "<td>" .$row["4"]. "</td>"; 
            echo "</tr>"; 
            $i = ($i==0) ? 1:0; 
           } 

           $mysqli->close(); 

          } 

         ?> 

        </tbody> 
       </table> 
      </div> 
     </div> 
    </div> 

<script src="js/vendor/jquery.js"></script> 
<script src="js/foundation.min.js"></script> 
<script> 
    $(document).foundation(); 
</script> 
</body> 

回答

0

想通了通过用

$variable = explode("\n", $_POST['variable']); 
循环
相关问题