将输入类型文本中的数据数组保存到数据库

Saving an Array of Data from Input type Text to Database

我们将在 PHP 中使用 CodeIgniter 将文本框的值保存到数据库中。

查看

echo form_open('site/addprice');
foreach($query5 as $row5){
    echo '<input type="text" name="artname"value = "'.$row5->articleName.'" style="background-color: #EDEDED; border: 0px;" readonly/>';
    echo '<input type="text" name="supId" value = "'.$row1->supplierId.'" style="display: none; background-color: #EDEDED; border: 0px;" readonly/>';
    echo '&nbsp';
    echo '&nbsp';
    echo '&nbsp';
    echo '&nbsp';
    echo '&nbsp';
    echo "<input type='text' name='price[]' />";
    echo '<br />';
    echo '<br />';
}
$r++; 
echo '<input type="submit" style="margin-left: 210px;" class="but" placeholder="Enter proposed price" required/>';
echo form_close();
echo '</div>';  

控制器

function addprice(){
    $this->load->model('site_model');
    $this->site_model->addprice();
}

型号

但是,我们的代码还没有完成。

function addprice(){
    $item = $this->input->post('artname');
    $query = $this->db->query("SELECT itemId FROM items WHERE articleName = '$item'");
    $res = $query->result();
    $row = $res[0];
    $supId = $this->input->post('supId');
    $sql = "UPDATE bac_bs SET price='' WHERE supplierId = '$supId' ";
}

我们将不胜感激。

我一直在等待您对我的评论的答复,但您似乎不在。无论如何,我假设您的 $query5 是一个包含数据的多维数组。因此,每个值的键应该足以设置您的数组。这就是我构建您的 view:

的方式
<div>
    <?= form_open('site/addprice') ?>
    <?php foreach ($query5 as $key => $row5) { ?>
        <input type="text" name="bac_bs_info[<?= $key ?>][artname]" value="<?= $row5->articleName ?>" style="background-color: #EDEDED; border: 0px;" readonly/>
        <input type="text" name="bac_bs_info[<?= $key ?>][supId]" value="<?= $row1->supplierId ?>" style="display: none; background-color: #EDEDED; border: 0px;" readonly/>
        &nbsp  &nbsp &nbsp &nbsp &nbsp
        <input type='text' name='bac_bs_info[<?= $key ?>][price]'/>
        <br /><br />
    <?php } ?> 
    <input type="submit" style="margin-left: 210px;" class="but" placeholder="Enter proposed price" required/>';
    <?= form_close() ?>
</div> 

您的 控制器 现在可以接收数组格式的信息,您可以像这样遍历并发送到您的模型:

function addprice() {
    $this->load->model('site_model');
    $bac_bs_info_arr = $this->input->post('bac_bs_info');
    foreach ($bac_bs_info_arr as $data) {
        $this->site_model->model_addprice($data);
    }
}

最后你的模型可以更容易地处理这些信息..虽然我们没有足够的信息来帮助你建立你的模型但是给你一个大概的想法,这是我将如何分解它:

function model_addprice($data) {
    $itemInfo = $this->getItemInfoByName($data['artname']); //dont know your plans for this query
    $this->updateBacbsBySupId($data['supId']);
}

function getItemInfoByName($artName) {
    $this->db->select('*');
    $this->db->from('items');
    $this->db->where('articleName', $artName);
    $query = $this->db->get();
    return $query->row();
}

function updateBacbsBySupId($supId) {
    $data = array(
        'price' => ''//left it blank since you did not specify what information should go here
    );

    $this->db->where('supplierId', $supId);
    $this->db->update('bac_bs', $data);
}