How to add new user and save it in the MYSQL DB?

Hello!

Please can somebody shoe me how i can add a new user and save the data into the database.
I have a working version, where i can read and update user data, but it have´s no function to add a new user.

Clicking on a row in DataGrid, another ViewStack(2) opens with detailed informations about this user. Now i need a button above the DataGrid "Add new User". Then have ViewStack 2 to open with empty Textfields and a function to save the written data.

Thank´s in advance!
Mario
Flex Code:
-------------------
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="960" height="650" xmlns="*" creationComplete="send_data()">
 
<mx:Script> 
 
	private function send_data():void {
  	userRequest.send();
  	}
  	
  	public function eventListener(event:Event):void
	{
	anrede.text = dgUserRequest.selectedItem.anrede;
	usernachname.text = dgUserRequest.selectedItem.usernachname;
	username.text = dgUserRequest.selectedItem.username;
	plz.text = dgUserRequest.selectedItem.plz;
	ort.text = dgUserRequest.selectedItem.ort;
	strasse.text = dgUserRequest.selectedItem.strasse;
	tel_privat.text = dgUserRequest.selectedItem.tel_privat;
	emailaddress.text = dgUserRequest.selectedItem.emailaddress;
	userid.text = dgUserRequest.selectedItem.userid;
	gebdatum.text = dgUserRequest.selectedItem.gebdatum;
	beruf.text = dgUserRequest.selectedItem.beruf;
	sonstiges.text = dgUserRequest.selectedItem.sonstiges;
	dw_datum.text= dgUserRequest.selectedItem.dw_datum;
	dw_bedienung.text = dgUserRequest.selectedItem.dw_bedienung;
	dw_wickler.text = dgUserRequest.selectedItem.dw_wickler;
	dw_anordnung.text = dgUserRequest.selectedItem.dw_anordnung;
	dw_einwirkzeit.text = dgUserRequest.selectedItem.dw_einwirkzeit;
	dw_nachbehandlung.text = dgUserRequest.selectedItem.dw_nachbehandlung;
	dw_preis.text = dgUserRequest.selectedItem.dw_preis;
	dw_anmerkungen.text = dgUserRequest.selectedItem.dw_anmerkungen;
	image_url.source= dgUserRequest.selectedItem.image_url;
	fa_datum.text = dgUserRequest.selectedItem.fa_datum;
	fa_bedienung.text = dgUserRequest.selectedItem.fa_bedienung;
	fa_rezeptur.text = dgUserRequest.selectedItem.fa_rezeptur;
	fa_einwirkzeit.text = dgUserRequest.selectedItem.fa_einwirkzeit;
	fa_preis.text = dgUserRequest.selectedItem.fa_preis;
	fa_anmerkungen.text = dgUserRequest.selectedItem.fa_anmerkungen;  
	//userRequest.send();
	}
 
</mx:Script>
 
<mx:VBox>
 
	<mx:ViewStack id="kunden_mutter" width="960" creationPolicy="all">
	<mx:Canvas id="kunden_1" label="Kunden">
	
		<mx:DataGrid change="eventListener(event)" id="dgUserRequest" x="0" y="50" dataProvider="{userRequest.lastResult.users.user}" width="960" height="600" click="kunden_mutter.selectedChild=kunden_detail;" fontSize="11">
  		<mx:columns>
    		<mx:DataGridColumn headerText="Nr." dataField="userid" width="35" itemRenderer="CustomRenderer" sortable="false"/>
    		<mx:DataGridColumn headerText="Anrede" dataField="anrede" width="65" itemRenderer="CustomRenderer"/>
    		<mx:DataGridColumn headerText="Nachname" dataField="usernachname" width="200" itemRenderer="CustomRenderer"/>
    		<mx:DataGridColumn headerText="Vorname" dataField="username" width="110" itemRenderer="CustomRenderer" sortable="false"/>
    		<mx:DataGridColumn headerText="Plz" dataField="plz" width="50" itemRenderer="CustomRenderer"/>
    		<mx:DataGridColumn headerText="Ort" dataField="ort" width="190" itemRenderer="CustomRenderer"/>
    		<mx:DataGridColumn headerText="Straße" dataField="strasse" width="200" itemRenderer="CustomRenderer" sortable="false"/>
    		<mx:DataGridColumn headerText="Telefon Nr." dataField="tel_privat" width="110" itemRenderer="CustomRenderer" sortable="false"/>
  		</mx:columns>
		</mx:DataGrid>
		<mx:Button x="0" y="10" label="Neukunden anlegen"/>
    </mx:Canvas>
 
	<mx:Canvas id="kunden_detail" label="Details">
    	<mx:Form x="0" y="0" width="100%" paddingBottom="0"
			paddingLeft="0"
			paddingRight="0"
			paddingTop="0">
 
  		<mx:Canvas id="personenDaten" width="100%" x="0" y="0" >
  
    	<mx:constraintColumns>
      	<mx:ConstraintColumn id="col1" width="0" />
      	<mx:ConstraintColumn id="col2" width="50%" />
      	<mx:ConstraintColumn id="col3" width="50%" />
    	</mx:constraintColumns>
    
    	<mx:Panel dropShadowEnabled="false" title="Angaben zur Person" width="400" height="100%" left="col2:0" paddingBottom="10"
			paddingLeft="10"
			paddingRight="10"
			paddingTop="10">
      
      	<mx:HBox>
        <mx:Label text="Kunden Nr." textAlign="left" fontWeight="bold" width="100"/>
        <mx:TextInput id="userid" text="id" editable="false" width="60"/>
      	</mx:HBox>
      
      	<mx:HRule width="358" strokeWidth="2" strokeColor="#0B333C"/>
      
      	<mx:HBox left="50" top="50">
        <mx:Label text="Anrede" fontWeight="bold" width="100"/>
        <mx:TextInput id="anrede"  width="60"/>
      	</mx:HBox>
      
      	<mx:HBox>
        <mx:Label text="Nachname" fontWeight="bold" width="100"/>
        <mx:TextInput id="usernachname"  width="250"/>
      	</mx:HBox>
     
      	<mx:HBox>
        <mx:Label text="Vorname" fontWeight="bold" width="100"/>
        <mx:TextInput id="username"  width="250"/>
      	</mx:HBox>
      
      	<mx:HBox>
        <mx:Label text="Plz/Ort" fontWeight="bold" width="100"/>
        <mx:TextInput id="plz"  width="60"/>
        <mx:TextInput id="ort"  width="182"/>
      	</mx:HBox>
      
      	<mx:HBox>
        <mx:Label text="Straße" fontWeight="bold" width="100"/>
        <mx:TextInput id="strasse"  width="250"/>
      
      	</mx:HBox>
      	<mx:HRule width="358" strokeWidth="2" strokeColor="#0B333C"/>
      
      	<mx:HBox>
        <mx:Label text="Tel. Privat" fontWeight="bold" width="100"/>
        <mx:TextInput id="tel_privat"  width="250"/>
      	</mx:HBox>
      
      	<mx:HBox>
        <mx:Label text="E-Mail" fontWeight="bold" width="100"/>
        <mx:TextInput id="emailaddress"  width="250"/>
      	</mx:HBox>
      
      	<mx:HRule width="358" strokeWidth="2" strokeColor="#0B333C"/>
 
      	<mx:HBox>
        <mx:Label text="Geb. Datum" fontWeight="bold" width="100"/>
        <mx:DateField id="gebdatum" formatString="YYYY-MM-DD" yearNavigationEnabled="true" />
      	</mx:HBox>
      	
      	<mx:HBox>
        <mx:Label text="Beruf" fontWeight="bold" width="100"/>
        <mx:TextInput id="beruf"  width="250"/>
      	</mx:HBox>
      
      	<mx:HRule width="358" strokeWidth="2" strokeColor="#0B333C"/>
      
      	<mx:Text text="Sonstiges" fontWeight="bold" fontSize="11"/>
      	<mx:TextArea id="sonstiges" width="358" height="200"/>
      
      	<mx:HBox width="100%" horizontalAlign="center">
      	<mx:Button label="Speichern" id="submit" click="send_data()"/><mx:Button label="Schliessen" id="close" click="kunden_mutter.selectedChild=kunden_1;"/>
 
      	</mx:HBox>
    
    	</mx:Panel>
    
    	<mx:Panel dropShadowEnabled="false" title="Weitere Infos" height="100%" left="422" width="538" paddingBottom="10"
			paddingLeft="10"
			paddingRight="10"
			paddingTop="10">
      
      	<mx:HBox width="100%" horizontalAlign="left">
        <mx:Image id="image_url" width="237" height="178"/>
      	</mx:HBox>
      
      	<mx:HRule width="100%" strokeWidth="2" strokeColor="#0B333C"/>
 
        <mx:Accordion id="accordion" width="100%" height="100%" creationPolicy="all">
        <mx:VBox label="Dauerwelle" fontSize="11" fontWeight="normal" paddingLeft="10" paddingBottom="10" paddingRight="10" paddingTop="10">
                
        <mx:HBox>
        <mx:Label text="Datum" fontWeight="bold" width="100"/>
        <mx:DateField id="dw_datum" formatString="YYYY-MM-DD"/>
        <mx:Label text="Bedient von" fontWeight="bold" width="85"/>
        <mx:TextInput id="dw_bedienung"  width="157"/>
      	</mx:HBox>
      			
      	<mx:HBox>
        <mx:Label text="Wicklerstärke" fontWeight="bold" width="100"/>
        <mx:TextInput id="dw_wickler"  width="358"/>
      	</mx:HBox>
      	
      	<mx:HBox>
        <mx:Label text="Anordnung" fontWeight="bold" width="100"/>
        <mx:TextInput id="dw_anordnung"  width="358"/>
      	</mx:HBox>
      		
      	<mx:HBox>
        <mx:Label text="Einwirkzeit" fontWeight="bold" width="100"/>
        <mx:TextInput id="dw_einwirkzeit"  width="100"/>
        <mx:Label text="Nachbehandlung" fontWeight="bold" width="122"/>
        <mx:DateField id="dw_nachbehandlung" formatString="YYYY-MM-DD"/>
      	</mx:HBox>
      		
      	<mx:HBox>
        <mx:Label text="Preis" fontWeight="bold" width="100"/>
        <mx:TextInput id="dw_preis"  width="358"/>
      	</mx:HBox>
      		
      	<mx:HBox>
        <mx:Label text="Anmerkungen" fontWeight="bold" width="100"/>
        <mx:TextArea id="dw_anmerkungen" width="358" height="100"/>
      	</mx:HBox>
   
        </mx:VBox>
 
        <mx:VBox label="Farbe" fontSize="11" fontWeight="normal" paddingLeft="10" paddingBottom="10" paddingRight="10" paddingTop="10">
            
        <mx:HBox>
        <mx:Label text="Datum" fontWeight="bold" width="100"/>
        <mx:DateField id="fa_datum" formatString="YYYY-MM-DD"/>
      	</mx:HBox>
      			
      	<mx:HBox>
        <mx:Label text="Bedient von" fontWeight="bold" width="100"/>
        <mx:TextInput id="fa_bedienung"  width="358"/>
      	</mx:HBox>
      			
      	<mx:HBox>
        <mx:Label text="Rezeptur" fontWeight="bold" width="100"/>
        <mx:TextInput id="fa_rezeptur"  width="358"/>
      	</mx:HBox>
      		
      	<mx:HBox>
        <mx:Label text="Einwirkzeit" fontWeight="bold" width="100"/>
        <mx:TextInput id="fa_einwirkzeit"  width="358"/>
      	</mx:HBox>
      			
      	<mx:HBox>
        <mx:Label text="Preis" fontWeight="bold" width="100"/>
        <mx:TextInput id="fa_preis"  width="358"/>
      	</mx:HBox>
      			
      	<mx:HBox>
        <mx:Label text="Anmerkungen" fontWeight="bold" width="100"/>
        <mx:TextArea id="fa_anmerkungen" width="358" height="100"/>
      	</mx:HBox>
      			
        </mx:VBox>
        </mx:Accordion>
      
    </mx:Panel>
    
  </mx:Canvas>
  
</mx:Form>
</mx:Canvas>
 
</mx:ViewStack>
</mx:VBox>
 
<mx:HTTPService id="userRequest" url="http://localhost/kunden.php" useProxy="false" method="POST">
  <mx:request xmlns="">
    <userid>{userid.text}</userid>
    <anrede>{anrede.text}</anrede>
    <username>{username.text}</username>
    <usernachname>{usernachname.text}</usernachname>
    <plz>{plz.text}</plz>
    <ort>{ort.text}</ort>
    <strasse>{strasse.text}</strasse>
    <tel_privat>{tel_privat.text}</tel_privat>
    <emailaddress>{emailaddress.text}</emailaddress>
    <gebdatum>{gebdatum.text}</gebdatum>
    <beruf>{beruf.text}</beruf>
    <sonstiges>{sonstiges.text}</sonstiges>
    <dw_datum>{dw_datum.text}</dw_datum>
    <dw_bedienung>{dw_bedienung.text}</dw_bedienung>
    <dw_wickler>{dw_wickler.text}</dw_wickler>
    <dw_anordnung>{dw_anordnung.text}</dw_anordnung>
    <dw_einwirkzeit>{dw_einwirkzeit.text}</dw_einwirkzeit>
    <dw_nachbehandlung>{dw_nachbehandlung.text}</dw_nachbehandlung>
    <dw_preis>{dw_preis.text}</dw_preis>
    <dw_anmerkungen>{dw_anmerkungen.text}</dw_anmerkungen>
    <fa_datum>{fa_datum.text}</fa_datum>
    <fa_bedienung>{fa_bedienung.text}</fa_bedienung>
    <fa_rezeptur>{fa_rezeptur.text}</fa_rezeptur>
    <fa_einwirkzeit>{fa_einwirkzeit.text}</fa_einwirkzeit>
    <fa_preis>{fa_preis.text}</fa_preis>
    <fa_anmerkungen>{fa_anmerkungen.text}</fa_anmerkungen>
  </mx:request>
