Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Table structure
- CREATE TABLE IF NOT EXISTS `tbl_users` (
- `userID` int(11) NOT NULL AUTO_INCREMENT,
- `userName` varchar(20) NOT NULL,
- `userProfession` varchar(50) NOT NULL,
- `userPic` varchar(200) NOT NULL,
- PRIMARY KEY (`userID`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;
- addnew.php
- <?php
- error_reporting( ~E_NOTICE ); // avoid notice
- require_once 'dbconfig.php';
- if(isset($_POST['btnsave']))
- {
- $username = $_POST['user_name'];// user name
- $userjob = $_POST['user_job'];// user email
- $imgFile = $_FILES['user_image']['name'];
- $tmp_dir = $_FILES['user_image']['tmp_name'];
- $imgSize = $_FILES['user_image']['size'];
- if(empty($username)){
- $errMSG = "Please Enter Username.";
- }
- else if(empty($userjob)){
- $errMSG = "Please Enter Your Job Work.";
- }
- else if(empty($imgFile)){
- $errMSG = "Please Select Image File.";
- }
- else
- {
- $upload_dir = 'user_images/'; // upload directory
- $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
- // valid image extensions
- $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
- // rename uploading image
- $userpic = rand(1000,1000000).".".$imgExt;
- // allow valid image file formats
- if(in_array($imgExt, $valid_extensions)){
- // Check file size '5MB'
- if($imgSize < 5000000) {
- move_uploaded_file($tmp_dir,$upload_dir.$userpic);
- }
- else{
- $errMSG = "Sorry, your file is too large.";
- }
- }
- else{
- $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- }
- }
- // if no error occured, continue ....
- if(!isset($errMSG))
- {
- $stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
- $stmt->bindParam(':uname',$username);
- $stmt->bindParam(':ujob',$userjob);
- $stmt->bindParam(':upic',$userpic);
- if($stmt->execute())
- {
- $successMSG = "new record succesfully inserted ...";
- header("refresh:5;index.php"); // redirects image view page after 5 seconds.
- }
- else
- {
- $errMSG = "error while inserting....";
- }
- }
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Upload, Insert, Update, Delete an Image using PHP MySQL</title>
- </head>
- <body>
- <?php
- if(isset($errMSG)){
- ?>
- <div class="alert alert-danger">
- <span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
- </div>
- <?php
- }
- else if(isset($successMSG)){
- ?>
- <div class="alert alert-success">
- <strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
- </div>
- <?php
- }
- ?>
- <form method="post" enctype="multipart/form-data" class="form-horizontal">
- <table class="table table-bordered table-responsive">
- <tr>
- <td><label class="control-label">Username.</label></td>
- <td><input class="form-control" type="text" name="user_name" placeholder="Enter Username" value="<?php echo $username; ?>" /></td>
- </tr>
- <tr>
- <td><label class="control-label">Profession(Job).</label></td>
- <td><input class="form-control" type="text" name="user_job" placeholder="Your Profession" value="<?php echo $userjob; ?>" /></td>
- </tr>
- <tr>
- <td><label class="control-label">Profile Img.</label></td>
- <td><input class="input-group" type="file" name="user_image" accept="image/*" /></td>
- </tr>
- <tr>
- <td colspan="2"><button type="submit" name="btnsave" class="btn btn-default">
- <span class="glyphicon glyphicon-save"></span> save
- </button>
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- <?php
- $DB_HOST = 'localhost';
- $DB_USER = 'root';
- $DB_PASS = '';
- $DB_NAME = 'dubicanac';
- try{
- $DB_con = new PDO("mysql:host={$DB_HOST};dbname={$DB_NAME}",$DB_USER,$DB_PASS);
- $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $e){
- echo $e->getMessage();
- }
- ?>
- <?php
- error_reporting( ~E_NOTICE );
- require_once 'dbconfig.php';
- if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
- {
- $id = $_GET['edit_id'];
- $stmt_edit = $DB_con->prepare('SELECT userName, userProfession, userPic FROM tbl_users WHERE userID =:uid');
- $stmt_edit->execute(array(':uid'=>$id));
- $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
- extract($edit_row);
- }
- else
- {
- header("Location: index.php");
- }
- if(isset($_POST['btn_save_updates']))
- {
- $username = $_POST['user_name'];// user name
- $userjob = $_POST['user_job'];// user email
- $imgFile = $_FILES['user_image']['name'];
- $tmp_dir = $_FILES['user_image']['tmp_name'];
- $imgSize = $_FILES['user_image']['size'];
- if($imgFile)
- {
- $upload_dir = 'user_images/'; // upload directory
- $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
- $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
- $userpic = rand(1000,1000000).".".$imgExt;
- if(in_array($imgExt, $valid_extensions))
- {
- if($imgSize < 5000000)
- {
- unlink($upload_dir.$edit_row['userPic']);
- move_uploaded_file($tmp_dir,$upload_dir.$userpic);
- }
- else
- {
- $errMSG = "Sorry, your file is too large it should be less then 5MB";
- }
- }
- else
- {
- $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- }
- }
- else
- {
- // if no image selected the old image remain as it is.
- $userpic = $edit_row['userPic']; // old image from database
- }
- // if no error occured, continue ....
- if(!isset($errMSG))
- {
- $stmt = $DB_con->prepare('UPDATE tbl_users
- SET userName=:uname,
- userProfession=:ujob,
- userPic=:upic
- WHERE userID=:uid');
- $stmt->bindParam(':uname',$username);
- $stmt->bindParam(':ujob',$userjob);
- $stmt->bindParam(':upic',$userpic);
- $stmt->bindParam(':uid',$id);
- if($stmt->execute()){
- ?>
- <script>
- alert('Successfully Updated ...');
- window.location.href='index.php';
- </script>
- <?php
- }
- else{
- $errMSG = "Sorry Data Could Not Updated !";
- }
- }
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Upload, Insert, Update, Delete an Image using PHP MySQL</title>
- </head>
- <body>
- <form method="post" enctype="multipart/form-data" class="form-horizontal">
- <?php
- if(isset($errMSG)){
- ?>
- <div class="alert alert-danger">
- <span class="glyphicon glyphicon-info-sign"></span> <?php echo $errMSG; ?>
- </div>
- <?php
- }
- ?>
- <table class="table table-bordered table-responsive">
- <tr>
- <td><label class="control-label">Username.</label></td>
- <td><input class="form-control" type="text" name="user_name" value="<?php echo $userName; ?>" required /></td>
- </tr>
- <tr>
- <td><label class="control-label">Profession(Job).</label></td>
- <td><input class="form-control" type="text" name="user_job" value="<?php echo $userProfession; ?>" required /></td>
- </tr>
- <tr>
- <td><label class="control-label">Profile Img.</label></td>
- <td>
- <p><img src="user_images/<?php echo $userPic; ?>" height="150" width="150" /></p>
- <input class="input-group" type="file" name="user_image" accept="image/*" />
- </td>
- </tr>
- <tr>
- <td colspan="2"><button type="submit" name="btn_save_updates" class="btn btn-default">
- <span class="glyphicon glyphicon-save"></span> Update
- </button>
- <a class="btn btn-default" href="index.php"> <span class="glyphicon glyphicon-backward"></span> cancel </a>
- </td>
- </tr>
- </table>
- </form>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement