你好,我是堆疊溢位的新手,codeigniter 4,對 mysql 沒有足夠的經驗。我第一次插入資料時遇到問題,資料顯示在表上應該是這樣,但是當我第二次插入資料時,我的第一個資料和第二個資料被洗掉了。
這是我的 tablesupp 查詢(我匯出了表,我做它時沒有使用查詢語法)
-- phpMyAdmin SQL Dump
-- version 5.1.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Mar 25, 2022 at 03:12 AM
-- Server version: 10.4.21-MariaDB
-- PHP Version: 7.3.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = " 00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `simstok`
--
-- --------------------------------------------------------
--
-- Table structure for table `tablesupp`
--
CREATE TABLE `tablesupp` (
`idSupp` int(11) NOT NULL,
`namaSupp` varchar(75) NOT NULL,
`alamatSupp` varchar(125) NOT NULL,
`noTelpSupp` varchar(14) NOT NULL,
`Keterangan` varchar(125) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `tablesupp`
--
ALTER TABLE `tablesupp`
ADD PRIMARY KEY (`idSupp`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `tablesupp`
--
ALTER TABLE `tablesupp`
MODIFY `idSupp` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=32;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
我在控制器(供應商)上的插入方法
public function save()
{
$this->supplierModel->save([
'namaSupp' => $this->request->getVar('inputNamaSupp'),
'alamatSupp' => $this->request->getVar('inputAlamatSupp'),
'noTelpSupp' => $this->request->getVar('inputNoTelpSupp'),
'Keterangan' => $this->request->getVar('deskripsi')
]);
return redirect()->to(base_url('supplier'));;
}
我的表格查看供應商
<form action="/saveSupp" method="POST">
<?= csrf_field(); ?>
<div class="form-group">
<label for="inputNamaUnit">Nama Supplier</label>
<input type="text" class="form-control" name="inputNamaSupp" id="inputNamaSupp" aria-describedby="inputNamaSuppHelp">
</div>
<div class="form-group">
<label for="inputNamaUnit">Alamat Supplier</label>
<input type="text" class="form-control" name="inputAlamatSupp" id="inputAlamatSupp" aria-describedby="inputAlamatSupp">
</div>
<div class="form-group">
<label for="inputNamaUnit">No Telepon</label>
<input type="text" class="form-control" name="inputNoTelpSupp" id="inputNoTelpSupp" aria-describedby="inputNoTelpSuppHelp">
</div>
<div class="form-floating">
<label for="floatingTextarea2">Keterangan</label>
<textarea class="form-control" name="deskripsi" placeholder="Deskripsi (optional)" id="floatingTextarea2" style="height: 100px"></textarea>
</div>
<div class="modal-footer">
<button class="btn btn-light" type="button" data-dismiss="modal">Close</button>
<button class="btn btn-primary" type="submit">Save</button>
</div>
</form>
我的模特
<?php
namespace App\Models;
use CodeIgniter\Model;
class supplierModel extends Model
{
protected $table = 'tablesupp';
protected $primaryKey = 'idSupp';
protected $allowedFields = ['namaSupp', 'alamatSupp', 'noTelpSupp', 'Keterangan'];
}
路線
$routes->add('/saveSupp', 'Supplier::save');
提前感謝那些想要幫助我的人。:)
我希望我插入到表中的資料在插入后不會被洗掉
uj5u.com熱心網友回復:
在 save() 方法的狀態下使用 insert() 方法
在控制器中:
$myModel = new supplierModel();
$data = [
'namaSupp' => $this->request->getVar('inputNamaSupp'),
'alamatSupp' => $this->request->getVar('inputAlamatSupp'),
'noTelpSupp' => $this->request->getVar('inputNoTelpSupp'),
'Keterangan' => $this->request->getVar('deskripsi')
]
$myModel->insert($data)
如果上述方法沒有做太多,請在您的資料庫和模型中添加這 3 個欄位('created_at'、'updated_at'、'deleted_at'),例如:
在模型中
protected $table = 'tablesupp';
protected $primaryKey = 'idSupp';
protected $allowedFields = ['namaSupp', 'alamatSupp', 'noTelpSupp',
'Keterangan'];
protected $useAutoIncrement = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
created_at 必須是curent_timestamp,updated_at 和deleted_at 可以是Text
uj5u.com熱心網友回復:
我已經自己解決了這個問題。對不起,伙計們,我在查看檔案時犯了一個愚蠢的錯誤,我忘記向您展示洗掉按鈕代碼,我使用了使用標簽實作它的 http 方法欺騙然后我忘記關閉標簽。所以代碼就像這樣:
<form>
<button>DELETE</button>
<form>
INPUT DATA FORM
</form>
結論是,當我點擊保存按鈕時,它會自動執行洗掉按鈕,因為它在同一個標??簽中
謝謝那些想要幫助我的人。(對不起我的英語,它真的很糟糕:D)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/453573.html
標籤:mysql 加入 codeigniter-4
