CI4 从选定的字符串中插入 ID

CI4 Inserting ID from selected Strings

我已经厌倦了思考如何解决这个案子。已经通过互联网搜索并没有解决。 开门见山

这是我的 Product.php

控制器
<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\Model_product;
use App\Models\Model_category;
use App\Models\Model_status;
use App\Models\Model_supplier;

class Product extends Controller
{
    protected $helpers = [];

    public function __construct()
    {
        helper(['form']);
        $this->model_category = new Model_category();
        $this->model_product = new Model_product();
        $this->model_status = new Model_status();
        $this->model_supplier = new Model_supplier();
    }

    public function index()
    {
        $data['tbproduct'] = $this->model_product->getProduct();
        return view('product/index', $data);
    }

    public function create()
    {
        $all = [
            'tbproduct' => $this->model_product->getProduct(),
            'tbcategory' => $this->model_category->getCategory(),
            'tbstatus' => $this->model_status->getStatus(),
            'tbsupplier' => $this->model_supplier->getSupplier()
        ];
        return view('product/create', $all);
    }

    public function store()
    {
        $validation =  \Config\Services::validation();

        $data = array(
            'prod_name'     => $this->request->getPost('prod_name'),
            'prodcat_id'    => $this->request->getGet('cat_id'),
            'prod_serial'   => $this->request->getPost('prod_serial'),
            'prod_barcode'  => $this->request->getPost('prod_barcode'),
            'prodstat_id'   => $this->request->getPost('stat_id'),
            'prodsup_id'    => $this->request->getPost('sup_id'),
            'prod_image'    => $this->request->getPost('prod_image')
        );

        if ($validation->run($data, 'product') == FALSE) {
            session()->setFlashdata('inputs', $this->request->getPost());
            session()->setFlashdata('errors', $validation->getErrors());
            return redirect()->to(base_url('product/create'));
        } else {
            $model = new Model_product();
            $simpan = $model->insertProduct($data);
            if ($simpan) {
                session()->setFlashdata('Success', 'Product has been created');
                return redirect()->to(base_url('product'));
            }
        }
    }
}

这是我的模特Model_product.php

<?php

namespace App\Models;

use CodeIgniter\Model;

class Model_product extends Model
{
    protected $table = 'tbproduct';

    public function getProduct($id = false)
    {
        if ($id === false) {
            return $this->table('tbproducts')
                ->join('tbcategory', 'tbproduct.prodcat_id = tbcategory.cat_id', 'left')
                ->join('tbstatus', 'tbproduct.prodstat_id = tbstatus.stat_id', 'left')
                ->join('tbsupplier', 'tbproduct.prodsup_id = tbsupplier.sup_id', 'left')
                ->get()
                ->getResultArray();
        } else {
            return $this->table('tbproducts')
                ->join('tbcategory', 'tbproduct.prodcat_id = tbcategory.cat_id', 'left')
                ->join('tbstatus', 'tbproduct.prodstat_id = tbstatus.stat_id', 'left')
                ->join('tbsupplier', 'tbproduct.prodsup_id = tbsupplier.sup_id', 'left')
                ->where('tbproduct.prod_id', $id)
                ->get()
                ->getRowArray();
        }
    }

    public function insertProduct($data)
    {
        return $this->db->table($this->table)->insert($data);
    }

    public function updateProduct($data, $id)
    {
        return $this->db->table($this->table)->update($data, ['prod_id' => $id]);
    }

    public function deleteProduct($id)
    {
        return $this->db->table($this->table)->delete(['prod_id' => $id]);
    }
}

这是我的观点create.php

<?php echo view('_partials/header'); ?>
<?php echo view('_partials/sidebar'); ?>

