php - Alert message always showing "successfully updated" even i input wrong using PDO? -
i have code updating records , working fine problem when try messed code see if working , try submit alert message displaying "successfully updated" should "updating failed" ever try displaying "successfully updated".can please me this?
here's code
class.user.php
public function upload($id,$file_name,$file_size,$file_type,$username,$password,$province) { $stmt = $this->db->prepare("update tish_images set file_name=:file_name,file_size=:file_size,file_type=:file_type,username=:username,password=:password,province=:province id=:id"); $errors= array(); foreach($_files['files']['tmp_name'] $key => $error ){ if ($error != upload_err_ok) { $errors[] = $_files['files']['name'][$key] . ' not uploaded.'; continue; } $file_name = $key.$_files['files']['name'][$key]; $file_size = $_files['files']['size'][$key]; $file_tmp = $_files['files']['tmp_name'][$key]; $file_type = $_files['files']['type'][$key]; if($file_size > 2097152){ $errors[] = 'file size must less 2 mb'; continue; } try{ $stmt->bindparam( ":id", $id); $stmt->bindparam( ":file_name", $file_name , pdo::param_str ); $stmt->bindparam( ":file_size", $file_size, pdo::param_str ); $stmt->bindparam( ":file_type", $file_type, pdo::param_str ); $stmt->bindparam( ":username", $username); $stmt->bindparam( ":password", $password); $stmt->bindparam( ":province", $province); $stmt->execute(); return true; $desired_dir="image_uploads"; if(is_dir($desired_dir)==false) { mkdir($desired_dir, 0700);// create directory if not exist } if(is_file($desired_dir.'/'.$file_name)==false) { move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name); } else { //rename file if 1 exist $new_file=$desired_dir.'/'.$file_name.time(); move_uploaded_file($file_tmp,$new_file) ; } } catch(pdoexception $e) { echo $e->getmessage(); return false; } } }
update.php
<?php include_once 'db.php'; $username = isset($_get['username']) ? $_get['username'] : ''; $password = isset($_get['password']) ? $_get['password'] : ''; $province = isset($_get['province']) ? $_get['province'] : ''; $file_name = isset($_get['file_name']) ? $_get['file_name'] : ''; $file_size = isset($_get['file_size']) ? $_get['file_size'] : ''; $file_type = isset($_get['file_type']) ? $_get['file_type'] : ''; if(isset($_files['files'])){ $id = $_get['id']; $username = $_post['username']; $password = $_post['password']; $province = $_post['province']; if($crud->upload($id,$file_name,$file_size,$file_type,$username,$password,$province)) { echo "<script type='text/javascript'>alert('successfully updated!'); </script>"; } else { echo "<script type='text/javascript'>alert('updating failed!'); </script>"; } } if(isset($_get['id'])) { $id = $_get['id']; extract($crud->getid($id)); } ?>
your checking see if $crud->upload() true or false , show js msgbox in return , based on code there nothing gives return of true or false. try set brackets have, makes lot more easier read. avoid using continue. continues script next statement.
try this: (place session_start(); on top of main index)
public function upload($id,$file_name,$file_size,$file_type,$username,$password,$province) { $stmt = $this->db->prepare("update tish_images set file_name=:file_name,file_size=:file_size,file_type=:file_type,username=:username,password=:password,province=:province id=:id"); $_session['errors'] = array(); //register empty session array foreach($_files['files']['tmp_name'] $key => $error ) { if ($error != upload_err_ok) { $_session['errors'] = $_files['files']['name'][$key] . ' not uploaded.'; //continue; return false; } $file_name = $key.$_files['files']['name'][$key]; $file_size = $_files['files']['size'][$key]; $file_tmp = $_files['files']['tmp_name'][$key]; $file_type = $_files['files']['type'][$key]; if($file_size > 2097152) { $_session['errors'] = 'file size must less 2 mb'; //continue; return false; } try { $stmt->bindparam( ":id", $id); $stmt->bindparam( ":file_name", $file_name , pdo::param_str ); $stmt->bindparam( ":file_size", $file_size, pdo::param_str ); $stmt->bindparam( ":file_type", $file_type, pdo::param_str ); $stmt->bindparam( ":username", $username); $stmt->bindparam( ":password", $password); $stmt->bindparam( ":province", $province); $stmt->execute(); $desired_dir="image_uploads"; if(is_dir($desired_dir)==false) { mkdir($desired_dir, 0700);// create directory if not exist } if(is_file($desired_dir.'/'.$file_name)==false) { move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name); return true; } else { //rename file if 1 exist $new_file=$desired_dir.'/'.$file_name.time(); move_uploaded_file($file_tmp,$new_file); return true; } } catch(pdoexception $e) { echo $e->getmessage(); return false; } } }
Comments
Post a Comment