Monday, 7 July 2014

How to upload a file using PHP ?

For uploading a file in PHP + MySQL, we have to use datatype as varchar(250) to store the filename and it's path.

Before uploading any photo or doc we must have to make our form tag as enctype. And inside the phpcode we must have to check weather the file is already exists in that location or not.

For Example. Table of Database is given
tblimagedemo
Data Type            Column Name 
id                           int                             PK  Auto_Increment  
photo                    varchar(250)

On Webpage file upload control is given in below image to choose file with button submit.


For that write HTML code.

<form method="post" name="pform" id="pform" action="" enctype="multipart/form-data">
<table>
<tr>
<td>Image</td>
<td>
<input type="file" value="" name="file" id="file"/>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="submit" name="btnsubmit"/>
<input type="button" value="reset" name="btnreset"/>
</td>
</tr>
</table>
</form>


Write PHP code in head or body section.
<?php

if(isset($_POST['btnsubmit']))
{
if($_FILES['file']['error']>0)
   {
         echo"Error in uploading";
   }
else
   {
       echo"upload:".$_FILES['file']['name']."<br>";
        $i="image/".$_FILES['file']['name'];

         if(file_exists("image/".$_FILES['file']['name']))
          {
                       echo $_FILES['file']['name']."File is already  Exists";
          }
          else
          {
                   move_uploaded_file($_FILES['file']['tmp_name'],"image/".$_FILES['file']['name']);
                  echo "File is uploaded successfully";
          }
    }
if(empty($i))
{
echo "Upload your image";
}
else
{
$sql = "INSERT INTO `superstore`.`tblimagedemo` (`id`, `photo`) VALUES (NULL, '$i')";

                 if(mysqli_query($con,$sql))
                {
                      echo"Record inserted";
                 }
                else
                {
                        echo"error";
                 }
}
}

Learn PHP Project with MySQL 
visit website : Final Year Project Training in Vadodara for student

Saturday, 5 April 2014

Date Datatype in PHP with Different Date Format.

Date in PHP with Example code.

Date Datatype's syntax is the 

date(format in string, time stamp);

Default format which the developer wants is dd/mm/yyyy we can do it by following code.

 date("d/m/Y");

This code will print the current date with 05/04/2014

Formats a time and date according to the format string provided in the first parameter. If the second parameter is not specified, the current time and date is used.As shown in the above example.

 date("m/d/Y");

This code will print the current date with 04/05/2014

The mktime() function returns the timestamp for the date and we can store the output of the function mktime() to any variable in php for printing suitable date.


Syntax of mktime() is given below

mktime(hour,minute,second,month,day,year,is_dst)

We can change the format of the date by mktime function.By providing the time stamp as a second argument in the date function.
if we want to print the date after 10 days then we can use below formatting.with the help of mktime and date.
hour=0
minite=0
second=0
month = m means current month April (4)
day = d+10 means 10 days will be added to the current date and
year = Y means current year (2014)

$dateten = mktime(0,0,0,date("m"),date("d")+10,date("Y"));
echo "The date after 10 days is ".date("d/m/Y", $dateten);


output : The date after 10 days is 15/04/2014

Here in above example the $dateten variable will print the date after 10 days as output.

Date Format

D
Instead of current date 05 if we have to print name of the day then use above char D.It will print first three letters of the current day.

 date("D/m/Y");

This code will print the current date with SAT/04/2014

l (small l)
Instead of current date 05 if we have to print name of the day then use above char D.It will print name the current day.

 date("l/m/Y");

This code will print the current date with Saturday/04/2014


M
Instead of current Month 04 if we have to print name of the month then use above char F.It will print first three letters of the current month.
 date("d/M/Y");

This code will print the current date with 05/Apr/2014

F
Instead of current Month 04 if we have to print name of the month then use above char F.It will print name of the current month.

 date("d/F/Y");

This code will print the current date with 05/April/2014

y
If we have to print the Current year's last two digits then use y.Year with two digits; e.g., “14”.

date("d/m/y");

This code will print the current date with 05/04/14

PHP PRoject Training in Vadodara Gujarat.
Best PHP training provider in Vadodara.
Visit Website : PHP Training Center Gujarat

Friday, 21 February 2014

How To Update Your Record Using PHP code ???

Update Records Using PHP + MySQL 

          If Any One wants to update his/her profile then he/she must have to login for that.
And according to their username and password the profile management is done.

Here is the code for any programmer or developer who want's to manage users and admins from their website.
1. First of all he/she have to see all the records of the table 
2. then select any record to update
3. After updating the record he/she must have to see updated record with all other records.

Table Name of MySQL is tbladmin_reg
Have columns
1. id
2. fullname
3. userid
4. password
5. emailid
6. mobileno

Here is the full code for admin registration1. show details


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show Registration Details </title>
<link rel="stylesheet" href="table.css" type="text/css">
<?php
include('dbconnection.php');
$sql="select * from tbladmin_reg";
$result=mysqli_query($con,$sql);
echo "<center>";
echo "<h1> SHOW REGISTRATION FORM </h1>";
echo "<table width='90%'>";
echo "<tr>";
echo "<th> ID NO. </th>";
echo "<th> FULL NAME </th>";
echo "<th> USER ID </th>";
echo "<th> PASSWORD </th>";
echo "<th> Email ID </th>";
echo "<th> MOBILE NO </th>";
echo "</tr>";

while($line=mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>";
echo $line['id'];
echo "</td>";
echo "<td>";
echo $line['fullname'];
echo "</td>";
echo "<td>";
echo $line['userid'];
echo "</td>";
echo "<td>";
echo $line['password'];
echo "</td>";

echo "<td>";
echo $line['emailid'];
echo "</td>";

echo "<td>";
echo $line['mobileno'];
echo "</td>";
$id=$line['id'];
echo "<td>";
echo "<a href=\"updatereg.php?id=$id\">Edit</a>";
echo "</td>";
echo "</tr>";
}
?>
</head>

<body>
</body>
</html>

2. 
And here is the code for updatereg.php file
updatereg.php

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Update Record</title>
</head>
<?php
$id=$_REQUEST['id'];
include('dbconnection.php');
$sql="select * from tbladmin_reg where `id`='$id'";
$result=mysqli_query($con,$sql) or die("Database table error");
echo "<center>";
echo "<h1> Update Record </h1>";
echo "<table width='90%'>";
echo "<tr><td>FullName</td><td>";
while($line=mysqli_fetch_assoc($result))
{
?>

<body>
<form id="form1" name="form1" method="post" action="" />
<input type="text" name="fullname" value="<?php echo $line['fullname']; ?>" />
<?php
echo "</td></tr><tr><td>UserID</td><td>";
?>
<input type="text" name="username" value="<?php echo $line['userid']; ?>" />

<?php
echo "</td></tr><tr><td>EmailID</td><td>";
?>
<input type="text" name="emailid" value="<?php echo $line['emailid']; ?>" />
<?php
echo "</td></tr><tr><td>Mobileno</td><td>";
?>
<input type="text" name="mobileno" value="<?php echo $line['mobileno']; ?>" />

<?php
echo "</td></tr><tr><td></td><td>";
?>

<?php
echo "</td></tr></table>";
}
?>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit']))
{
$id=$_REQUEST['id'];
$ui=$_POST['username'];
$fn=$_POST['fullname'];
$ei=$_POST['emailid'];
$mn=$_POST['mobileno'];

include('dbconnection.php');
$sql="update tbladmin_reg set `userid`='$ui',`fullname`='$fn',`emailid`='$ei',`mobileno`='$mn' where `id`='$id'";
$result=mysqli_query($con,$sql) or die("Database table error");
echo "Record Updated Successfully!!!";

}
?>   
</body>
</html>