如何将mysql的结果放在文本框中?

时间:2015-08-24 03:34:37

标签: php pdo

我有更新页面和更新功能,更新正在运行我的问题是如何将图像的值放在文本框中?有人可以帮助我吗?我正在使用PDO。

user.class.php

import Foundation
import UIKit

let kMLGroundHeight: CGFloat = 20.0

let kDefaultXToMovePerSecond: CGFloat = 320.0

//CollisionDetection

let heroCategory: UInt32 = 0x1 << 0
let wallCategory: UInt32 = 0x1 << 1

//Levels
let kLevelGenerationTimes: [NSTimeInterval] = [0.8, 0.7, 0.6, 0.4, 0.3]
let kNumberOfPointsPerLevel = 5

update.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;
        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)
    {
        $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;
    }   
}
}

HTML

<?php
include_once 'DB.php';
$username = isset($_POST['username']) ? $_POST['username'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';
$province = isset($_POST['province']) ? $_POST['province'] : '';
$FILE_NAME = isset($_POST['FILE_NAME']) ? $_POST['FILE_NAME'] :$_POST['image_name']  ;
$FILE_SIZE = isset($_POST['FILE_SIZE']) ? $_POST['FILE_SIZE'] : '';
$FILE_TYPE = isset($_POST['FILE_TYPE']) ? $_POST['FILE_TYPE'] : '';



if(isset($_FILES['files'])){
$id = $_GET['id'];
$username = $_POST['username'];
$password = $_POST['password'];
$province = $_POST['province'];

result = $crud->upload($id,$FILE_NAME,$FILE_SIZE,$FILE_TYPE,$username,$password,$province);
if (!$result['error'])
{
echo '<script type="text/javascript">';
echo 'document.getElementById("name").value="'.$data["FILE_NAME"].'"';
echo '</script>';
}
else{
echo "<script type='text/javascript'>alert('Updating Failed!'); </script>";
} 

}
if(isset($_GET['id']))
    {
    $id = $_GET['id'];
    extract($crud->getID($id));
    }
?>

1 个答案:

答案 0 :(得分:0)

而不是返回true,返回一个像这样的数组

return ['error' =>false, 'file_name' => 'image file name']

更改此

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>";
}

$result = $crud->upload($id,$FILE_NAME,$FILE_SIZE,$FILE_TYPE,$username,$password,$province);
if (!$result['error'])
{
  echo '<script type="text/javascript">';
  echo 'document.getElementById("name").value="'.$data["file_name"].'"';
  echo '</script>';
}
else{
   echo "<script type='text/javascript'>alert('Updating Failed!'); </script>";
}