Tuesday, 21 April 2020

Auto Id and File Upload

Hi All,
Find the code for Auto Id and File Upload in PHP...



CREATE TABLE IF NOT EXISTS `studentreg` (
  `sid` int(11) NOT NULL,
  `name` text NOT NULL,
  `address` text NOT NULL,
  `email` text NOT NULL,
  `phone` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

------------------------------------------------
studentform.php
-----------------------------------------------------
<html>
<body>
<?php

$con=mysql_connect("localhost","root","");
mysql_select_db("snit",$con);


//Code to insert into both registration and Login table
if(isset($_POST['btnsubmit']))
{
$sql="insert into studentreg values('$_POST[id1]','$_POST

[name]','$_POST[address]','$_POST[email]','$_POST

[phone]')";
echo $sql;
$sql1="insert into login values('$_POST[id1]','$_POST[password]','student','0')";
mysql_query($sql,$con);
mysql_query($sql1,$con);

$target_dir = "uploads/";
$target_file=$target_dir .$_POST['id1'].'.jpg';
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],

$target_file) or die('Error'.mysql_error());


}

?>

<?php

//Auto generate next id in the screen.

//Query to get the highest value in table

$sql="select max(sid) as mid from studentreg";
$data=mysql_query($sql);
$id=0;
while($row=mysql_fetch_array($data))
{
 $id=$row['mid'];
}

$id=$id+1;
echo $id;
?>
<form action=# method=post  enctype="multipart/form-data">
<pre>
Id      <input name=id disabled=disabled value="<?php echo

$id ; ?>" pattern="[a-zA-Z]{2,10}" title="Enter only

characters">
<input type=hidden name=id1 value="<?php echo $id ; ?>" >
Name    <input name=name required=required maxlength=18>
Address <input name=address>
Email   <input type=email name=email>
Phone   <input name=phone pattern="[0-9]{10,12}">
Password<input name=password >
Photo   <input type="file" name="fileToUpload"

id="fileToUpload">
<input type=submit name=btnsubmit value='Save'>
</pre>
</form>

2 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog. for more article on PHP Language like Functions,
    PHP with MYSQL for future reference , you can refer form here. thanks a lot .

    ReplyDelete