We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to save custom field information into database

Hi, I am beginner I want to save information into database table using phalcon. I am reading csv file and saving information into database but not able to save gettting errors.

Code:
  //echo '<pre>'; print_r($_FILES); echo '</pre>';//exit;
  $lines = new CsvReader($_FILES['file']['tmp_name']);
  echo "<table>\n";
  $csv_data = array();
  foreach ($lines as $line_number => $values) {
  echo '<tr>';
  echo '<td>'.$line_number.'</td>';
  foreach ($values as $value) {
  echo '<td>'.$value."</td>";
  $csv_data = $value;
  echo '<br/>';
  }
  echo "</tr>\n";
  }
  echo '</table>';
  $data   = preg_split('/\s+/', $csv_data);
  $company = new Company();    
  $company->company_name = $data[0];
  $company->url = $data[1];
  $company->visits_per_month = $data[2];
  $company->alexa = $data[3];
  $company->quantcast = $data[4];
  $company->country = $data[5];
  $company->email = $data[6];

  if ($company->save() == false) {
  echo "OOPS ERROR.....??????: ";
  foreach ($company->getMessages() as $message) {
  echo $message;
  }
  } else {
  echo "Great!!!";
  }

  return $this->dispatcher->forward(array(
  "controller" => "company",
  "action" => "uplad_csv"
  ));

not working

edited Aug '14

THe Error-Message from the database is also important, maybe the primary key(company-name???) isn't unique, some data has got the wrong type(string but int is required).....there are so many things that could happen....



27.6k
edited Aug '14
OOPS ERROR.....??????: domain is requiredphone is requiredaddress is requiredcity is requiredstate is requiredzip is requiredindustry is requiredhash_of_product is requiredflag is requiredcreated_at is requiredupdated_at is requiredaddress1 is requiredrevenue is requiredrev_range is requiredemp is requiredemp_range is requiredtech_id is required


27.6k
edited Aug '14

Hello Sir, Company

(`id`, `url`, `domain`, `company_name`, `visits_per_month`, `alexa`, `quantcast`, `country`, `email`, `phone`, `address`, `city`, `state`, `zip`, `industry`, *`hash_of_product`, `flag`, `created_at`, `updated_at`, `address1`, `revenue`, `rev_range`, `emp`, `emp_range`, `tech_id`)

Above is my company table I m reading one csv and trying to save only company name,alexa,quantacast,country,email these custom fields of table.


/*Code snippets*/
$company = new Company();    
$company->company_name = Rakesh;
$company->email = [email protected];
$company->save();

Hi sir i'm not getting exactly what is the issue?

There are many fields you don't fill with informations e.g. domain, zip etc.....



27.6k

Yes I want only 6 fields to save its information into database others are not needed.

edited Aug '14
$company->company_name = "Rakesh";
$company->email = "[email protected]";
$company->save(); 

You need "" for strings!!! and you have to fill all fields which are required or you must able NULL for the fields in the table on the database which you won't fill!



27.6k
edited Aug '14
Array
(
    [0] => Betterlabs
    [1] => www.betterlabs.net
    [2] => 10
    [3] => 211
    [4] => 454
    [5] => India
    [6] => [email protected]
)

Above is my array

        $company = new Company();    
        $company->company_name = $data[0];
        $company->url = $data[1];
        $company->visits_per_month = $data[2];
        $company->alexa = $data[3];
        $company->quantcast = $data[4];
        $company->country = $data[5];
        $company->email = $data[6];
        $company->save();

Above is my scenario. According to you I have toexplictly mention sother fields as Null am I right?

Like $company->domain = NULL; etc...

No you are not right!

This is no php-code, you must set this on the database(DB), which DB do you use?



27.6k
edited Aug '14
return new \Phalcon\Config(array(
    'database' => array(
        'adapter'     => 'Mysql',
        'host'        => 'localhost',
        'username'    => 'root',
        'password'    => 'better',
        'dbname'      => 'capturev1',
    ),
    'application' => array(
        'controllersDir' => __DIR__ . '/../../app/controllers/',
        'modelsDir'      => __DIR__ . '/../../app/models/',
        'viewsDir'       => __DIR__ . '/../../app/views/',
        'pluginsDir'     => __DIR__ . '/../../app/plugins/',
        'libraryDir'     => __DIR__ . '/../../app/library/',
        'cacheDir'       => __DIR__ . '/../../app/cache/',
        'baseUri'        => '/capturev1/',
    )
));

Above is config. Mysql Adapter

Then you should browse to this link

localhost/phpmyadmin/

there you can set your table-fields to nullable!



27.6k

Sir How to save into database in phalcon this is my question I am beginner.



27.6k

Still Sir I am not able to resolve. I want to read csv file which is upladed and save its information into database

edited Aug '14

Who create the table on the database?

You are not able to save only some coloums of a row, when all coulums are required, which your error-message told you!

When youir table looks like this

ID | company-name |  company-email
-----------------------------------------------
1  |                          |     [email protected]

And the company-name is required, than you are not able to save the data, alright?



27.6k
edited Aug '14

Yes Sir U r right Thr Scaffold I have generated Company table.

who create the table on mysql???



27.6k
edited Aug '14
CREATE TABLE IF NOT EXISTS `company` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
  `url` varchar(255) NOT NULL,
  `domain` varchar(255) NOT NULL COMMENT ' ',
  `company_name` varchar(255) NOT NULL,
  `visits_per_month` text NOT NULL,
  `alexa` int(11) NOT NULL,
  `quantcast` int(11) NOT NULL,
  `country` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  `address` text NOT NULL,
  `city` varchar(255) NOT NULL,
  `state` varchar(255) NOT NULL,
  `zip` varchar(255) NOT NULL,
  `industry` varchar(255) NOT NULL,
  `hash_of_product` int(25) NOT NULL,
  `flag` tinyint(4) NOT NULL COMMENT 'tb_flag,dd_flag,manual_cleansed_flag,phone_validation_flag,netsuite_flag,quickbook_flag,not_application_flag',
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  `address1` text NOT NULL,
  `revenue` text NOT NULL,
  `rev_range` text NOT NULL,
  `emp` text NOT NULL,
  `emp_range` text NOT NULL,
  `tech_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `companyForeignKey1` (`tech_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

I have manually fired above in phpmyadmin

edited Aug '14

change every row which you won't fill with data like

domain varchar(255) NOT NULL COMMENT ' '

to

domain varchar(255) NULL COMMENT ' '

Every coloumn like "domain" where you won't fill data must be able so save NULL, so you must delete the "NOT" infront of "NULL" like in my example!

and then create the table with the new statement, befor you must delete the wrong table with

Drop table company;


27.6k

Sir I have done edited table structure

and which error message appears now?



27.6k
edited Aug '14
OOPS ERROR.....??????: Value of field 'email' must have a valid e-mail formatSQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`capturev1`.`company`, CONSTRAINT `companyForeignKey1` FOREIGN KEY (`tech_id`) REFERENCES `technology` (`id`))


27.6k

At least provide some code snippets where csv files are parsed and its information saved into database.

your phalcon-code(php-code) is correct, you should learn the basics of database-design, what is a relationship ans so on....

The errormessage mean, you must put a techid(int value) inside which is also available in the table technology in the coloumn id!

greez



27.6k
edited Aug '14

Ok Sir, Thanks, Can you provide me some links where database related operations are done such as file reading and saving its contents into database etc.

take a look on this link:

https://www.analysisandsolutions.com/code/mysql-tutorial.htm

or search "mysql basics", "database basics" or "database design" with your searchside like duckduckgo.com