0

I have an error from Codeigniter which is :

"A PHP Error was encountered

Severity: Notice

Message: Undefined index: domain" it's not the only one ALL the data that I want to access with the id return the same error but with its own id...

I try to take the values of a form (with a POST) and put them in a database. However I can't access to the values because of this. I just tried with hardcode values and I was able to record it in the database.

Here my code for the view it's a form for selling book:

<form method="post" action="<?php echo 'http://localhost:8888/index.php/sell/addBook/'?>">


        <label for="title">Title</label>
        <input type="text" class="form-control" value="" id="title" placeholder="Title">


        <label for="author">Author</label>
        <input type="text" class="form-control" value="" id="author" placeholder="Author">


        <label for="subject">Subject</label>
        <input type="text" class="form-control" value="" id="subject" placeholder="Subject">


        <label for="price">Price</label>
        <input type="float" class="form-control" value="" id="price" placeholder="Price">


        <label for="code">Course Code</label>
        <input type="text" class="form-control" value="" id="coursecode" placeholder="Course Code">


        <label for="domain">Please select a Domain</label>
        <select id="domain" class="form-control">
            <?php

            $domain = $this->db->get('Domain');
            foreach ($domain->result() as $row){
                echo " <option value=\"".$row->name."\">";

                echo $row->name;
                echo "</option>";
            }?>
        </select>

        <label for="coursename">Course Name</label>
        <select id="coursename" class="form-control">
            <?php

            $domain = $this->db->get('Course');
            foreach ($domain->result() as $row){
                echo " <option value=\"".$row->name."\">";
                echo $row->name;
                echo "</option>";
            }?>
        </select>

        <label for="university">University</label>
            <select id="university" class="form-control">
                <?php

                $domain = $this->db->get('Course');
                foreach ($domain->result() as $row){
                    echo " <option value=\"".$row->college."\">";
                    echo $row->college;
                    echo "</option>";
                }?>
            </select>

        <label for = "selectCondition">Select the Condition of the Book</label> <button type="button" class="btn btn-default btn-xs">
        <span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
        </button>

        <select id="condition" class="form-control">
            <option value="mint">Mint</option>
            <option value="very good">Very Good</option>
            <option value="good">Good</option>
            <option value="fair">Fair</option>
            <option value="poor" selected="true">Poor</option>
        </select>

        <br>
        <label for="exampleInputFile">Upload Photo</label>
        <input type="file" id="exampleInputFile">
        <p class="help-block">Upload photos to assure the buyer your book is in good condition</p>



    <button type="submit" class="btn btn-default">Submit</button>

</form>
and here the code of my controller

<?php

class sell extends CI_Controller
{

    public $user = "";

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->database();

    }

    // Store user information and send to profile page
    public function index()
    {

        $this->load->view('sell');

    }

    public function addBook()
    {

        $query1 = "SELECT id FROM Domain WHERE name=" . $_POST['domain']. ";";
        /*$this->db->select('id');
        $this->db->from('Domain');
        $this->db->where('name =', $_POST['domain'));*/


        $domid = $this->db->query($query1);


        $query2 = "SELECT id FROM Course WHERE name=" . $_POST['coursename'] . " AND college=" + $_POST['university'] . ";";


        $course = $this->db->query($query2);




        $this->db->set('title', $_POST['title']);
        $this->db->set('author', $_POST['author']);
        $this->db->set('price', intval($_POST['price']));
        $this->db->set('subject', $_POST['subject']);
        $this->db->set('coursecode', $_POST['coursecode']);
        $this->db->set('cond', $_POST['condition']);
        $this->db->set('domainid', intval($domid));
        $this->db->set('courseid', intval($course));
        $this->db->set('sellerid', $_SESSION['id']);
      

        $this->db->insert('Book');
      
    }


}

?>

Thank you for the help !

moli
  • 3
  • 1
  • 2

1 Answers1

1

You just need to add name attribute for domain field as:

<select id="domain" class="form-control">

Should be:

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

One more thing if domain field is getting string value than you need to use quotes around domain field in your SELECT Statement as:

$query1 = "SELECT id FROM Domain WHERE name='".$_POST['domain']."'";
devpro
  • 16,184
  • 3
  • 27
  • 38