简体   繁体   English

如何在PHP中的mysqli联接查询中遍历多个变量

[英]How to loop through multiple variables for a mysqli join query in php

I currently have a webpage setup with a textarea that should be used to enter in a variable(s) to then be used in a subsequent mysqli query. 我目前有一个带有textarea的网页设置,应将其用于输入变量,然后在随后的mysqli查询中使用。 The query does a couple joins in the database and then lists the results in a table. 该查询在数据库中进行了几次联接,然后在表中列出了结果。 How would I be able to enter multiple entries in the textarea to do the query multiple times and list all results in the table? 我如何能够在文本区域中输入多个条目以进行多次查询并在表中列出所有结果?

-- also, would it be possible to have the textarea entry go to different variables based on the text format: ie if the entry is letters/numbers -> $variable1, if the entry is dbname:(letters/numbers) -> $variable2 so then I could have multiple "WHERE" clauses be specified by different variables matching in different columns. -同样,是否有可能使textarea条目根据文本格式进入不同的变量:即,如果条目是字母/数字-> $ variable1,如果条目是dbname :(字母/数字)-> $ variable2,因此我可以通过在不同列中匹配的不同变量来指定多个“ WHERE”子句。

The code is below: 代码如下:

<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>

通过循环找到它

$variable = explode("\n", $_POST['variable']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM