CodeIgniter Forums
How to insert data from _POST - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How to insert data from _POST (/showthread.php?tid=23156)



How to insert data from _POST - El Forum - 10-02-2009

[eluser]jabga[/eluser]
Hi there.
This is my addnews.php
Code:
<?php

class Addnews extends Controller {

    function addnews()
    {
        parent::Controller();    
    }
    function index ()
    {
        $this->load->view('insertnews');        

    }

This is insertnews.php
Code:
<html>
<body>
<?php $sql="INSERT INTO news (title, content, category)
VALUES
('$_POST[title]','$_POST[content]','$_POST[category]')";
?>
<form action="" method="post">
Title: <input type="text" name="title" />
Content: <input type="text" name="content" />
Category: <input type="text" name="category" />
<input type="submit" />
</form>

</body>
</html>


There are following errors occuring when I click on the submit button
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: title

Filename: views/insertnews.php

Line Number: 7



Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: content

Filename: views/insertnews.php

Line Number: 7


Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: category

Filename: views/insertnews.php

Line Number: 7

How can I add news from insert form easily? Please tell me


How to insert data from _POST - El Forum - 10-02-2009

[eluser]jabga[/eluser]
Should I use helper or class or model ?
I am trying to create a small website for practice.

Here is my html design
http://download.idiotsheep.com/Dulaanurguu Static.zip





ok ok I hAve found it
http://www.phpeveryday.com/articles/CodeIgniter-Form-Creating-Skeleton-P228.html


How to insert data from _POST - El Forum - 10-02-2009

[eluser]n0xie[/eluser]
You shouldn't put your sql in your view. It belongs in your model. Take a look at the CodeIgniter tutorials


How to insert data from _POST - El Forum - 10-02-2009

[eluser]jabga[/eluser]
Tnx, I have got it.
i have finished watching CI tutorial and also this tutorial is very useful
http://www.phpeveryday.com/articles/CodeIgniter-Form-Creating-Skeleton-P228.html
Tnx


How to insert data from _POST - El Forum - 10-02-2009

[eluser]anyshpm[/eluser]
codeigniter tutorial is always helpful


How to insert data from _POST - El Forum - 10-02-2009

[eluser]Zack Kitzmiller[/eluser]
You were missing 's anyway.

$_POST['title']


How to insert data from _POST - El Forum - 10-03-2009

[eluser]richfearless[/eluser]
Try this

Code:
<?php $sql="INSERT INTO news (title, content, category)
VALUES
('".$_POST[title]."','".$_POST[content]."','".$_POST[category]')."";
?>



How to insert data from _POST - El Forum - 10-03-2009

[eluser]InsiteFX[/eluser]
Data access should be done in your model! Like n0xie stated above.

Enjoy
InsiteFX