Zum Inhalt springen

Formular schreibt in Datenbank mehrfach leere Einträge

Erstellt am 6. August 2007 · 4 Antworten · letzte Antwort am 7. August 2007

Tags: Datenbank, Frage

RT-Schwabe ·

Hallo zusammen,

ich habe mir anhand dieses Tutorials http://www.mcuniverse.com/Editing-PHP-File.169.0.html ein Formular gebaut, dass Daten in eine Tabelle schreibt. Das klappt soweit auch wunderbar.

Allerdings wird jedesmal wenn ich die Seite lade ein leerer Eintrag in die Datenbank geschrieben (alle Felder ohne Ihnalt einfach übernommen).

Hier ist der Code:

<?php

require_once(PATH_tslib.'class.tslib_pibase.php');

class tx_admreport_pi1 extends tslib_pibase {
	var $prefixId = 'tx_admreport_pi1';		// Same as class name
	var $scriptRelPath = 'pi1/class.tx_admreport_pi1.php';	// Path to this script relative to the extension dir.
	var $extKey = 'adm_report';	// The extension key.
	var $pi_checkCHash = TRUE;
	
	/**
	 * Main method of your PlugIn
	 *
	 * @param	string		$content: The content of the PlugIn
	 * @param	array		$conf: The PlugIn Configuration
	 * @return	The content that should be displayed on the website
	 */
	function main($content,$conf)	{
		switch((string)$conf['CMD'])	{
			case 'singleView':
				list($t) = explode(':',$this->cObj->currentRecord);
				$this->internal['currentTable']=$t;
				$this->internal['currentRow']=$this->cObj->data;
				return $this->pi_wrapInBaseClass($this->singleView($content,$conf));
			break;
			default:
				if (strstr($this->cObj->currentRecord,'tt_content'))	{
					$conf['pidList'] = $this->cObj->data['pages'];
					$conf['recursive'] = $this->cObj->data['recursive'];
				}
				return $this->pi_wrapInBaseClass($this->listView($content,$conf));
			break;
		}
	}
	
	/**
	 * Shows a list of database entries
	 *
	 * @param	string		$content: content of the PlugIn
	 * @param	array		$conf: PlugIn Configuration
	 * @return	HTML list of table entries
	 */
	function listView($content,$conf)	{
		$this->conf=$conf;		// Setting the TypoScript passed to this function in $this->conf
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();		// Loading the LOCAL_LANG values
		
		$lConf = $this->conf['listView.'];	// Local settings for the listView function
	
		if ($this->piVars['showUid'])	{	// If a single element should be displayed:
			$this->internal['currentTable'] = 'tx_admreport_adm';
			$this->internal['currentRow'] = $this->pi_getRecord('tx_admreport_adm',$this->piVars['showUid']);
	
			$content = $this->singleView($content,$conf);
			return $content;
		} else {
			$items=array(
				'1'=> $this->pi_getLL('list_mode_1','Mode 1'),
				'2'=> $this->pi_getLL('list_mode_2','Mode 2'),
				'3'=> $this->pi_getLL('list_mode_3','Mode 3'),
			);
			if (!isset($this->piVars['pointer']))	$this->piVars['pointer']=0;
			if (!isset($this->piVars['mode']))	$this->piVars['mode']=1;
	
				// Initializing the query parameters:
			list($this->internal['orderBy'],$this->internal['descFlag']) = explode(':',$this->piVars['sort']);
			$this->internal['results_at_a_time']=t3lib_div::intInRange($lConf['results_at_a_time'],0,1000,50);		// Number of results to show in a listing.
			$this->internal['maxPages']=t3lib_div::intInRange($lConf['maxPages'],0,1000,2);;		// The maximum number of "pages" in the browse-box: "Page 1", "Page 2", etc.
			$this->internal['searchFieldList']='bericht,kundennummer,kundenname,plz,ort';
			$this->internal['orderByList']='uid,kundennummer,kundenname,plz,ort';
	
				// Get number of records:
			$res = $this->pi_exec_query('tx_admreport_adm',1);
			list($this->internal['res_count']) = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
	
				// Make listing query, pass query to SQL database:
			$res = $this->pi_exec_query('tx_admreport_adm');
			$this->internal['currentTable'] = 'tx_admreport_adm';
	
				// Put the whole list together:
			$fullTable='';	// Clear var;
		#	$fullTable.=t3lib_div::view_array($this->piVars);	// DEBUG: Output the content of $this->piVars for debug purposes. REMEMBER to comment out the IP-lock in the debug() function in t3lib/config_default.php if nothing happens when you un-comment this line!
	
				// Adds the mode selector.
			// $fullTable.=$this->pi_list_modeSelector($items);
	
				// Adds the whole list table
			$fullTable.=$this->pi_list_makelist($res);
	
				// Adds the search box:
			$fullTable.=$this->pi_list_searchBox();
	
				// Adds the result browser:
			$fullTable.=$this->pi_list_browseresults();
	
				// Returns the content from the plugin.
			return $fullTable;
		}
	}
	/**
	 * Display a single item from the database
	 *
	 * @param	string		$content: The PlugIn content
	 * @param	array		$conf: The PlugIn configuration
	 * @return	HTML of a single database entry
	 */
	function singleView($content,$conf)	{
		$this->conf=$conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
		
	
			// This sets the title of the page for use in indexed search results:
		if ($this->internal['currentRow']['title'])	$GLOBALS['TSFE']->indexedDocTitle=$this->internal['currentRow']['title'];
	
		$content='<div'.$this->pi_classParam('singleView').'>
			<h2>Record "'.$this->internal['currentRow']['uid'].'" from table "'.$this->internal['currentTable'].'":</h2>
				<p'.$this->pi_classParam("singleViewField-datum").'><strong>'.$this->getFieldHeader('datum').':</strong> '.$this->getFieldContent('datum').'</p>
				<p'.$this->pi_classParam("singleViewField-adm").'><strong>'.$this->getFieldHeader('adm').':</strong> '.$this->getFieldContent('adm').'</p>
				<p'.$this->pi_classParam("singleViewField-bericht").'><strong>'.$this->getFieldHeader('bericht').':</strong> '.$this->getFieldContent('bericht').'</p>
				<p'.$this->pi_classParam("singleViewField-kundennummer").'><strong>'.$this->getFieldHeader('kundennummer').':</strong> '.$this->getFieldContent('kundennummer').'</p>
				<p'.$this->pi_classParam("singleViewField-kundenname").'><strong>'.$this->getFieldHeader('kundenname').':</strong> '.$this->getFieldContent('kundenname').'</p>
				<p'.$this->pi_classParam("singleViewField-plz").'><strong>'.$this->getFieldHeader('plz').':</strong> '.$this->getFieldContent('plz').'</p>
				<p'.$this->pi_classParam("singleViewField-ort").'><strong>'.$this->getFieldHeader('ort').':</strong> '.$this->getFieldContent('ort').'</p>
				<p'.$this->pi_classParam("singleViewField-branche").'><strong>'.$this->getFieldHeader('branche').':</strong> '.$this->getFieldContent('branche').'</p>
		<p>'.$this->pi_list_linkSingle($this->pi_getLL('back','Back'),0).'</p></div>'.
		$this->pi_getEditPanel();
	
		return $content;
	}
	/**
	 * Returns a single table row for list view
	 *
	 * @param	integer		$c: Counter for odd / even behavior
	 * @return	A HTML table row
	 */
	function pi_list_row($c)	{
		$editPanel = $this->pi_getEditPanel();
		if ($editPanel)	$editPanel='<td>'.$editPanel.'</td>';
	
		return '<tr'.($c%2 this-="">pi_classParam('listrow-odd') : '').'>
				<td><p>'.$this->getFieldContent('uid').'</p></td>
				<td valign="top"><p>'.$this->getFieldContent('datum').'</p></td>
				<td valign="top"><p>'.$this->getFieldContent('adm').'</p></td>
				<td valign="top"><p>'.$this->getFieldContent('kundennummer').'</p></td>
				<td valign="top"><p>'.$this->getFieldContent('kundenname').'</p></td>
				<td valign="top"><p>'.$this->getFieldContent('plz').'</p></td>
				<td valign="top"><p>'.$this->getFieldContent('ort').'</p></td>
				<td valign="top"><p>'.$this->getFieldContent('branche').'</p></td>
			</tr>';
	}
	/**
	 * Returns a table row with column names of the table
	 *
	 * @return	A HTML table row
	 */
	function pi_list_header()	{
		return '<tr'.$this->pi_classParam('listrow-header').'>
				<td><p>'.$this->getFieldHeader_sortLink('uid').'</p></td>
				<td nowrap=""><p>'.$this->getFieldHeader('datum').'</p></td>
				<td nowrap=""><p>'.$this->getFieldHeader('adm').'</p></td>
				<td><p>'.$this->getFieldHeader_sortLink('kundennummer').'</p></td>
				<td><p>'.$this->getFieldHeader_sortLink('kundenname').'</p></td>
				<td><p>'.$this->getFieldHeader_sortLink('plz').'</p></td>
				<td><p>'.$this->getFieldHeader_sortLink('ort').'</p></td>
				<td nowrap=""><p>'.$this->getFieldHeader('branche').'</p></td>
			</tr>';
	}
	/**
	 * Returns the content of a given field
	 *
	 * @param	string		$fN: name of table field
	 * @return	Value of the field
	 */
	function getFieldContent($fN)	{
		switch($fN) {
			case 'uid':
				return $this->pi_list_linkSingle($this->internal['currentRow'][$fN],$this->internal['currentRow']['uid'],1);	// The "1" means that the display of single items is CACHED! Set to zero to disable caching.
			break;
			case "datum":
					// For a numbers-only date, use something like: %d-%m-%y
				return strftime('%A %e. %B %Y',$this->internal['currentRow']['datum']);
			break;
			default:
				return $this->internal['currentRow'][$fN];
			break;
		}
	}
	/**
	 * Returns the label for a fieldname from local language array
	 *
	 * @param	[type]		$fN: ...
	 * @return	[type]		...
	 */
	function getFieldHeader($fN)	{
		switch($fN) {
			
			default:
				return $this->pi_getLL('listFieldHeader_'.$fN,'['.$fN.']');
			break;
		}
	}
	
