How to Save Data (without Image) in MySql Database using Php

<!doctype html>
<?php
  
 /*-------------------- CONNECTIVITY CODE --------------------*/

     error_reporting(E_ERROR | E_PARSE);      //optional,to remove warning messages.

     $conn=mysqli_connect("localhost","root","","codershelpline");
        if ($conn)
          {
           echo "database connected successfully","<br>";
          }
        else
          {
           die("Connection Aborted:" . mysqli_connect_error());
          }
 
           --------------------  OR  --------------------

     error_reporting(E_ERROR | E_PARSE);      //optional,to remove warning messages.

     $conn=mysqli_connect("localhost","root","","codershelpline");
        if (!$conn)
         
          {
           die("Connection Aborted:" . mysqli_connect_error());
          }       


   /*-----------  TRANSFER VALUES FROM HTML TO PHP -----------*/

        $TxtSlno1=$_REQUEST['TxtSlno'];
        $TxtSname1=$_REQUEST['TxtSname'];
	$TxaAddr1=$_REQUEST['TxaAddr'];
	$Rdbsex1=$_REQUEST['Rdbsex'];
	$TxtUname1=$_REQUEST['TxtUname'];
	$TxtPassword1=$_REQUEST['TxtPassword'];
	$DtpDob1=$_REQUEST['DtpDob'];
	$TxtMobno1=$_REQUEST['TxtMobno'];
	$TxtEmail1=$_REQUEST['TxtEmail'];	
	$CmbSecQues1=$_REQUEST['CmbSecQues'];
	$TxtSecAns1=$_REQUEST['TxtSecAns'];
	$ChkCricket1=$_REQUEST['ChkCricket'];	
	$TChkDance1=$_REQUEST['ChkDance'];
	$ChkMusic1=$_REQUEST['ChkMusic'];	
	$TxtRem1=$_REQUEST['TxtRem'];

 /*---------- NORMAL BASIC SAVE CODE TO MYSQL DATABASE ------------*/ 

        if(isset($_REQUEST['BtnSave']))
         { 
            mysqli_query($conn,"INSERT INTO chlsave(TxtSlno4,
            TxtSname4,TxaAddr4,Rdbsex4,TxtUname4,TxtPassword4,
            DtpDob4,TxtMobno4,TxtEmail4,CmbSecQues4,TxtSecAns4,
            ChkCricket4,TChkDance4,ChkMusic4,TxtRem4)VALUES            
            ('$TxtSlno1','$TxtSname1','$TxaAddr1','$Rdbsex1',
           '$TxtUname1','$TxtPassword1','$DtpDob1','$TxtMobno1',                                        
           '$TxtEmail1','$CmbSecQues1','$TxtSecAns1',
           '$ChkCricket1','$TChkDance1','$ChkMusic1',     
           '$TxtRem1')");

           if(mysqli_affected_rows($conn)>0)
              {
                echo "Data Saved Successfully";
              } 
             else 
              {
                echo "Data Not Saved <br/>";
                echo mysqli_error($conn);
              }
           }

        //mysqli_close($conn);      //optional,to close the connection properly.
  
  /*-------- VALIDATION SAVE CODE TO MYSQL DATABASE ----------*/
   
    if(isset($_REQUEST['BtnSave']))
      {
          $TxtSlno1=$_REQUEST['TxtSlno'];
 
// code to check first the saved values before saving new one.

          $qry="select TxtSlno4 from chlsave";
  	  $result=mysqli_query($conn,$qry);			
	  $a=0;			
	  while($row1=mysqli_fetch_array($result))
  	  {
	    $Sno=$row1['TxtSlno4'];
            
              /* if($Sno==$TxtSlno1)
	       {
	         echo "This serial No. Already Exist";
	         $a=$a+1;
	       }  OR   */
				
	    if($Sno)
	    {
	      echo "This serial No. Already Exist";
	      $a=$a+1;
	    }
	  }
	    
// code to save fresh/unique data 
		
	  if($a===0)
	  {
            mysqli_query($conn,"INSERT INTO chlsave(TxtSlno4,
            TxtSname4,TxaAddr4,Rdbsex4,TxtUname4,TxtPassword4,
            DtpDob4,TxtMobno4,TxtEmail4,CmbSecQues4,
            TxtSecAns4,ChkCricket4,TChkDance4,ChkMusic4,
            TxtRem4)VALUES ('$TxtSlno1','$TxtSname1',
            '$TxaAddr1','$Rdbsex1','$TxtUname1','$TxtPassword1',
            '$DtpDob1','$TxtMobno1','$TxtEmail1','$CmbSecQues1',
            '$TxtSecAns1','$ChkCricket1','$TChkDance1',
            '$ChkMusic1','$TxtRem1')");

             if(mysqli_affected_rows($conn)>0)
              {
                echo "Data Saved Successfully";
              } 
             else 
              {
                echo "Data Not Saved <br/>";
                echo mysqli_error($conn);
              }
           }
        //mysqli_close($conn);      //optional,to close the connection properly.

?> 
   
 <!---------------------HTML DESIGN CODE-------------------------> 
   
<html>
   <head>
      <title>Codershelpline Code for display html value</title>
   </head>
   <body>
      <form name="form1" id="form2" action="" method="post" >
	 <center>
	    <fieldset style="width: 600px">				
		<legend>Create New Student Account</legend>
		   <table>
		      <tr>
			  <td>Serial No.</td>
			  <td>:</td>
			  <td><input type="text" name="TxtSlno"></td>
		      </tr>
		      <tr>
			  <td>Student Name</td>
			  <td>:</td>
			  <td><input type="text" name="TxtSname"></td>
		      </tr>
		      <tr>
			  <td>Address</td>
			  <td>:</td>
			  <td>
			     <textarea cols="35" rows="5" name="TxaAddr"></textarea>							
			  </td>
		       </tr>
		       <tr>
			  <td>Sex</td>
			  <td>:</td>
		          <td>
			<input type="radio" name="Rdbsex" id="RdbMale" value="Male">Male
			<input type="radio" name="Rdbsex" id="RdbFemale" value="Female">Female
			<input type="radio" name="Rdbsex" id="RdbOther" value="Other">Other
		          </td>
		       </tr>						
		       <tr>
			  <td>User Name</td>
			  <td>:</td>
			  <td><input type="text" name="TxtUname"></td>
		       </tr>
		       <tr>
			  <td>Password</td>
			  <td>:</td>
			  <td><input type="password" name="TxtPassword"></td>
		       </tr>
		       <tr>
			  <td>Confirm Password</td>
			  <td>:</td>
			  <td><input type="password" name="TxtCpassword"></td>
		       </tr>
		       <tr>
		          <td>DOB</td>
			  <td>:</td>
			  <td><input type="date" name="DtpDob"></td>
		       </tr>
		       <tr>
			  <td>Mobile No.</td>
			  <td>:</td>
			  <td><input type="Number" name="TxtMobno"></td>
		       </tr>
		       <tr>
			  <td>Email</td>
			  <td>:</td>
			  <td><input type="email" name="TxtEmail"></td>
		       </tr>					
		       <tr>
			  <td>Security Questions</td>
			  <td>:</td>
			  <td>
   <select name="CmbSecQues">
       <option value="Choose One">Choose One</option>
       <option value="What is Your Favourite Book">What is Your Favourite Book</option>
       <option value="What is Your Favourite Teacher">What is Your Favourite Teacher</option>
       <option value="What is Your Favourate Place">What is Your Favourate Place</option> 
       <option value="What is Your Favourate Pets">What is Your Favourate Pets</option>
   </select>
			  </td>
		     </tr>
		     <tr>
			<td>Security Answer</td>
			<td>:</td>
			<td><input type="text" name="TxtSecAns"></td>
		     </tr>
		     <tr>
			<td>Hobbies</td>
			<td>:</td>
			<td>
			   <input type="checkbox" name="ChkCricket" value="Cricket">Cricket
			   <input type="checkbox" name="ChkDance" value="Dance">Dance
			   <input type="checkbox" name="ChkMusic" value="Music">Music			   
			</td>
		     </tr>
		     <tr>
			<td>Remarks</td>
			<td>:</td>
			<td><input type="text" name="TxtRem" value="N/A"></td>
		     </tr>
		     <tr>
			<td></td>
			<td></td>
			<td><input type="submit" name="BtnSave" value="Save">
			     <input type="reset" name="BtnReset" value="Reset">
			</td>
		     </tr>

		  </table>
	       </fieldset>
            </center>
	  </form>
        </body>
      </html>

NB: Run the program and Feed all the values correctly and then press save button to store the data in mysql database.

How to Save only Images in MySql Database using Php

<!doctype html>
 <?php
 	 
    error_reporting(E_ERROR | E_PARSE);      //optional,to remove warning messages.
    $conn=mysqli_connect("localhost","root","","codershelpline");
        if(!$conn)
          {
           die("Connection Aborted:" . mysqli_connect_error());
          }
   
   if(isset($_REQUEST['BtnSave']))
      {   
        if(getimagesize($_FILES['image1']['tmp_name'])==FALSE)
    	  {
             echo "Please select an image";
          }
        else
          {
             $image= addslashes($_FILES['image1']['tmp_name']);
             $name1= addslashes($_FILES['image1']['name']);

             $image2= file_get_contents($image);
             $image3= base64_encode($image2);
	     mysqli_query($conn,"INSERT INTO SaveImage(ImageFileName4,Image4) VALUES 
             ('$name1','$image3')");
        
             if(mysqli_affected_rows($conn)>0)
	        {
	          echo "Image Saved in Database";
	        } 
             else 
                {
	          echo "Image Not Saved";
                  echo mysqli_error($conn);
                }
          }
      }
      mysqli_close($conn);      //optional,to close the connection properly.	
?>
<html>
   <head>
      <title>Codershelpline Code to Save Image</title>
   </head>
   <body>
     <form name="form1" id="form2" action="#" method="post" enctype="multipart/form-data">
	<center>
	    <fieldset style="width: 600px">				
		<legend>Create New Student Account</legend>
		<table>
     		   <tr>
			<td>Image</td>
			<td>:</td>
			<td>
			    <input type="file" name="image1">
			</td>
		   </tr>
		   <tr>
			<td></td>
			<td></td>
			<td>
			   <input type="submit" name="BtnSave" value="SaveImage">
			   <input type="reset" name="BtnReset" value="Reset">								
			</td>
		    </tr>
	       </table>
	   </fieldset>
	</center>
     </form>
   </body>
</html>

/* NB: 
Database Name=codershelpline. Table name=SaveImage. Fields name=ImageFileName4 (to store by default image  path name as text) & Image4 (to store the selected image) in MySql database.

The above codes store the name of image path file name as text and selected image both.
 
Here 'tmp_name' and 'name' is the default temporary name of the uploaded/selected image and path of the image name respectively which is generated automatically during runtime by php, and stores both these files in the temporary folder on the server.

The Base64_encode() function used in the program, converts a group of similar binary data into ASCII string/text format by translating it into a radix-64 representation.

The file_get_contents() function prepares a way to read the contents of a image file into a string format using memory mapping techniques to enhance the overall performance.

The addslashes() function is used to add slashes symbol in place of certain characters(such as single quote ('), double quote ("), and the NULL character) in a ASCII strings that help to insert image binary data or bit of stream in the form of ASCII strings into a database in easy way.
*/

How to Save other Data with Images to MySql Database using Php

<!doctype html>

<?php
     error_reporting(E_ERROR | E_PARSE);      //optional,to remove warning messages.
     $conn=mysqli_connect("localhost","root","","codershelpline");
        if (!$conn)
          {
           die("Connection Aborted:" . mysqli_connect_error());
          }

     $TxtSlno1=$_REQUEST['TxtSlno'];
     $TxtSname1=$_REQUEST['TxtSname'];
     $TxaAddr1=$_REQUEST['TxaAddr'];
     $Rdbsex1=$_REQUEST['Rdbsex'];
     $TxtUname1=$_REQUEST['TxtUname'];	
     $TxtPassword1=$_REQUEST['TxtPassword'];
     $DtpDob1=$_REQUEST['DtpDob'];
     $TxtMobno1=$_REQUEST['TxtMobno'];
     $TxtEmail1=$_REQUEST['TxtEmail'];
     $CmbSecQues1=$_REQUEST['CmbSecQues'];
     $TxtSecAns1=$_REQUEST['TxtSecAns'];
     $ChkCricket1=$_REQUEST['ChkCricket'];
     $TChkDance1=$_REQUEST['ChkDance'];
     $ChkMusic1=$_REQUEST['ChkMusic'];
     $ChkSurfing1=$_REQUEST['ChkSurfing'];
     $TxtRem1=$_REQUEST['TxtRem'];    
    
     if(isset($_REQUEST['BtnSave']))
         {   
           if(getimagesize($_FILES['image1']['tmp_name'])==FALSE)
    	    {
    	      echo "Please select an image";
            }
           else
            {
              $image0= addslashes($_FILES['image1']['tmp_name']);
              $name1= addslashes($_FILES['image1']['name']);

              $image2= file_get_contents($image0);
              $image3= base64_encode($image2);

	mysqli_query($conn,"INSERT INTO chlsave(TxtSlno4,TxtSname4,TxaAddr4,Rdbsex4,TxtUname4,
TxtPassword4,DtpDob4,TxtMobno4,TxtEmail4,CmbSecQues4,TxtSecAns4,ChkCricket4,TChkDance4,ChkMusic4,TxtRem4,TxtImageFileName,image) VALUES ('$TxtSlno1','$TxtSname1','$TxaAddr1','$Rdbsex1',
'$TxtUname1','$TxtPassword1','$DtpDob1','$TxtMobno1','$TxtEmail1','$CmbSecQues1','$TxtSecAns1','$ChkCricket1','$TChkDance1','$ChkMusic1','$TxtRem1','$name1','$image3')");
        
                  if(mysqli_affected_rows($conn)>0)
	            {
	              echo "Data Saved";
                    } 
	          else 
	            {
                       echo "Data Not Saved";
	               echo mysqli_error($conn);
	            }
    	        }
	  }

  mysqli_close($conn);      //optional,to close the connection properly.
?>
<html>
    <head>
	<title>Codershelpline Code for Save Image</title>
    </head>
    <body>
       <form name="form1" id="form2" action="#" method="post" enctype="multipart/form-data">
	   <center>
		<fieldset style="width: 600px">				
		    <legend>Create New Student Account</legend>
			<table>
			   <tr>
			        <td>Serial No.</td>
			        <td>:</td>
				<td><input type="text" name="TxtSlno"></td>
			   </tr>
			   <tr>
				<td>Student Name</td>
				<td>:</td>
				<td><input type="text" name="TxtSname"></td>
			   </tr>
			   <tr>
				<td>Address</td>
			        <td>:</td>
				<td>
				    <textarea cols="35" rows="5" name="TxaAddr"></textarea>							
				</td>
			   </tr>
			   <tr>
			        <td>Sex</td>
				<td>:</td>
				<td>
				    <input type="radio" name="Rdbsex" id="RdbMale" 
                                    value="Male">Male
				    <input type="radio" name="Rdbsex" id="RdbFemale" 
                                    value="Female">Female
				    <input type="radio" name="Rdbsex" id="RdbOther" 
                                    value="Other">Other
				</td>
			    </tr>						
			    <tr>
				<td>User Name</td>
				<td>:</td>
				<td><input type="text" name="TxtUname"></td>
			    </tr>
			    <tr>
				<td>Password</td>
				<td>:</td>
				<td><input type="password" name="TxtPassword"></td>
			    </tr>					
			    <tr>
				<td>Confirm Password</td>
				<td>:</td>
				<td><input type="password" name="TxtCpassword"></td>
			    </tr>
			    <tr>
				<td>DOB</td>
				<td>:</td>
				<td><input type="date" name="DtpDob"></td>
			    </tr>
			    <tr>
				<td>Mobile No.</td>
				<td>:</td>
				<td><input type="Number" name="TxtMobno"></td>
			    </tr>
			    <tr>
				<td>Email</td>
				<td>:</td>
				<td><input type="text" name="TxtEmail"></td>
			    </tr>						
			    <tr>
				<td>Security Questions</td>
				<td>:</td>
				<td>
				    <select name="CmbSecQues">
					<option value="Choose One">Choose One</option>
					<option value="What is Your Favourite Book">What is 
                                        Your Favourite Book</option>
					<option value="What is Your Favourite Teacher">What is 
                                        Your Favourite Teacher</option>
					<option value="What is Your Favourate Place">What is 
                                        Your Favourate Place</option>
					<option value="What is Your Favourate Pets">What is 
                                        Your Favourate Pets</option>							
				    </select>
				 </td>
			     </tr>
			     <tr>
				<td>Security Answer</td>
				<td>:</td>
				<td><input type="text" name="TxtSecAns"></td>
			     </tr>
			     <tr>
				<td>Hobbies</td>
				<td>:</td>
				<td>
			<input type="checkbox" name="ChkCricket" value="Cricket">Cricket
			<input type="checkbox" name="ChkDance" value="Dance">Dance
			<input type="checkbox" name="ChkMusic" value="Music">Music							
				</td>
			      </tr>
			      <tr>
				<td>Remarks</td>
				<td>:</td>
				<td>
				   <input type="text" name="TxtRem" value="N/A">
				</td>
			      </tr>
			      <tr>
				<td>Image</td>
				<td>:</td>
				<td>
				   <input type="file" name="image1">
				</td>
			      </tr>
			      <tr>
			        <td></td>
				<td></td>
				<td>
				    <input type="submit" name="BtnSave" value="Save">
				    <input type="reset" name="BtnReset" value="Reset">
				    <input type="submit" name="BtnDelete" value="Delete">
				</td>
			      </tr>
			   </table>
		       </fieldset>
	            </center>
               </form>
	</body>
</html>

Loading

Categories: Php Proj

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.