Welcome Guest, Not a member yet? Register   Sign In
please hepl me.. about database..
#1

[eluser]fyzza[/eluser]
i have problem for connecting to database.. i just make simple form... but i was try 3 weeks to connect them.. can you help me? what is the problem?


model

<?php
class Welcome_model extends Model {

function Welcome_model()
{
parent::Model();
}

function addclient($client_data)
{
if ($this->db->insert('feedback', $client_data))
{
return TRUE;
}
else
{
return FALSE;
}
}
}

?>


view

<?php
$attributes = array ('class'=>'welcome_message', 'id'=>'feedback');
echo form_open('welcome_message', $attributes);?>
<table>
<tr>
<td valign="top"> Title: </td>
<td>&lt;input type="text" name="title" id="title" size="40" maxlength="30" /&gt;&lt;/td>
</tr>
<tr>
<td valign="top"> Name: </td>
<td>&lt;input type="text" name="name" id="name" size="40" maxlength="30" /&gt;&lt;/td>
</tr>
<tr>
<td valign="top"> Comments: </td>
<td>&lt;input type name="comments" id="comments" size="40" maxlength="30" /&gt;&lt;/td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&lt;input type="reset" name="reset" id="reset" value="Reset" /&gt;&lt;input type="submit" name="feedback" id="feedback" value="Submit" /></td>
</tr>
</table>
&lt;/form&gt;
<br/>


controller

&lt;?php

class Welcome extends Controller {

function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->view('welcome_message');
$this->load->model('welcome_model');
$this->load->helper('file','form','url');

function addclient($client_data)
{
$client_data = array(
'client_id' => $this->input->post('client_id'),
'title' => $this->input->post('title'),
'name' => $this->input->post('name'),
'comments' => $this->input->post('comments'),
);
$this->db->insert('feedback', $client_data);
}

}
}
?&gt;



1)$autoload['libraries'] = array('database');

2)Database

$active_group = "default";

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "feedback";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['active_r'] = TRUE;
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

?&gt;

3)$config['base_url'] = "http://localhost/testing/";


I hope you all can help me because i am a beginner.. i love to learn it.. thank you..
#2

[eluser]Jelmer[/eluser]
First off, a couple of tips about using the forums:
- use the [code ] tag around your code snippets, this makes them a lot more readable.
- give the error you are receiving, most users won't completely re-enact your problem on their own server but can (from experience) deduce what causes the error

You say you have a problem connecting to the database, are you actually receiving an error that says so? Because when I look at your controller I see a couple of other problems:
- In your index() you load your view before loading helpers & model, so the helpers and model won't be available when your view is generated (that would cause problems)
- The same index() function isn't closed by a }, though there's an extra } after the addclient() function which would probably crash your controller as the index() function encapsulates your addclient() function
- This isn't the real problem, but it is somewhat bad form: you have a model written for DB insertion but you're putting it straight into the DB from your controller's addclient() function

Your controller might work better if you'd rewrite it to the following (I've added comments where I've edited code):
EDIT 20/2: correction to the model loading.
Code:
&lt;?php

class Welcome extends Controller {

  function Welcome()
  {
      parent::Controller();
  }
  function index()
  {
      // removed the model as you don't use it in your view
      $this->load->helper(‘file’,‘form’,‘url’);
      $this->load->view(‘welcome_message’);
  }

  function addclient($client_data)
  {
      $this->load->model(‘Welcome_model’);
      $client_data = array(
           ‘client_id’ => $this->input->post(‘client_id’),
           ‘title’ => $this->input->post(‘title’),
           ‘name’ => $this->input->post(‘name’),
           ‘comments’ => $this->input->post(‘comments’),
      );
      $this->Welcome_model->addclient($client_data);
   }

// Removed the extra }

}

// Removed the PHP closing tag, not neccessary
#3

[eluser]xwero[/eluser]
Instead of loading the database in the autoload file you can try
Code:
$this->load->model(‘welcome_model’,'',true);
The third parameter connects to the database which eliminates the need to autoload the database library.
#4

[eluser]fyzza[/eluser]
[quote author="Jelmer" date="1235064552"]First off, a couple of tips about using the forums:
- use the [code ] tag around your code snippets, this makes them a lot more readable.
- give the error you are receiving, most users won't completely re-enact your problem on their own server but can (from experience) deduce what causes the error

You say you have a problem connecting to the database, are you actually receiving an error that says so? Because when I look at your controller I see a couple of other problems:
- In your index() you load your view before loading helpers & model, so the helpers and model won't be available when your view is generated (that would cause problems)
- The same index() function isn't closed by a }, though there's an extra } after the addclient() function which would probably crash your controller as the index() function encapsulates your addclient() function
- This isn't the real problem, but it is somewhat bad form: you have a model written for DB insertion but you're putting it straight into the DB from your controller's addclient() function

Your controller might work better if you'd rewrite it to the following (I've added comments where I've edited code):
Code:
&lt;?php

class Welcome extends Controller {

  function Welcome()
  {
      parent::Controller();
  }
  function index()
  {
      // removed the model as you don't use it in your view
      $this->load->helper(‘file’,‘form’,‘url’);
      $this->load->view(‘welcome_message’);
  }

