我有一个更新记录的代码及其工作正常我唯一的问题是,当我试图弄乱代码以查看它是否正常工作并尝试提交时,警报消息始终显示“成功更新”它应该是“更新失败“我试着总是显示这个”成功更新“。有人可以帮我这个吗?
这是我的代码
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 WHERE id=:id");
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $error ){
if ($error != UPLOAD_ERR_OK) {
$errors[] = $_FILES['files']['name'][$key] . ' was 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 be less than 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 it does not exist
}
if(is_file($desired_dir.'/'.$file_name)==false)
{
move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name);
}
else
{ //rename the file if another one 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));
}
?>
答案 0 :(得分:0)
您检查$ crud-&gt; upload()是true还是false并显示js msgbox作为回报并且根据您的代码,没有任何内容返回true或false。同时尝试按我的方式设置括号,使其更容易阅读。避免继续使用。所有这一切都是将脚本继续到下一个语句。
试试这个:(将session_start();放在主索引的最顶端)
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 WHERE id=:id");
$_SESSION['errors'] = array(); //register an empty session array
foreach($_FILES['files']['tmp_name'] as $key => $error )
{
if ($error != UPLOAD_ERR_OK)
{
$_SESSION['errors'] = $_FILES['files']['name'][$key] . ' was 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 be less than 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 it does not exist
}
if(is_file($desired_dir.'/'.$file_name)==false)
{
move_uploaded_file($file_tmp,$desired_dir.'/'.$file_name);
return TRUE;
}
else
{ //rename the file if another one 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;
}
}
}