-1

我无法理解它不会更新或保存我放置的任何记录。你能告诉我有什么问题吗?

这是我的 StockController 函数add

function add()
{
    if (!empty($this->data['Stocks']))
    {
        if ($this->Stocks->Save($this->data['Stocks']))
        {
            ($this->flash('Your Inventory has been added! /Stocks/'));
        }
    }
}

这是 add.ctp

 <h1>Add Inventory</h1>    
<form action="<?php echo $this->Html->url("/Stocks/add"); ?>" method="post">    

    <p>    
        ProductName:      
        <?php echo $this->Form->varchar('Stocks/ProductName', array('size' => '40'))?>    
    </p>    
    <p>    
        Price:      
        <?php echo $this->Form->integers('Stocks/Price', array('size' => '10'))?>    
    </p>    
<p>
   Quantity:
   <?php echo $this->Form->integers('Stocks/Quantity', array('size' => '5'))?>
</p>
    <p>    
        <?php echo $this->Form->submit('Save') ?>    
    </p>  

</form>  
4

1 回答 1

4

您没有提及您使用的是哪个版本的 CakePHP,并且您的示例中有相当多的非标准代码。

模型应该是单数的:

function add()
{
    if (!empty($this->data))
    {
        if ($this->Stock->Save($this->data))
        {
            ($this->flash('Your Inventory has been added! /Stocks/'));
        }
        else {
            debug($this->Stock->validationErrors);
        }
    }
}

(我添加的 else 将显示任何验证错误)

您的表单字段名称格式不正确:

h1>Add Inventory</h1>    
  <?php echo $this->Form->create('Stock'); ?>

    <p>    
        ProductName:      
        <?php echo $this->Form->varchar('Stocks.product_name', array('size' => '40'))?>    
    </p>    
    <p>    
        Price:      
        <?php echo $this->Form->integers('Stock.price', array('size' => '10'))?>    
    </p>    
<p>
   Quantity:
   <?php echo $this->Form->integers('Stock.quantity', array('size' => '5'))?>
</p>
    <p>    
        <?php echo $this->Form->submit('Save') ?>    
    </p>  

<?php echo $this->Form->end('Submit'); ?>
于 2012-05-09T14:46:56.530 回答