【问题标题】:How to get data from the form如何从表单中获取数据
【发布时间】:2016-09-19 01:12:45
【问题描述】:
<form name="screenDishForm"  action="bentoQuery.php"  method="POST"> 
        <!--Label and input for the name of dish -->
            <label class="contentLabel"> Name: </label><br> 
            <input class="inputField" type="text" id="dishName" name="dishName" placeholder="E.g. Beef Teriyaki" required><br>
                        
    <!-- Label and Drop down list for the type of dish -->
            <label class="contentLabel"> Type: </label><br> 
            <select class = "inputField" id="dishType" name = "dishType">
                   <option value="mainDish">Main Dish</option>
                   <option value="sideDish">Side Dish</option>
                   <option value="soup">Soup</option>
            </select><br>
                     
    <!--Label and input for the price of dishes -->
            <label class="contentLabel"> Price: </label> <br>
            <input class="inputField" type="number" minimum="0" id="dishPrice" name="dishPrice" required><br>
                    
    <!--Button that will be used to register the dishes. -->
           <button class="btnRegister" type="submit" name="submit"> Register Dish </button>

 </form>

这是我命名为bentoApp.php的表单

   <?php
     require_once 'dbConn.php';

         $dishName = $_POST['dishName'];
         $dishType = $_POST['dishType'];
         $dishPrice = $_POST['dishPrice'];

       $sql = "INSERT INTO dish(dishName, dishType, dishPrice)
        VALUES ('$this->dishName', '$this->dishType', '$this->dishPrice')";

  ?>

这是我的 BentoQuery.php 的内容

我将如何获取表单的数据? 原来是一个错误

致命错误:当不在对象上下文中时使用 $this

【问题讨论】:

  • 那么,这一切的课程在哪里?如果您不使用类,那么为什么要使用 $this-&gt; 而不是变量?您也从未执行过查询,我们也无法知道您使用哪个 API 进行连接。

标签: php sql database


【解决方案1】:

从你的 mysql Values 变量中删除$this-&gt;。我假设您在这里犯了错误,如果不是问题是您将发布的值分配给变量但没有使用这些值。你写它们就好像你指的是一个你目前没有的对象的实例,即使这样也是不正确的。

这就是您的代码应该是什么样子。

   <?php
     require_once 'dbConn.php';

         $dishName = $_POST['dishName'];
         $dishType = $_POST['dishType'];
         $dishPrice = $_POST['dishPrice'];

       $sql = "INSERT INTO dish(dishName, dishType, dishPrice)
        VALUES ('$dishName', '$dishType', $dishPrice)";

  ?>

【讨论】:

  • VALUES ($dishName, $dishType, $dishPrice) 仍然会抛出一个错误,只是一个不同的错误。另外,该查询也从未被执行。
  • 我不是发布问题的人。
  • 另外问题不是他不知道如何执行查询。我使用他提供的代码回答了他的问题。
【解决方案2】:

只需使用您的变量 $dishName、$dishType、$dishPrice。

$this 用于引用您在其中使用它的对象的当前实例。您当前没有使用任何对象。

【讨论】:

    猜你喜欢
    • 2021-03-11
    • 2018-03-05
    • 2017-12-31
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2012-03-07
    相关资源
    最近更新 更多