Advertisement
Guest User

Untitled

a guest
Mar 18th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. Table structure
  2.  
  3. CREATE TABLE IF NOT EXISTS `tbl_users` (
  4. `userID` int(11) NOT NULL AUTO_INCREMENT,
  5. `userName` varchar(20) NOT NULL,
  6. `userProfession` varchar(50) NOT NULL,
  7. `userPic` varchar(200) NOT NULL,
  8. PRIMARY KEY (`userID`)
  9. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;
  10.  
  11. addnew.php
  12.  
  13. <?php
  14.  
  15. error_reporting( ~E_NOTICE ); // avoid notice
  16.  
  17. require_once 'dbconfig.php';
  18.  
  19. if(isset($_POST['btnsave']))
  20. {
  21. $username = $_POST['user_name'];// user name
  22. $userjob = $_POST['user_job'];// user email
  23.  
  24. $imgFile = $_FILES['user_image']['name'];
  25. $tmp_dir = $_FILES['user_image']['tmp_name'];
  26. $imgSize = $_FILES['user_image']['size'];
  27.  
  28.  
  29. if(empty($username)){
  30. $errMSG = "Please Enter Username.";
  31. }
  32. else if(empty($userjob)){
  33. $errMSG = "Please Enter Your Job Work.";
  34. }
  35. else if(empty($imgFile)){
  36. $errMSG = "Please Select Image File.";
  37. }
  38. else
  39. {
  40. $upload_dir = 'user_images/'; // upload directory
  41.  
  42. $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
  43.  
  44. // valid image extensions
  45. $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
  46.  
  47. // rename uploading image
  48. $userpic = rand(1000,1000000).".".$imgExt;
  49.  
  50. // allow valid image file formats
  51. if(in_array($imgExt, $valid_extensions)){
  52. // Check file size '5MB'
  53. if($imgSize < 5000000) {
  54. move_uploaded_file($tmp_dir,$upload_dir.$userpic);
  55. }
  56. else{
  57. $errMSG = "Sorry, your file is too large.";
  58. }
  59. }
  60. else{
  61. $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  62. }
  63. }
  64.  
  65.  
  66. // if no error occured, continue ....
  67. if(!isset($errMSG))
  68. {
  69. $stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
  70. $stmt->bindParam(':uname',$username);
  71. $stmt->bindParam(':ujob',$userjob);
  72. $stmt->bindParam(':upic',$userpic);
  73.  
  74. if($stmt->execute())
  75. {
  76. $successMSG = "new record succesfully inserted ...";
  77. header("refresh:5;index.php"); // redirects image view page after 5 seconds.
  78. }
  79. else
  80. {
  81. $errMSG = "error while inserting....";
  82. }
  83. }
  84. }
  85. ?>
  86. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  87. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  88. <html xmlns="http://www.w3.org/1999/xhtml">
  89. <head>
  90. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  91. <title>Upload, Insert, Update, Delete an Image using PHP MySQL</title>
  92. </head>
  93. <body>
  94. <?php
  95. if(isset($errMSG)){
  96. ?>
  97. <div class="alert alert-danger">
  98. <span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
  99. </div>
  100. <?php
  101. }
  102. else if(isset($successMSG)){
  103. ?>
  104. <div class="alert alert-success">
  105. <strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
  106. </div>
  107. <?php
  108. }
  109. ?>
  110. <form method="post" enctype="multipart/form-data" class="form-horizontal">
  111. <table class="table table-bordered table-responsive">
  112. <tr>
  113. <td><label class="control-label">Username.</label></td>
  114. <td><input class="form-control" type="text" name="user_name" placeholder="Enter Username" value="<?php echo $username; ?>" /></td>
  115. </tr>
  116.  
  117. <tr>
  118. <td><label class="control-label">Profession(Job).</label></td>
  119. <td><input class="form-control" type="text" name="user_job" placeholder="Your Profession" value="<?php echo $userjob; ?>" /></td>
  120. </tr>
  121. <tr>
  122. <td><label class="control-label">Profile Img.</label></td>
  123. <td><input class="input-group" type="file" name="user_image" accept="image/*" /></td>
  124. </tr>
  125. <tr>
  126. <td colspan="2"><button type="submit" name="btnsave" class="btn btn-default">
  127. <span class="glyphicon glyphicon-save"></span> &nbsp; save
  128. </button>
  129. </td>
  130. </tr>
  131. </table>
  132. </form>
  133. </body>
  134. </html>
  135.  
  136. <?php
  137.  
  138. $DB_HOST = 'localhost';
  139. $DB_USER = 'root';
  140. $DB_PASS = '';
  141. $DB_NAME = 'dubicanac';
  142.  
  143. try{
  144. $DB_con = new PDO("mysql:host={$DB_HOST};dbname={$DB_NAME}",$DB_USER,$DB_PASS);
  145. $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  146. }
  147. catch(PDOException $e){
  148. echo $e->getMessage();
  149. }
  150. ?>
  151.  
  152. <?php
  153.  
  154. error_reporting( ~E_NOTICE );
  155.  
  156. require_once 'dbconfig.php';
  157.  
  158. if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
  159. {
  160. $id = $_GET['edit_id'];
  161. $stmt_edit = $DB_con->prepare('SELECT userName, userProfession, userPic FROM tbl_users WHERE userID =:uid');
  162. $stmt_edit->execute(array(':uid'=>$id));
  163. $edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
  164. extract($edit_row);
  165. }
  166. else
  167. {
  168. header("Location: index.php");
  169. }
  170.  
  171.  
  172.  
  173. if(isset($_POST['btn_save_updates']))
  174. {
  175. $username = $_POST['user_name'];// user name
  176. $userjob = $_POST['user_job'];// user email
  177.  
  178. $imgFile = $_FILES['user_image']['name'];
  179. $tmp_dir = $_FILES['user_image']['tmp_name'];
  180. $imgSize = $_FILES['user_image']['size'];
  181.  
  182. if($imgFile)
  183. {
  184. $upload_dir = 'user_images/'; // upload directory
  185. $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
  186. $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
  187. $userpic = rand(1000,1000000).".".$imgExt;
  188. if(in_array($imgExt, $valid_extensions))
  189. {
  190. if($imgSize < 5000000)
  191. {
  192. unlink($upload_dir.$edit_row['userPic']);
  193. move_uploaded_file($tmp_dir,$upload_dir.$userpic);
  194. }
  195. else
  196. {
  197. $errMSG = "Sorry, your file is too large it should be less then 5MB";
  198. }
  199. }
  200. else
  201. {
  202. $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  203. }
  204. }
  205. else
  206. {
  207. // if no image selected the old image remain as it is.
  208. $userpic = $edit_row['userPic']; // old image from database
  209. }
  210.  
  211.  
  212. // if no error occured, continue ....
  213. if(!isset($errMSG))
  214. {
  215. $stmt = $DB_con->prepare('UPDATE tbl_users
  216. SET userName=:uname,
  217. userProfession=:ujob,
  218. userPic=:upic
  219. WHERE userID=:uid');
  220. $stmt->bindParam(':uname',$username);
  221. $stmt->bindParam(':ujob',$userjob);
  222. $stmt->bindParam(':upic',$userpic);
  223. $stmt->bindParam(':uid',$id);
  224.  
  225. if($stmt->execute()){
  226. ?>
  227. <script>
  228. alert('Successfully Updated ...');
  229. window.location.href='index.php';
  230. </script>
  231. <?php
  232. }
  233. else{
  234. $errMSG = "Sorry Data Could Not Updated !";
  235. }
  236.  
  237. }
  238.  
  239.  
  240. }
  241. ?>
  242. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  243. <html xmlns="http://www.w3.org/1999/xhtml">
  244. <head>
  245. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  246. <title>Upload, Insert, Update, Delete an Image using PHP MySQL</title>
  247. </head>
  248. <body>
  249.  
  250. <form method="post" enctype="multipart/form-data" class="form-horizontal">
  251. <?php
  252. if(isset($errMSG)){
  253. ?>
  254. <div class="alert alert-danger">
  255. <span class="glyphicon glyphicon-info-sign"></span> &nbsp; <?php echo $errMSG; ?>
  256. </div>
  257. <?php
  258. }
  259. ?>
  260.  
  261.  
  262.  
  263. <table class="table table-bordered table-responsive">
  264.  
  265. <tr>
  266. <td><label class="control-label">Username.</label></td>
  267. <td><input class="form-control" type="text" name="user_name" value="<?php echo $userName; ?>" required /></td>
  268. </tr>
  269.  
  270. <tr>
  271. <td><label class="control-label">Profession(Job).</label></td>
  272. <td><input class="form-control" type="text" name="user_job" value="<?php echo $userProfession; ?>" required /></td>
  273. </tr>
  274.  
  275. <tr>
  276. <td><label class="control-label">Profile Img.</label></td>
  277. <td>
  278. <p><img src="user_images/<?php echo $userPic; ?>" height="150" width="150" /></p>
  279. <input class="input-group" type="file" name="user_image" accept="image/*" />
  280. </td>
  281. </tr>
  282.  
  283. <tr>
  284. <td colspan="2"><button type="submit" name="btn_save_updates" class="btn btn-default">
  285. <span class="glyphicon glyphicon-save"></span> Update
  286. </button>
  287.  
  288. <a class="btn btn-default" href="index.php"> <span class="glyphicon glyphicon-backward"></span> cancel </a>
  289.  
  290. </td>
  291. </tr>
  292. </table>
  293. </form>
  294.  
  295. </div>
  296. </body>
  297. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement