2012-05-09 132 views
-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> 
+3

這不是4chan。我們是這裏的專業人士; '你不'米姆和'XD'表情不會增加你的問題的價值,所以請不要使用它們。 –

+0

-shivan蛋糕php – Ying

+0

李昂葉 - 對不起,我編輯它。 – Ying

回答

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'); ?>