在Codeigniter中将多个特色产品信息保存到MySQL中

时间:2013-10-03 07:03:11

标签: php mysql codeigniter

我在jpg文件中附加了我的问题,以解决代码格式:::

的问题

问题是它没有将任何数据保存到“tbl_featured_table”中。

控制器

public function save_featured_product() {
$data = array();

if ($this->input->post()) {
$data['featured_id'] = $this->input->post('featured_id', true);
$data['product_id'] = $this->input->post('product_id', true);
$data['product_name'] = $this->input->post('product_name', true);

$data['featured_product_name'] = $this->input->post('featured_product_name', true);
$data['featured_product_price'] = $this->input->post('featured_product_price', true);
$data['featured_product_quantity'] = $this->input->post('featured_product_quantity', true);
$data['featured_product_sku'] = $this->input->post('featured_product_sku', true);
$data['featured_product_short_description'] = $this->input->post('featured_product_short_description', true);
$data['featured_product_long_description'] = $this->input->post('featured_product_long_description', true);
$data['featured_product_status'] = $this->input->post('featured_product_status', true);
$data['featured_product_image'] = $this->input->post('featured_product_image', true);

$this->sa_model->save_featured_product_info($data);
}

模型

function save_featured_product_info($data){
if (isset($data['product_id']) && is_array($data['product_id'])) {
foreach ($data['product_id'] as $key => $each) {
$temp[] = array(
'featured_id' => $data['featured_id'][$key],
'product_id' => $data['product_id'][$key],
'product_name' => $data['product_name'][$key],
'featured_product_name' => $data['featured_product_name'][$key],
'featured_product_price' => $data['featured_product_price'][$key],
'featured_product_quantity' => $data['featured_product_quantity'][$key],
'featured_product_sku' => $data['featured_product_sku'][$key],
'featured_product_short_description' => $data['featured_product_short_description'][$key],
'featured_product_long_description' => $data['featured_product_long_description'][$key],
'featured_product_status' => $data['featured_product_status'][$key],
'featured_product_image' => $data['featured_product_image'][$key],
);
}
if (isset($temp)) {
$this->db->insert_batch('tbl_featured_products', $temp);
}

视图

        Product Name:
    <select name="product_id[]">
    <?php foreach ($all_product as $values) { ?>
    <option value="<?php echo $values->product_name; ?>"><?php echo $values->product_name; ?></option>
    <?php } ?>
    </select>
    <?php foreach ($all_product as $values) { ?>
    <input type="checkbox" name="featured_product_name[]" value="<?php echo $values->product_name; ?>" /> 
    <?php echo $values->product_name; ?> <br>
    <input hidden="hidden" name="featured_id[]" value="<?php echo $values->product_id; ?>" />
    <input type="hidden" name="product_name[]" value="<?php echo $values->product_name; ?>" >
    <input name="featured_product_price[]" value="<?php echo $values->product_price;?>" /> 
    <input name="featured_product_quantity[]" value="<?php echo $values->product_quantity; ?>" /> 
    <input name="featured_product_sku[]" value="<?php echo $values->product_sku; ?>" /> 
    <input name="featured_product_short_description[]" value="<?php echo $values->product_short_description; ?>" /> 
    <input name="featured_product_long_description[]" value="<?php echo $values->product_long_description; ?>" /> 
    <input name="featured_product_status[]" value="<?php echo $values->product_status; ?>" /> 
    <input name="featured_product_image[]" value="<?php echo $values->product_image; ?>" /> 
    <?php } ?>

注意:::我想按产品名称或产品ID保存多个数据。它运作不佳。我需要你的帮助来解决我的问题。

请专业人士帮助我。请不要将我评为否定。

2 个答案:

答案 0 :(得分:2)

查看

<select name="product_id[]">
<?php foreach ($all_product as $values) { ?>
<option value="<?php echo $values->product_name; ?>"><?php echo $values->product_name; ?></option>
<?php } ?>
</select>
<?php foreach ($all_product as $values) { ?>
<input type="checkbox" name="product_id[]" value="<?php echo $values->product_id; ?>" /> 
<?php echo $values->product_name; ?> <br />
<!-- <input hidden="hidden" name="featured_id[]" value="<?php //echo $values->product_id; ?>" /> -->
<input type="hidden" name="product_name[]" value="<?php echo $values->product_name; ?>" />
<input name="featured_product_price[]" value="<?php echo $values->product_price;?>" /> 
<input name="featured_product_quantity[]" value="<?php echo $values->product_quantity; ?>" /> 
<input name="featured_product_sku[]" value="<?php echo $values->product_sku; ?>" /> 
<input name="featured_product_short_description[]" value="<?php echo $values->product_short_description; ?>" /> 
<input name="featured_product_long_description[]" value="<?php echo $values->product_long_description; ?>" /> 
<input name="featured_product_status[]" value="<?php echo $values->product_status; ?>" /> 
<input name="featured_product_image[]" value="<?php echo $values->product_image; ?>" />

<强>控制器

function save_featured_product() {
    $data = array();
    $feat = array();
    $fin  = array();
    if ($this->input->post()) {
        $feat    = $this->input->post('product_id', true);
        $data['featured_id']                        = $this->input->post('featured_id', true);
        //$data['product_id']                         = $this->input->post('product_id', true);
        $data['product_name']                       = $this->input->post('product_name', true);
        //$data['featured_product_name']              = $this->input->post('featured_product_name', true);
        $data['featured_product_price']             = $this->input->post('featured_product_price', true);
        $data['featured_product_quantity']          = $this->input->post('featured_product_quantity', true);
        $data['featured_product_sku']               = $this->input->post('featured_product_sku', true);
        $data['featured_product_short_description'] = $this->input->post('featured_product_short_description', true);
        $data['featured_product_long_description']  = $this->input->post('featured_product_long_description', true);
        $data['featured_product_status']            = $this->input->post('featured_product_status', true);
        $data['featured_product_image']             = $this->input->post('featured_product_image', true);
       if( isset( $feat ) && is_array( $feat ) && count( $feat ) > 0 ){
            foreach( $feat as $key => $each ){
                $fin[]  = array(
                'product_id'                            => $each,
                'product_name'                          => $data['product_name'][$key],
                'featured_product_price'                => $data['featured_product_price'][$key],
                'featured_product_quantity'             => $data['featured_product_quantity'][$key],
                'featured_product_sku'                  => $data['featured_product_sku'][$key],
                'featured_product_short_description'    => $data['featured_product_short_description'][$key],
                'featured_product_long_description'     => $data['featured_product_long_description'][$key],
                'featured_product_status'               => $data['featured_product_status'][$key],
                'featured_product_image'                => $data['featured_product_image'][$key],
                );  
            }
       } 
    $this->sa_model->save_featured_product_info($fin);
    }
}

<强>模型

function save_featured_product_info($data){
    if( is_array( $data ) && count( $data ) > 0 ){
        $this->db->insert_batch('tbl_featured_products', $data);    
    }
}

答案 1 :(得分:1)

我只想给你一些一般的提示。并且在你的$ this-&gt; input-&gt; post

中包含xss clean TRUE的荣誉

始终只使用一个或两个值启动这样的项目。让它工作。然后添加其余的字段。我称之为“理智检查”,因为试图处理这么多领域 - 会让你发疯:-)

查看控制器中的代码。现在看一下模型中的代码。你看到你有很多代码 - 对于完全相同的值? 控制器应该薄而且专横。控制器老板是这样的:这个形式是否验证真或假?如果为false - 再次显示视图。如果为true:您是否将值添加到数据库表是或否?如果是 - 显示成功页面。

模型可以解决大量数据。模型验证表单数据。该模型创建数据库所需的数据数组。对于拥有这么多数据的东西 - 创建一个只创建数据数组的单独方法。然后你有一个很好的简单数据库调用,如

function _insertProduct( $product ) {
    $this->db->insert( 'tbl_featured_products', $product );
    if ( $this->db->affected_rows() == '1' ) {return TRUE;}
    else {return FALSE;}

} 
看看那个简单漂亮的代码吧。它让我们知道插入是否有效。

现在从你的模型db方法

看一下
if (isset($data['product_id']) && is_array($data['product_id'])) {

不,不,不!和不! 如果您将数据结构传递给数据库调用 - 您已经清理并打磨并先打蜡。您知道它的完美和有效,所以您不必再用一堆混乱来猜测自己这套呢?

终于永远不会说出任何名字:temp 因为临时意味着什么?回答:这并不意味着什么。这就是问题所在。而我在咆哮时不要称它为$ data。 仅为您传递给视图的值保留名称$ data。(好的,我知道一个可能不是一个流行的观点,但我保证它会使您的编码更容易)。方法和变量名称非常重要,它们是您的代码之旅的指南: - )