</mx:HTTPService>
 
 
 
PHP:
-----------------------
$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
 
mysql_select_db( DATABASE_NAME );
 
 
if (isset($_POST["emailaddress"]) && is_numeric($_POST["userid"])) {
	//mysql_query("INSERT INTO users (emailaddress,username) VALUES('".$_POST["emailaddress"]."','".$_POST["username"]."')");
	mysql_query("UPDATE users SET emailaddress='".$_POST["emailaddress"]."', username='".$_POST["username"]."', usernachname='".$_POST["usernachname"]."', anrede='".$_POST["anrede"]."', plz='".$_POST["plz"]."', ort='".$_POST["ort"]."', strasse='".$_POST["strasse"]."', tel_privat='".$_POST["tel_privat"]."', gebdatum='".$_POST["gebdatum"]."', beruf='".$_POST["beruf"]."', sonstiges='".$_POST["sonstiges"]."', dw_datum='".$_POST["dw_datum"]."', dw_bedienung='".$_POST["dw_bedienung"]."', dw_wickler='".$_POST["dw_wickler"]."', dw_anordnung='".$_POST["dw_anordnung"]."', dw_einwirkzeit='".$_POST["dw_einwirkzeit"]."', dw_nachbehandlung='".$_POST["dw_nachbehandlung"]."', dw_preis='".$_POST["dw_preis"]."', dw_anmerkungen='".$_POST["dw_anmerkungen"]."', fa_datum='".$_POST["fa_datum"]."', fa_bedienung='".$_POST["fa_bedienung"]."', fa_rezeptur='".$_POST["fa_rezeptur"]."', fa_einwirkzeit='".$_POST["fa_einwirkzeit"]."', fa_preis='".$_POST["fa_preis"]."', fa_anmerkungen='".$_POST["fa_anmerkungen"]."', image_url='".$_POST["image_url"]."' WHERE userid=".$_POST["userid"]);
}
 
 
$Query = "SELECT * from users";
$Result = mysql_query( $Query );
 