	/**
	 * Returns a sorting link for a column header
	 *
	 * @param	string		$fN: Fieldname
	 * @return	The fieldlabel wrapped in link that contains sorting vars
	 */
	function getFieldHeader_sortLink($fN)	{
		return $this->pi_linkTP_keepPIvars($this->getFieldHeader($fN),array('sort'=>$fN.':'.($this->internal['descFlag']?0:1)));
	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/adm_report/pi1/class.tx_admreport_pi1.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/adm_report/pi1/class.tx_admreport_pi1.php']);
}

?>

Kann mir einer sagen, warum das so ist, bzw. was ich tun muss damit dies nicht mehr vorkommt?

Gruß
Thorsten

Norman2k ·

Bist du sicher, dass das der richtige Code ist? Ich sehe da weder ein Formular noch ein INSERT der irgendwas in die DB schreiben soll.
Sieht mir mehr aus wie der Standard-Code einer frisch erstellten Ext. mit dem Kickstarter.

Norman

RT-Schwabe ·

Sorry, habe die falsche Ext gepostet. Hier kommt die um die es geht:

<?php

require_once(PATH_tslib.'class.tslib_pibase.php');

class tx_admtest_pi1 extends tslib_pibase {
	var $prefixId = 'tx_admtest_pi1';		// Same as class name
	var $scriptRelPath = 'pi1/class.tx_admtest_pi1.php';	// Path to this script relative to the extension dir.
	var $extKey = 'adm_test';	// The extension key.
	
