【问题标题】:Unable to connect the Database and Handle the POST request无法连接数据库并处理 POST 请求
【发布时间】:2018-02-17 05:11:46
【问题描述】:

您好,我正在使用预定义的模板,我正在尝试使用 php 以 POST/GET 请求的形式从输入空间获取一些数据。但是我做不到,如何整合数据库和处理php参数?

<div class="w3_agileits_card_number_grids">
     <div class="w3_agileits_card_number_grid_left">
        <div class="controls">
           <input type="text" placeholder="Adhaar" name="Adhaar" required="">
        </div>
    </div>
    <div class="controls">
        <input type="text" placeholder="Town/City" name="city" required="">
    <?php 
    if(isset($_GET['Adhaar']) && $_GET ['Adhaar']!=NULL)
    {
       $x = $_GET['Adhaar'];
        echo "Your Adhaar is $x";
    ?>
    }

【问题讨论】:

  • 您的代码中有错字。我修的时候注意到了。它读取?&gt; 然后}
  • @SuperKevin 不起作用尝试修复它
  • PHP 在服务器上运行。这意味着您需要处理/提交页面,以便 PHP 从您的输入中读取数据。
  • 所以你的意思是在页面加载时你想从数据库中获取数据并显示在这个页面上?

标签: php html database


【解决方案1】:
Hello change your code to this

    <div class="w3_agileits_card_number_grids">
        <div class="w3_agileits_card_number_grid_left">
            <div class="controls">
             <input type="text" placeholder="Adhaar" name="Adhaar" required="">
            </div>
        </div>
    <div class="controls">
       <input type="text" placeholder="Town/City" name="city" required="">
             <?php 
                  if(isset($_GET['Adhaar']) && $_GET ['Adhaar']!=NULL)
                            {
                              $x = $_GET['Adhaar'];
                              echo "Your Adhaar is $x";
                              //Connect to the database here

                            }
             ?>
    </div>
</div>

对于数据库连接,它取决于您正在使用的数据库,但您可以从这里开始。一个简单的 Google 查询即可为您提供所需的内容

【讨论】:

    【解决方案2】:

    我为您整理了一个可能会派上用场的示例。这显示了如何使用 PHP 提交表单打印用户在页面上输入的一些值。我还包含了一些注释掉的代码,您可以将它们复制并移动到单独的脚本中,并通过将 action 值更改为文件路径来调用它们。

    PHP 脚本:

    <?php
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            // try {
    
            // Connect to the database:
            // $db = mysqli_connect('localhost', 'username', 'password', 'database','port');
            // Retrieve all records:
            // $sql = 'SELECT * FROM categories';
            // $result = $db->query($sql);
    
            // } catch (Exception $e) {
            // $error = $e->getMessage();
            // }
    
            // echo '<pre>';
            // Pass MYSQLI_BOTH or MYSQLI_ASSOC as the argument to change the array type
            // $all = $result->fetch_all();
            // echo json_encode($all);
            // echo '</pre>';
            // $db->close();
    
            $data = [
                "BOB" => "AWESOME",
                "JOE" => "AVERAGE",
                "TOM" => "COOL"
            ];
        }
    ?>
    

    接下来,我们有表格。我添加此表单是因为您需要将其提交到页面。 (好吧,你并不“需要”它,但它让生活变得轻松。)

    <div class="container">
        <form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
    
            <div class="form-group">
               <input class="form-control"
                      type="text"
                      placeholder="Adhaar"
                      name="adhaar"
                      required
                      value="<?= isset($_POST['adhaar']) ? $_POST['adhaar'] : '' ?>">
            </div>
    
            <div class="form-group">
                <input class="form-control"
                       type="text"
                       placeholder="Town/City"
                       name="city"
                       required
                       value="<?= isset($_POST['city']) ? $_POST['city'] : '' ?>">
            </div>
    
            <div class="form-group">
                <button type="submit" class="btn btn-primary">CLICK ME!</button>
            </div>
    
        </form>
        <?php if (isset($_POST['adhaar'])) : ?>
            <p>Hi there <?= $_POST['adhaar'] ?></p>
        <?php endif ?>
    
        <?php if (isset($_POST['city'])) : ?>
            <p><?= $_POST['city'] ?> is a great place to live!</p>
        <?php endif ?>
    
        <?php if (isset($data)) : ?>
            <?php foreach ($data as $key => $value) : ?>
                <p><?= $key ?> - <?= $value ?></p>
            <?php endforeach ?>
        <?php endif ?>
    </div>
    

    如果文件的最后一部分在 $_POST 全局数组中找到它,它只会将信息输出到页面上。

    <?php if (isset($_POST['adhaar'])) : ?>
        <p>Hi there <?= $_POST['adhaar'] ?></p>
    <?php endif ?>
    
    <?php if (isset($_POST['city'])) : ?>
        <p><?= $_POST['city'] ?> is a great place to live!</p>
    <?php endif ?>
    
    <?php if (isset($data)) : ?>
        <?php foreach ($data as $key => $value) : ?>
            <p><?= $key ?> - <?= $value ?></p>
        <?php endforeach ?>
    <?php endif ?>
    

    此处注释掉的部分可用于从数据库中提取数据并将其传递回您的页面。如果您刚刚开始,修补起来很酷,但理想情况下,您不想在与视图相同的页面上调用数据库。它应该存在于它自己的文件中。

            // try {
    
            // Connect to the database:
            // $db = mysqli_connect('localhost', 'username', 'password', 'database','port');
            // Retrieve all records:
            // $sql = 'SELECT * FROM categories';
            // $result = $db->query($sql);
    
            // } catch (Exception $e) {
            // $error = $e->getMessage();
            // }
    
            // echo '<pre>';
            // Pass MYSQLI_BOTH or MYSQLI_ASSOC as the argument to change the array type
            // $all = $result->fetch_all();
            // echo json_encode($all);
            // echo '</pre>';
            // $db->close();
    

    【讨论】:

      【解决方案3】:

      你应该试试这个代码..这是工作我只需添加一个提交按钮

      <div class="w3_agileits_card_number_grids">
              <div class="w3_agileits_card_number_grid_left">
                  <div class="controls">
                  <form method="GET" action="xxx.php">
                   <input type="text" placeholder="Adhaar" name="Adhaar" required="" />
                  </div>
              </div>
          <div class="controls">
             <input type="text" placeholder="Town/City" name="city" required="" />
             <input type="submit" name="submit" value="show">
                   <?php 
                        if(isset($_GET['submit']) && $_GET ['Adhaar']!=NULL)
                                  {
                                    $x = $_GET['Adhaar'];
                                    echo "Your Adhaar is $x";
                                    //Connect to the database here
      
                                  }
                   ?>
      
          </div>
          </form>
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-30
        • 1970-01-01
        • 1970-01-01
        • 2016-05-10
        • 1970-01-01
        • 2010-12-08
        相关资源
        最近更新 更多