$Return = "<users>";
 
while ( $User = mysql_fetch_object( $Result ) )
{
$Return .= "<user><userid>".$User->userid."</userid><anrede>".$User->anrede."</anrede><username>".$User->username."</username><usernachname>".$User->usernachname."</usernachname><emailaddress>".$User->emailaddress."</emailaddress><plz>".$User->plz."</plz><ort>".$User->ort."</ort><strasse>".$User->strasse."</strasse><tel_privat>".$User->tel_privat."</tel_privat><gebdatum>".$User->gebdatum."</gebdatum><beruf>".$User->beruf."</beruf><sonstiges>".$User->sonstiges."</sonstiges><dw_datum>".$User->dw_datum."</dw_datum><dw_bedienung>".$User->dw_bedienung."</dw_bedienung><dw_wickler>".$User->dw_wickler."</dw_wickler><dw_anordnung>".$User->dw_anordnung."</dw_anordnung><dw_einwirkzeit>".$User->dw_einwirkzeit."</dw_einwirkzeit><dw_nachbehandlung>".$User->dw_nachbehandlung."</dw_nachbehandlung><dw_preis>".$User->dw_preis."</dw_preis><dw_anmerkungen>".$User->dw_anmerkungen."</dw_anmerkungen><fa_datum>".$User->fa_datum."</fa_datum><fa_bedienung>".$User->fa_bedienung."</fa_bedienung><fa_rezeptur>".$User->fa_rezeptur."</fa_rezeptur><fa_einwirkzeit>".$User->fa_einwirkzeit."</fa_einwirkzeit><fa_preis>".$User->fa_preis."</fa_preis><fa_anmerkungen>".$User->fa_anmerkungen."</fa_anmerkungen><image_url>".$User->image_url."</image_url></user>";
}
$Return .= "</users>";
mysql_free_result( $Result );
print ($Return);

Open in new window

20oneAsked:
Who is Participating?
 
20oneAuthor Commented:
Please can somebody help me?

Thank´s,
Mario
0
 
Jones911Commented:
Well you have most of what you need.

You need a new function to clear the fields on click of a "New" button.
Then you can populate then with what ever text/values.

Then save using the PHP fucntions you have for the update but modified for an insert.  On return of the insert send back the new data so the grid can be updated.
0
 
20oneAuthor Commented:
I have create a 2. function. But first time i click the button, i get an error. Click i the button a 2. time, the other ViewStack opens, but within loaded user data.

Thank´s!
function to open viewstack with emtpy textfields:
---------------------------------------------------
public function eventListenerEmpty(event:Event):void
	{
	anrede.text = "";
	usernachname.text = "";
	username.text = "";
	plz.text = "";
	ort.text = "";
	strasse.text = "";
	tel_privat.text = "";
	emailaddress.text = "";
	userid.text = "";
	gebdatum.text = "";
	beruf.text = "";
	sonstiges.text = "";
	dw_datum.text= "";
	dw_bedienung.text = "";
	dw_wickler.text = "";
	dw_anordnung.text = "";
	dw_einwirkzeit.text = "";
	dw_nachbehandlung.text = "";
	dw_preis.text = "";
	dw_anmerkungen.text = "";
	image_url.source= "";
	fa_datum.text = "";
	fa_bedienung.text = "";
	fa_rezeptur.text = "";
	fa_einwirkzeit.text = "";
	fa_preis.text = "";
	fa_anmerkungen.text = "";  
	}
 
Button to add new user:
-------------------------
		<mx:Button click="eventListener(event);kunden_mutter.selectedChild=kunden_detail" x="0" y="10" label="Neukunden anlegen" />

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Jones911Commented:
Why do you do this:   kunden_mutter.selectedChild=kunden_detail
0
 
20oneAuthor Commented:
To open the second ViewStack, named kunden_detail. kunden_mutter is the mother and kunden_detail her child.
0
 
20oneAuthor Commented:
i have use the false eventListener. Instead eventListenerEmpty i use the eventlIstener.
0
 
