Below log message appears when i am trying to upload three files using the cck node adding programatically. not sure as to what mistake i am doing below is the code

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'uri': INSERT INTO {file_managed} (filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_2] => 1309100206 ) in drupal_write_record() (line 6851 of /opt/lampstack-3-0-6-0/apps/drupal/htdocs/includes/common.inc).

$validators = array(
		'file_validate_extensions' => array('html htm'),
	);
	$pdfvalidators = array(
		'file_validate_extensions' => array('pdf'),
	);
	$imgvalidators = array(
		'file_validate_extensions' => array('gif jpg jpeg png'),
	);
		/*
		Check to see what fields are getting saved on node save
		$sampleNode = node_load('125');
		echo "<PRE>" . print_r($sampleNode, TRUE) . "</pre>";
		die;*/
    if($file = file_save_upload('table_upload', $validators,'', FILE_EXISTS_RENAME))
    {
		
		$chart_file = file_save_upload('image_upload', $imgvalidators,'public://images', FILE_EXISTS_RENAME);
		$pdf_file = file_save_upload('pdf_upload', $pdfvalidators,'public://pdf', FILE_EXISTS_RENAME);
		$node = new stdClass();
		$node->type = 'event';
		node_object_prepare($node);
		$node->title    = $form_state['values']['title'];
		$node->field_second_line['und'][0]['value'] = $form_state['values']['second_line'];
		$node->testevent['und'][0]['value'] = $form_state['values']['event_number'];
		$node->language = LANGUAGE_NONE;
		$mime = 'application/xhtml+xml';
		$pdf_mime = 'application/pdf';
		$chart_mime = 'image/jpeg';
		$node->testevent['und'][0] = array(
				array(
					'fid' => $file->fid,
					'filename' => $file->filename,
					'uri' => $file->uri,
					'filesize' => $file->filesize,
					'mimetype' => $mime,
					'status' => 1,
				),
			);

		$node->field_pdf['und'][0] = array(
				array(
					'fid' => $pdf_file->fid,
					'filename' => $pdf_file->filename,
					'uri' => $pdf_file->uri,
					'filesize' => $pdf_file->filesize,
					'mimetype' => $pdf_mime,
					'status' => 1,
				),
			);

		$node->field_chart['und'][0] = array(
				array(
					'fid' => $chart_file->fid,
					'filename' => $chart_file->filename,
					'uri' => $chart_file->uri,
					'filesize' => $chart_file->filesize,
					'mimetype' => $chart_mime,
					'status' => 1,
					'data'=>array('alt' => 'Excel alt',
						'title' => 'Excel File',
					),
				),
			);
	  $node->body[$node->language][0]['summary'] = 'this is test';
	  $node->body[$node->language][0]['format']  = 'full_html';
	 
	  $path = 'content/'.$file->filename.'_'. date('YmdHis');
	  $node->path = array('alias' => $path);
	  node_save($node);

Comments

sbanerjee.302’s picture

Check the $path variable in the code. Also, check if you have already defined any url alias for this node type via the drupal admin, 'cause when you are trying to perform a node_save that alias and the alias that you are creating via the code are getting inserted twice which is causing the duplicate entry sql exception. Last but not the least, is there any specific reason you are handling all these via custom code ? 'cause it can also be done quite well via module level configuration and cck.

kenorb’s picture