  function addclient($client_data)
  {
      $this->load->model(‘welcome_model’);
      $client_data = array(
           ‘client_id’ => $this->input->post(‘client_id’),
           ‘title’ => $this->input->post(‘title’),
           ‘name’ => $this->input->post(‘name’),
           ‘comments’ => $this->input->post(‘comments’),
      );
      $this->welcome_model->addclient($client_data);
   }

// Removed the extra }

}

// Removed the PHP closing tag, not neccessary
[/quote]

I was correct the problem.. my form can function but database still have problem.. the data still cannot insert into database.. What i need to do? please help me.. Thank you..
#5

[eluser]Relexx[/eluser]
the inclusion of the model is incorrect I think
Code:
$this->load->model(‘welcome_model’);
should be
Code:
$this->load->model(‘Welcome_model’);
refer to the Model documentation
#6

[eluser]Jelmer[/eluser]
@ fyzza
Quote:I was correct the problem..
I never said you gave the wrong problem, I only mentioned I did see a couple of other problems with your code.

Quote:my form can function
That probably means you had already loaded the form helper from your autoload, or the code you gave wasn't the real code you're using.

Quote:but database still have problem.. the data still cannot insert into database.. What i need to do? please help me.. Thank you..
I still need the full error message you're getting, as far as I can see this should work fine. If you only say that inserting goes wrong that could have many causes, among which:
- You haven't configured your database variables correctly (not likely though, as that would crash your form as well since you autoload the DB)
- One of the array keys from your $client_data variable doesn't match a field name
- The tablename is incorrect
- The database isn't loaded correctly
- Some of the input isn't escaped correctly
- You aren't inserting a field that doesn't accept NULL

The only way any of us could help you figure out the problem is when you copy/paste the error message in full, there's no other way any of us could know what's going wrong - especially if the code you're posting isn't the actual code you're using.

By the way, if you aren't getting an error message and only a blank screen: try changing the 12th line of the index.php file in your webroot to:
Code:
error_reporting(E_ALL);
And if that doesn't do it, you could add:
Code:
ini_set('display_errors', 'On');

@ relexx
You're right, my mistake. I've updated my previous post to the correct upper/lowercase.
#7

[eluser]Jelmer[/eluser]
@ fyzza
I just noticed that you've already posted this question once before and got pretty much the same answer as I gave you: post the actual error message.

In the end though, you did in the other topic: 404. Which is in no way, and would in no way be caused by the database if your controller, view & model are like you posted them. This made me take a second look at your view and I finally noticed the problem (which I might have before if you had given the error message).

In your view it says:
Code:
echo form_open(‘welcome_message’, $attributes);
That's incorrect, the URI to which your form is posted should be in the form of
Quote:controller/function[/args]
So it should be:
Code:
echo form_open(‘welcome/addclient’, $attributes);

For the future:
- Don't double post a question, that's considered SPAM
- If people tell you the error couldn't be caused by something (like a 404 by the DB), consider that they might have more experience.
- When you ask for help solving an error in your programming: give the error message. Especially after that's been pointed out by more than one person trying to help you.
#8

[eluser]fyzza[/eluser]
[quote author="Jelmer" date="1235158133"]@ fyzza
I just noticed that you've already posted this question once before and got pretty much the same answer as I gave you: post the actual error message.

In the end though, you did in the other topic: 404. Which is in no way, and would in no way be caused by the database if your controller, view & model are like you posted them. This made me take a second look at your view and I finally noticed the problem (which I might have before if you had given the error message).

In your view it says:
Code:
echo form_open(‘welcome_message’, $attributes);
That's incorrect, the URI to which your form is posted should be in the form of
Quote:controller/function[/args]
So it should be:
Code:
echo form_open(‘welcome/addclient’, $attributes);

For the future:
- Don't double post a question, that's considered SPAM
- If people tell you the error couldn't be caused by something (like a 404 by the DB), consider that they might have more experience.
- When you ask for help solving an error in your programming: give the error message. Especially after that's been pointed out by more than one person trying to help you.[/quote]

i am sorry, there are not have any error message... that's why i not put the error message here, just when i submit, the data not insert in database..
#9

[eluser]Relexx[/eluser]
[quote author="Relexx" date="1235130083"]the inclusion of the model is incorrect I think
Code:
$this->load->model(‘welcome_model’);
should be
Code:
$this->load->model(‘Welcome_model’);
refer to the Model documentation[/quote]
[quote author="Jelmer" date="1235151100"]@ relexx
You're right, my mistake. I've updated my previous post to the correct upper/lowercase.[/quote]

@Jelmer: Yes but it is in the original code as well, not just yours.
#10

[eluser]Jelmer[/eluser]
[quote author="fyzza" date="1235462605"]i am sorry, there are not have any error message... that's why i not put the error message here, just when i submit, the data not insert in database..[/quote]
Fyzza, another good thing to do when people take the time to explain things in detail is to actually read what they write. Others and I have pointed out multiple problems with your code by now and I've explained how to get error messages when you get just a blank screen.
At some point people who have taken the time to point those out would like to know you actually read them and if you have tried if the solutions solve your problem.

So when I've taken the time to explain in detail what could have gone wrong and you reply with only a little more than nothing I can only come to the conclusion that it's not that important to you and that I've wasted my time trying to explain things to you.




Theme © iAndrew 2016 - Forum software by © MyBB