20oneAuthor Commented:
OK, the changing of the ViewStack works now. I also have modified the PHP with an INSERT. But how can i tell PHP, it´s an UPDATE or an INSERT, what coming from Flex. I think i have to define a if/else, but i don´t know how.
PHP UPDATE:
----------------
if (isset($_POST["emailaddress"]) && is_numeric($_POST["userid"])) {
	//mysql_query("INSERT INTO users (emailaddress,username) VALUES('".$_POST["emailaddress"]."','".$_POST["username"]."')");
	mysql_query("UPDATE users SET emailaddress='".$_POST["emailaddress"]."', username='".$_POST["username"]."', usernachname='".$_POST["usernachname"]."', anrede='".$_POST["anrede"]."', plz='".$_POST["plz"]."', ort='".$_POST["ort"]."', strasse='".$_POST["strasse"]."', tel_privat='".$_POST["tel_privat"]."', gebdatum='".$_POST["gebdatum"]."', beruf='".$_POST["beruf"]."', sonstiges='".$_POST["sonstiges"]."', dw_datum='".$_POST["dw_datum"]."', dw_bedienung='".$_POST["dw_bedienung"]."', dw_wickler='".$_POST["dw_wickler"]."', dw_anordnung='".$_POST["dw_anordnung"]."', dw_einwirkzeit='".$_POST["dw_einwirkzeit"]."', dw_nachbehandlung='".$_POST["dw_nachbehandlung"]."', dw_preis='".$_POST["dw_preis"]."', dw_anmerkungen='".$_POST["dw_anmerkungen"]."', fa_datum='".$_POST["fa_datum"]."', fa_bedienung='".$_POST["fa_bedienung"]."', fa_rezeptur='".$_POST["fa_rezeptur"]."', fa_einwirkzeit='".$_POST["fa_einwirkzeit"]."', fa_preis='".$_POST["fa_preis"]."', fa_anmerkungen='".$_POST["fa_anmerkungen"]."', image_url='".$_POST["image_url"]."' WHERE userid=".$_POST["userid"]);
}
 
 
PHP INSERT:
----------------
mysql_query("INSERT INTO users VALUES emailaddress='".$_POST["emailaddress"]."', username='".$_POST["username"]."', usernachname='".$_POST["usernachname"]."', anrede='".$_POST["anrede"]."', plz='".$_POST["plz"]."', ort='".$_POST["ort"]."', strasse='".$_POST["strasse"]."', tel_privat='".$_POST["tel_privat"]."', gebdatum='".$_POST["gebdatum"]."', beruf='".$_POST["beruf"]."', sonstiges='".$_POST["sonstiges"]."', dw_datum='".$_POST["dw_datum"]."', dw_bedienung='".$_POST["dw_bedienung"]."', dw_wickler='".$_POST["dw_wickler"]."', dw_anordnung='".$_POST["dw_anordnung"]."', dw_einwirkzeit='".$_POST["dw_einwirkzeit"]."', dw_nachbehandlung='".$_POST["dw_nachbehandlung"]."', dw_preis='".$_POST["dw_preis"]."', dw_anmerkungen='".$_POST["dw_anmerkungen"]."', fa_datum='".$_POST["fa_datum"]."', fa_bedienung='".$_POST["fa_bedienung"]."', fa_rezeptur='".$_POST["fa_rezeptur"]."', fa_einwirkzeit='".$_POST["fa_einwirkzeit"]."', fa_preis='".$_POST["fa_preis"]."', fa_anmerkungen='".$_POST["fa_anmerkungen"]."', image_url='".$_POST["image_url"]."' WHERE userid=".$_POST["userid"]);

Open in new window

0
 
Jones911Commented:
From flex you have 2 methods.

Save

and

Update


Clicking Save calls the save method in PHP clicking Update calls the update method.
0
 
20oneAuthor Commented:
Thank´s Jones911 but i have my own solution.
The INSERT was not correct.
0
 
Jones911Commented:
Good glad its workign.
0
 
20oneAuthor Commented:
Yeah, thank´s!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.