いつもお世話になっております。
codeigniterでフォームから入力した値を取得してDBへ登録したいのですが以下のようなエラー内容がでてしまい、
登録できない状態になっています。
調べてみたところ、配列の中からデータを取り出せていない状態のようで、取り出すやり方をさがしてみましたが、
よくわかりませんでした。
ヒントでもよいので配列から取り出してデータを登録する方法を教えていただけますでしょうか?
ほかにも問題がありましたらご指摘いただけると幸いです。
よろしくお願いいたします。
◆エラー内容 A PHP Error was encountered Severity: Notice Message: Array to string conversion Filename: models/News_model_02.php Line Number: 26 Backtrace: File: C:\xampp\htdocs\CodeIgniter\application\models\News_model_02.php Line: 26 Function: _error_handler File: C:\xampp\htdocs\CodeIgniter\application\controllers\News_02.php Line: 71 Function: insert_answer File: C:\xampp\htdocs\CodeIgniter\index.php Line: 315 Function: require_once A Database Error Occurred Error Number: 1054 Unknown column 'Array' in 'field list' insert into news ( id, title, slug, text ) values ( Array, Array, Array, Array ) Filename: C:/xampp/htdocs/CodeIgniter/system/database/DB_driver.php Line Number: 691
【model】 <?php class news_model_02 extends CI_Model { public function __construct() { $this->load->database(); } public function insert_answer($hikisu) { $sql = " insert into news ( id, title, slug, text ) values ( ".$this->db->escape($hikisu).", ".$this->db->escape($hikisu).", ".$this->db->escape($hikisu).", ".$this->db->escape($hikisu)." ) "; $result = $this->db->query($sql); } }
【controller】 public function create_02 () { $this->load->helper('form'); $this->load->model('create_02'); $data['title'] = 'Create a news item'; $hikisu = $this->input->post(); $this->news_model_02->insert_answer($hikisu); $this->load->view('news/success'); }
【view】 <h2>xxxxx</h2> <h2><?php echo $title; ?></h2> <?php echo form_open('news/create'); ?> <label for="title">Title</label> <input type="input" name="title" /><br /> <label for="text">Text</label> <textarea name="text"></textarea><br /> <input type="submit" name="submit" value="Create news item" /> </form>
- 補足
アドバイスありがとうございます。
以下の形で修正してみたところ、エラーは表示されなくなりましたが、データを入力しても
NULLしかインサートされなくなりました。こういった場合はどのような対処をとればよいですか?
当方、php自体の基本もわかっていない状態なため、この部分の理解ができていないなどありましたらご指摘お願いいたします。
【model】 public function create_02 (){ $this->load->helper('form'); $data['title'] = 'Create a news item'; $this->load->model('News_model_02'); $hikisu = $this->input->post('hikisu'); $this->News_model_02->insert_answer($hikisu); $this->load->view('news/create_02',$data); }
【controller】 public function create_02 (){ $this->load->helper('form'); $data['title'] = 'Create a news item'; $this->load->model('News_model_02'); $hikisu = $this->input->post('hikisu'); $this->News_model_02->insert_answer($hikisu); $this->load->view('news/create_02',$data); }
【view】 <h2>news_02</h2> <h2><?php echo $title; ?></h2> <?php echo validation_errors(); ?> <?php echo form_open('news_02/create_02'); ?> <label for="title">Title</label> <input type="input" name="title" /><br /> <label for="text">Text</label> <textarea name="text"></textarea><br /> <input type="submit" name="submit" value="Create news item" /> </form>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。