【问题标题】:Getting the id of an item selected by the user?获取用户选择的项目的ID?
【发布时间】:2014-02-06 06:19:42
【问题描述】:

在 phtml 文件中将我的所有数据库显示在一个表中,包括 priceid 等,然后在 php 文件中 Details 当用户尝试按租金时,它会获取项目的 ID 并存储它在店里使用add的方法我一直收到这个错误

Fatal error: Function name must be a string.

我从数据库中存储项目 id 的原因是,当用户结帐时,我将使用用户选择的项目的 id 检索所有信息。而且我正在使用数组列表来存储一个以上的项目,要提到的另一件事是他们获取项目ID的方式是通过添加购物车文件中的局部变量$tem = $_POST(['$dvdDetails->getDvdId()']);,但我不确定它是否存储正确的值,因为有一个 foreach 循环,并且变量的持续时间将在方法结束时被扭曲,所以我怎样才能获得被选中项目的 id。

<?php
    require_once('Models/Product.php');

    class Shop {
         private $_products = array();

        public function getProducts() 
            { return $this->_products;}

        public function addProduct(Product $product) 
            { $this->_products[] = $product; 
              return $this;
            }

    }

    ?>


    <?php


    class Option {
      private $_optionKey;
        private $_optionValue;

        public function getKey()
            { return $this->_optionKey; }

        public function getVlue()
            { 
            return $this->_optionValue; 

            }

        public function setOption($key, $value)
            { 
              $this->_optionKey = $key;
              $this->_optionValue = $value;
              return $this;
            }
    }

    ?>


    <?php
    require_once('Models/Option.php');



    class Product {
        private $_options = array();

        public function getOptions()
            { return $this->_options; }

        public function addOption(Option $option)
            { $this->_options[] = $option;
              return $this;
            }
    }

    ?>
    //SHow all 
    <?php require('template/header.phtml') ?> 
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 

     <table class="datatable"> 
     <tbody> 

     <?php foreach ($view->dd as $dvdDetails) { 

     echo '<tr> <td>' 
           .'<img src="images/'. $dvdDetails->getPhotoDetails() .'"alt="no picture" height="240" width="820" />' .'<br><br>'.'<font size="3" color="#2E2E2E"><center>'. $dvdDetails->getDvdPhoto(). '</center></font>'.
             '<br><br><br>'.  '<font size="2" color="blue"><strong>Genre: </strong></font>'.  $dvdDetails->getDvdGenre() .
              '<br><br>'. '<font size="2" color="blue"><strong>Director: </strong></font>'.  $dvdDetails->getDvdDirector() . '<br><br>' .'<font size="2" color="blue"><strong>Ritels: </strong></font>'. $dvdDetails->getDvdRitels() .
             '<br><br>' . '<font size="2" color="blue"><strong>Price for rent: </strong></font>'. $dvdDetails->getDvdId()) . 
       '<br><br>' .
          '<div class="ghassar">' .
             '<div id="op"> <label>Number of days </label> <select name="days" > <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> 
     </select> 
     <br><br><br><br> 

     </div> 



     <div> 



     <input type="submit" value="Add to the basket" name="rent" id="buttom1" /> 
     </div> ' . '<br><br>' .
           '<br><br>' .'<div>'.

    '</td> </td> </tr>'; 
     } ?> 
     </tbody> 
    </table>
     <?php require('template/footer.phtml') ?>

//加入购物车

<?php
require_once('Models/Dvd_sql.php'); 
require_once('Models/Shop.php');  


$view = new stdClass(); 
$view->dd = 'SQL'; 
$dvd_sql = new Dvd_sql(); 
$view->dd  = $dvd_sql->fetchAllStudents();    //->fetchAllStudents(); 
if (isset($_POST['rent']))
{
    $tem = $_POST(['$dvdDetails->getDvdId()']);
 $shop = new Shop(); $shop->addProduct($tem);
}

   require_once('Views/dvdDetails.phtml');

【问题讨论】:

    标签: php html mysql arrays post


    【解决方案1】:

    我认为您正在尝试将 DVD 的 ID 从表单页面获取到表单处理页面。假设就是这样,那么您的处理方式存在一些问题。

    • 首先,$_POST['$dvdDetails-&gt;getDvdId()'] 在帖子数据中查找与字符串“$dvdDetails->getDvdId()”字面匹配的字符串键。 PHP 不会对单引号中的字符串进行插值,即使使用双引号,您也确实希望坚持使用简单变量和/或使用 {$brackets}
    • 其次,在我看来对象$dvdDetails 甚至不存在于表单处理脚本中。我只在表单显示代码中看到它,并且不会自动将其转移到处理脚本中。您需要在表单中明确传递所需的数据。为此,您可以执行以下操作:

    在表单中(按照您的字符串连接样式)

    '<input type="hidden" name="item_id" value="' . $dvdDetails->getDvdId() . '">' .
    

    在处理脚本中

    $item_id = $_POST['item_id'];
    

    编辑
    第一次,我错过了您正在循环播放许多 DVD 项目的事实。在这种情况下,我的建议会导致很多 item_id 输入,而处理脚本只会看到一个(我认为是最后一个)。您的“天数”选择器也会遇到同样的问题。在这种情况下,我认为最简单的解决方案是为每张 DVD 创建一个单独的表单并使用我上面的建议。这样,当您提交特定 DVD 的表单时,只会发送一份“days”和“item_id”的副本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      相关资源
      最近更新 更多