		function main($content,$conf)	{
		$this->conf=$conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
		$this->pi_USER_INT_obj=1;	// Configuring so caching is not expected. This value means that no cHash params are ever set. We do this, because it's a USER_INT object!
	

$query = 'INSERT INTO tx_admreport_adm (adm,kundennummer,kundenname,datum,plz,ort,bericht,branche,pid) 
 VALUES(
 "'.addslashes($this->piVars['DATA']['adm']).'", 
 "'.addslashes($this->piVars['DATA']['kundennummer']).'", 
 "'.addslashes($this->piVars['DATA']['kundenname']).'", 
 "'.addslashes($this->piVars['DATA']['date']).'", 
 "'.addslashes($this->piVars['DATA']['plz']).'", 
 "'.addslashes($this->piVars['DATA']['ort']).'", 
 "'.addslashes($this->piVars['DATA']['bericht']).'", 
 "'.addslashes($this->piVars['DATA']['branche']).'", 
 
 "'.$GLOBALS['TSFE']->id.'")'; 
 $res = mysql(TYPO3_db, $query); 
 echo mysql_error(); 
    
 
		$content='
			<h3>This is a form:</h3>
			<form action="'.$this->pi_getPageLink($GLOBALS['TSFE']->id).'" method="POST">
			<input type="hidden" name="no_cache" value="1">	
				<table>
				<tr><td>Datum:</td><td> <input type="text" name="'.$this->prefixId.'[DATA][date]" value=""></td></tr>
				<tr><td>Name ADM:</td><td> <input type="text" name="'.$this->prefixId.'[DATA][adm]" value=""></td></tr>
				<tr><td>Kundennummer:</td><td> <input type="text" name="'.$this->prefixId.'[DATA][kundennummer]" value=""></td></tr>
				<tr><td>Name Kunde:</td><td> <input type="text" name="'.$this->prefixId.'[DATA][kundenname]" value=""></td></tr>
				<tr><td>PLZ:</td><td> <input type="text" name="'.$this->prefixId.'[DATA][plz]" value=""></td></tr>
				<tr><td>Ort:</td><td> <input type="text" name="'.$this->prefixId.'[DATA][ort]" value=""></td></tr>
				<tr><td>Branche:</td><td> <input type="text" name="'.$this->prefixId.'[DATA][branche]" value=""></td></tr>
				<tr><td>Bericht:</td><td> <input type="text" name="'.$this->prefixId.'[DATA][bericht]" value=""></td></tr>
				<tr><td><input type="submit" name="'.$this->prefixId.'[submit_button]" value="SEND FORM"></td></tr>
				</table>
			</form>
';
	
		return $this->pi_wrapInBaseClass($content);
	}
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/adm_test/pi1/class.tx_admtest_pi1.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/adm_test/pi1/class.tx_admtest_pi1.php']);
}

?>

Gruß
Thorsten

Norman2k ·

Du solltest dich schon ans Tutorial halten #giggle#

<?php
if(isset($this->piVars['submit_button'])) { 
   // WHAT TO DO if content is submitted 
   # t3lib_div::debug($this->piVars); 
   $query = 'INSERT INTO user_test_testing (title,description,pid)'
   ...
} else {
   $content=' 
   <H3>This is a form:</H3>';
   ...
}
?>

Dir fehlt die if-Abfrage ob der User das Formular abgeschickt hat. Erst dann soll der INSERT ausgeführt werden.

Norman

RT-Schwabe ·

Hallo Norman,

danke für den Hinweis!
Ich hatte die IF-Abfrage entfernt, da ich immer einen Ausgabefehler beim Laden der Seite erhalten habe.
Jetzt weiss ich aber auch, woran das liegt:

wenn man eine Klammer { auf macht, sollte man diese auch wieder } zu machen #paralyzed#

gruß
Thorsten