In this example, we are learning to how to store image in database using Php.
First of all, create a folder of images name where your code file available. After create a database with two column of id and name.
We are storing just path of the image and give id of the image in database.
Let's see the example that how it works :
file name : index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
<HTML><BODY><FORMMETHOD="POST"ACTION="image_upload.PHP"enctype="multipart/form-data"><pre>Example of Image Upload<br>ID :<INPUTTYPE='TEXT'NAME='ID'> <br/>Image:<inputname="uploadedfile"type="file"/><br/><INPUTTYPE="SUBMIT"VALUE="Insert"><INPUTTYPE="RESET"VALUE="CLEAR"></pre></FORM></BODY></HTML>
<?php$id=$_POST['ID'];$conn=mysql_connect("localhost","root","");mysql_select_db("demo");$fn=basename($_FILES['uploadedfile']['name']);$target_path="./images/";$target_path=$target_path.$fn;$tfn=$_FILES['uploadedfile']['tmp_name'];$x=move_uploaded_file($tfn,$target_path);if($x){echo"<img src='images/".basename($_FILES['uploadedfile']['name'])."'</img> ";echo"<br>The file <u>[".basename($_FILES['uploadedfile']['name'])."</u>] has been uploaded<br>";}else{echo"There was an error uploading the file, please try again!";die;}$insert="insert into img values($id,'$fn')";mysql_query($insert)ordie("INSERT ".mysql_error());mysql_query("commit")ordie("commit fail ".mysql_error());echo"<br>Record Successfully Added";?><ahref="index.php">Back to Home</a>
Output :
Note : database name : demo table name : img field name : id (varchar) , name (varchar)
In this example , we are learning about how to delete selected row record from database in php.
Here, we are fetching record from database. If database not available then create a database and add records. then put link of delete record in each row.
Here, I create a database login_tbl which have 3 column of id,name,molbile.
From first file , If you delete 1st record then id of the first record pass by query string on second page and delete selected record.
Let's see the example :
<?php$servername="localhost";$username="root";$password="";$dbname="demo";$conn=newmysqli($servername,$username,$password,$dbname);?><FORMMETHOD="POST"enctype="multipart/form-data"><?php$sql="SELECT *FROM login_tbl";$result=$conn->query($sql);echo"<table border='1' style='width:50%'><tr><th>ID</th><th>Name</th><th>Mobile</th><th>Delete</th></tr>";if($result->num_rows>0){// output data of each rowwhile($row=$result->fetch_assoc()){echo"<br><tr><td align='center'>".$row["id"]." </td><td align='center'>".$row["name"]."</td><td align='center'>".$row["mobile"]."</td><td align='center'><a href='delete.php?id=".$row["id"]."'>Delete</td><br>";}}else{echo"No data available";}echo"</table>";?></FORM>
File name : delete.php
1
2
3
4
5
6
7
8
9
10
11
<?php$id=$_GET['id'];$cn=mysql_connect("localhost","root","");mysql_select_db("demo");$s1="delete from login_tbl where id=".$id;mysql_query($s1)ordie("INSERT ".mysql_error());mysql_query("commit")ordie("commit fail ".mysql_error());header("Location: index.php");?>
In this example, we are learning that how to use color dialog box control in c#.
First of all , we need to add Color Dialog Control in the form. In this example, one multi line textbox and two buttons were added in the form.
After adding the control, we are learning that how to change background color of textbox using color dialog box and also change font color of textbox.
When you click on the button color dialog box will be open and then you choose color. After selecting color, color will be change of textbox background color and font color.
In this example , we are learning how to encrypt and decrypt password in php.
Php
Provide md5() function to encrypt password. Using md5() function we can
store data in encrypted format and can secure password using md5(). Let's see the Example :- File Name : index.php
1
2
3
4
5
6
7
8
9
10
<?php$password="Hello";$encrypt=md5($password);echo"Your password before encrypted is : ".$password."<br/>";echo"Encrypted password [HELLO] : ".$encrypt."<br>";if($encrypt==md5($password)){echo"Your Decrypt password : ".$password;}?>
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceItem_Add_ListBox{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){item_listbox.Items.Add(txt_item_add.Text);txt_item_add.Text="";//item_listbox = name of listbox//txt_item_add = name of textbox}}}
In this example, we are learning about how to create login form with session using Php.
Session is one type of way which is used to store information to be used in multiple pages. Session is used to store some details about purchasing product in any website , store username, pass data in multiple forms , etc.
for staring session in file , session_start() function is used. to set value in session variable, $_SESSION php variable is used.
for destroy session , session_destroy() function is used.
<?phpsession_start();//start sessionif(isset($_POST['click'])){$_SESSION['user']=$_POST['username'];//set value of username in session varibleheader("Location:file2.php");exit;}?><html><body><formmethod="post"><inputtype="text"placeholder="Enter Username"name="username"/><br/><inputtype="password"placeholder="Enter Password"name="password"/><br/><inputtype="submit"name="click"value="Click Here"><inputtype="reset"name="cancel"value="Cancel"></form></body></html>
Spinner is a one type of drop down menu which is allow you to select an item from menu. In this Example, On Item select of Spinner , Background color will be changed with image.
First of all, when you select Red then background color will be changed and set red color and image of red circle will be set in Image View. As the same , Select on Green , background color will be set green and green circle set in Imageview.
Using This Example, We can change background color of the layout on item select in spinner.
packagecom.example.spinnerdemo;importjava.util.ArrayList;importjava.util.List;importandroid.app.Activity;importandroid.graphics.Color;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.View;importandroid.widget.AdapterView;importandroid.widget.AdapterView.OnItemSelectedListener;importandroid.widget.ArrayAdapter;importandroid.widget.ImageView;importandroid.widget.RelativeLayout;importandroid.widget.Spinner;importandroid.widget.Toast;publicclassMainActivityextendsActivity{Spinnerspinner;RelativeLayoutr1;ImageViewi1;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);spinner=(Spinner)findViewById(R.id.spinner1);r1=(RelativeLayout)findViewById(R.id.r1);i1=(ImageView)findViewById(R.id.imageView1);List<String>list=newArrayList<String>();list.add("RED");list.add("GREEN");list.add("BLUE");ArrayAdapter<String>dataAdapter=newArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);spinner.setAdapter(dataAdapter);spinner.setOnItemSelectedListener(newOnItemSelectedListener(){@OverridepublicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){// TODO Auto-generated method stubStringitem=arg0.getItemAtPosition(arg2).toString();Toast.makeText(getApplicationContext(),"Selected: "+item,Toast.LENGTH_LONG).show();if(item=="RED"){r1.setBackgroundColor(Color.RED);i1.setBackgroundResource(R.drawable.red);}if(item=="GREEN"){r1.setBackgroundColor(Color.GREEN);i1.setBackgroundResource(R.drawable.green);}if(item=="BLUE"){r1.setBackgroundColor(Color.BLUE);i1.setBackgroundResource(R.drawable.blue);}}@OverridepublicvoidonNothingSelected(AdapterView<?>arg0){// TODO Auto-generated method stub}});}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main,menu);returntrue;}}
In Android, There are many way to store data of an application. But if you want to store some small value then shared preferences is best way to store data of an application. It is allow to save and retrieve data from key and value.
Shared Preference is worked Key and Value. We can store string,integer, long, number etc. value
packagecom.example.sharedpreference;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.content.Context;importandroid.content.SharedPreferences;importandroid.view.MenuItem;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.Toast;publicclassMainActivityextendsActivity{EditTexte1,e2;Buttonb1;SharedPreferencesshared;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);e1=(EditText)findViewById(R.id.editText1);e2=(EditText)findViewById(R.id.editText2);b1=(Button)findViewById(R.id.button);shared=getSharedPreferences("MyPreference",Context.MODE_PRIVATE);Stringuser=shared.getString("UserKey","");//In first arguement set key which you store in variable and second argue is default valueStringpwd=shared.getString("PassKey","");e1.setText(user);//Here, if value is available in shared preference then value is display in edit text.e2.setText(pwd);b1.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewarg0){// TODO Auto-generated method stubStringuname=e1.getText().toString();Stringpass=e2.getText().toString();//it is used to data by shared preference, SharedPreference.Editor class is used SharedPreferences.Editoreditor=shared.edit();editor.putString("UserKey",uname);// it is store key and value... UserKey is key and uname is valueeditor.putString("PassKey",pass);editor.commit();Toast.makeText(getApplicationContext(),"Record Saved in SharedPreference",Toast.LENGTH_SHORT).show();}});}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main,menu);returntrue;}}
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/editText1"android:layout_marginTop="67dp"android:hint="Enter User Name"android:layout_alignParentRight="true"android:layout_alignParentEnd="true"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"/><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/editText2"android:layout_below="@+id/editText1"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_alignParentRight="true"android:layout_alignParentEnd="true"android:hint="Enter Password"/><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/editText2"android:layout_centerHorizontal="true"android:layout_marginTop="18dp"android:text="Save"/></RelativeLayout>
After click on save button data saved in shared preference. To see the proper output, first close this application after save data in shared preference, then open application and you will see all the text you have written back in your application.
In this example, We will learn that how to copy data from editText and paste data.
For copying and pasting different types of data , Android provides clipboard framework. In this example, we use ClipboardManager class which is used to allow you to copy and paste text via clipboard.
First of all that, You input some data in editText, then click on Copy Button to copy text from edit text. After for paste this copied , You click on Paste button. You will display copied text in text view after clicked on Paste Button.
packagecom.example.copypastedemo;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.content.ClipData;importandroid.content.ClipboardManager;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.TextView;importandroid.widget.Toast;publicclassMainActivityextendsActivity{EditTexted1;TextViewt1;Buttonb1,b2;privateClipboardManagermyClipboard;privateClipDatamyClip;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ed1=(EditText)findViewById(R.id.editText1);t1=(TextView)findViewById(R.id.textView1);b1=(Button)findViewById(R.id.button1);b2=(Button)findViewById(R.id.button2);myClipboard=(ClipboardManager)getSystemService(CLIPBOARD_SERVICE);b1.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewarg0){// TODO Auto-generated method stubStringtext;text=ed1.getText().toString();myClip=ClipData.newPlainText("text",text);myClipboard.setPrimaryClip(myClip);Toast.makeText(getApplicationContext(),"Text Copied",Toast.LENGTH_SHORT).show();}});b2.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewarg0){// TODO Auto-generated method stubClipDataabc=myClipboard.getPrimaryClip();ClipData.Itemitem=abc.getItemAt(0);Stringtext=item.getText().toString();t1.setText(text);Toast.makeText(getApplicationContext(),"Text Pasted",Toast.LENGTH_SHORT).show();}});}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main,menu);returntrue;}}
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><EditTextandroid:id="@+id/editText1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:layout_marginLeft="48dp"android:layout_marginTop="76dp"android:ems="10"android:hint=""><requestFocus/></EditText><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/editText1"android:layout_centerVertical="true"android:text="@string/b1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBaseline="@+id/button1"android:layout_alignBottom="@+id/button1"android:layout_centerHorizontal="true"android:text="@string/b2"/><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignLeft="@+id/editText1"android:layout_below="@+id/editText1"android:layout_marginTop="20dp"android:text="Paste Text Here"/></RelativeLayout>