<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0 text-dark">Create New Product</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="/">Home</a></li>
                        <li class="breadcrumb-item active">Create New Product</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <form action="<?php echo base_url('product/store'); ?>" method="post">
                        <div class="card">
                            <div class="card-body">
                                <?php
                                $inputs = session()->getFlashdata('inputs');
                                //$inputs_cat = isset($inputs['cat_id']) == null ? '' : $inputs['cat_id'];
                                $errors = session()->getFlashdata('errors');
                                if (!empty($errors)) { ?>
                                    <div class="alert alert-danger" role="alert">
                                        Whoops! There is something wrong, that is:
                                        <ul>
                                            <?php foreach ($errors as $error) : ?>
                                                <li><?= esc($error) ?></li>
                                            <?php endforeach ?>
                                        </ul>
                                    </div>
                                <?php } ?>

                                <div class="form-group">
                                    <label for="">Product Name</label>
                                    <input type="text" class="form-control" name="prod_name" placeholder="Enter product name" value="<?php echo isset($inputs['prod_name']); ?>">
                                </div>
                                <div class="form-row">
                                    <div class="col mb-3">
                                        <label for="">Category</label>
                                        <select class="form-control" name="cat_name" id="cat_name">
                                            <?php foreach ($tbcategory as $row) {
                                                echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
                                            } ?>
                                        </select>
                                    </div>
                                    <div class="col mb-3">
                                        <label for="">Serial Number</label>
                                        <input type="text" class="form-control" name="prod_serial" placeholder="Enter product serial number" value="<?php echo isset($inputs['prod_serial']); ?>">
                                    </div>
                                    <div class="col mb-3">
                                        <label for="">Barcode</label>
                                        <input type="text" class="form-control" name="prod_barcode" placeholder="Enter product barcode" value="<?php echo isset($inputs['prod_barcode']); ?>">
                                    </div>
                                </div>
                                <div class="form-row">
                                    <div class="col mb-3">
                                        <label for="">Status</label>
                                        <select class="form-control" name="stat_name" id="stat_name">
                                            <?php foreach ($tbstatus as $row) {
                                                echo '<option value="' . $row['stat_id'] . '">' . $row['stat_name'] . '</option>';
                                            } ?>
                                        </select>
                                    </div>
                                    <div class="col mb-3">
                                        <label for="">Supplier</label>
                                        <select class="form-control" name="sup_name" id="sup_name">
                                            <?php foreach ($tbsupplier as $row) {
                                                echo '<option value="' . $row['sup_id'] . '">' . $row['sup_name'] . '</option>';
                                            } ?>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="card-footer">
                                <a href="<?php echo base_url('product'); ?>" class="btn btn-outline-info">Back</a>
                                <button type="submit" class="btn btn-primary float-right">Save</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<?php echo view('_partials/footer'); ?>

我想从 Controller Product 功能存储中插入新数据。 我有这个问题,我无法将字符串值更改为整数值

$data = array(
    'prod_name'     => $this->request->getPost('prod_name'),
    'prodcat_id'    => $this->request->getGet('cat_id'), ---->>> this is the problem
    'prod_serial'   => $this->request->getPost('prod_serial'),
    'prod_barcode'  => $this->request->getPost('prod_barcode'),
    'prodstat_id'   => $this->request->getPost('stat_id'), ---->>> and this
    'prodsup_id'    => $this->request->getPost('sup_id'), ---->>> and also this
    'prod_image'    => $this->request->getPost('prod_image')
);

如果您在 create.php 视图中看到,所有三个表都有 $row['***_id']。 我想将这三个 ID 存入商店。

这是显示的错误:

mysqli_sql_exception #1048
Column 'prodcat_id' cannot be null

您的问题是您发送的是 cat_name 字段而不是表单中的 cat_id 字段。修复字段的名称并将起作用。

以下将有效

<?php

$data = array (
    'prod_name'    => $this->request->getPost('prod_name'),
    'prodcat_id'   => $this->request->getPost('cat_name'), // ---->>> name fixed **
    'prod_serial'  => $this->request->getPost('prod_serial'),
    'prod_barcode' => $this->request->getPost('prod_barcode'),
    'prodstat_id'  => $this->request->getPost('stat_name'), // ---->>> and this **
    'prodsup_id'   => $this->request->getPost('sup_name'), // ---->>> and also this**
    'prod_image'   => $this->request->getPost('prod_image'),
);

更新: 由于这看起来很简单,但实际上这可能是骗人的,我建议接下来的步骤来解决它:

  1. 确保表单字段名称与 php 脚本中使用的名称相匹配
  2. 确保您正在向服务器发送数据。您可以检查超级全局变量。 print_r($_POST) 这样的东西就足够了。
  3. 确保从正确的来源收集数据:get 或 post

在这种情况下,错误来自 mysql。要缓解这种情况,您可以:

  1. 如果需要,在列上设置默认值
  2. 有条件地填充 $data 数组。如果没有 cat_id,请不要放置没有值(或 null)
  3. 的索引 'cat_id'

谢谢你之前帮助过我。 我找到了答案。 这是我可以获得 cat_id、stat_id 和 sup_id.

的正确代码

在视图中 create.php 更改此代码

<select class="form-control" name="cat_name" id="cat_name">

进入这个

<select class="form-control" name="cat_id" id="cat_id">

这意味着您将从数据库中获取 cat_id 值,而不是 cat_name。 将 stat_name 更改为 stat_id,然后将 sup_name 更改为 sup_id。

当所有这些都改变后,您可以在从下拉框中选择值时插入具有正确 ID 号的数据库。

这就是我的问题的答案,谢谢你的帮助。