2013-08-31 53 views
0

几个月前我开始编写我的第一个动态页面......学习地面零点前端和后端的东西。在数据库中插入图片 - PHP - MySQL WITH用户名

在这里和那里有几个颠簸,所以这是多一个。

这是一个房地产页面,人们可以添加列表和这样的图片。 我读了一些关于数据库内图片问题的着名线索herethere。 我只是决定使用数据库,因为我需要从某处开始。

我用这个tutorial开始。几小时后,瞧,我能够在真实的服务器中插入图片。很高兴。

但随后出现问题。图片需要附加到用户名(或电子邮件)。 当您尝试插入内容时,该网站具有用户名受保护的页面。

这是插入图片到数据库的主要文件。请注意,我在代码中添加了用户名,但用户名并未被插入,只是图片。我需要知道谁在插入什么。

有一个侧面的问题,你可能会跳过:图片需要附加到地址。当用户选择了图片并按下了上传按钮时,我是否可以拥有一个输入字段和地址,其中同一个按钮将同时执行两个功能? (如果我有两个按钮,用户可能不会这样做,这样我们就可以同时获取图片和地址,并在后面输入用户名)。我会知道如何有两个按钮,两个查询。

非常感谢您的帮助。我真的需要它。

$query = sprintf(
     "insert into images (filename, mime_type, file_size, file_data, username) 
      values ('%s', '%s', %d, '%s','%s')", 
     mysql_real_escape_string($image['name']), 
     mysql_real_escape_string($info['mime']), 
     $image['size'], 
     mysql_real_escape_string(
      file_get_contents($image['tmp_name'])), 
    mysql_real_escape_string(
      file_get_contents($_SESSION['user']) 
     ) 
    ); 

这里是完整的代码。

<?php 
//error_reporting(E_ALL); 
//ini_set("display_errors", 1); 
require("common.php"); 
if(empty($_SESSION['user']))  
    { 
     header("Location: ../index.php"); 
     die("Redirecting to .../index.php"); 
    } 

?> 

以上代码的第一部分只是检查用户是否已经登录。然后是代码。

<?php 
    require_once('globalsConfigPic1.php'); 

    function assertValidUpload($code) 
    { 
     if ($code == UPLOAD_ERR_OK) { 
      return; 
     } 

     switch ($code) { 
      case UPLOAD_ERR_INI_SIZE: 
      case UPLOAD_ERR_FORM_SIZE: 
       $msg = 'Image is too large'; 
       break; 

      case UPLOAD_ERR_PARTIAL: 
       $msg = 'Image was only partially uploaded'; 
       break; 

      case UPLOAD_ERR_NO_FILE: 
       $msg = 'No image was uploaded'; 
       break; 

      case UPLOAD_ERR_NO_TMP_DIR: 
       $msg = 'Upload folder not found'; 
       break; 

      case UPLOAD_ERR_CANT_WRITE: 
       $msg = 'Unable to write uploaded file'; 
       break; 

      case UPLOAD_ERR_EXTENSION: 
       $msg = 'Upload failed due to extension'; 
       break; 

      default: 
       $msg = 'Unknown error'; 
     } 

     throw new Exception($msg); 
    } 

    $errors = array(); 

    try { 
     if (!array_key_exists('image', $_FILES)) { 
      throw new Exception('Image not found in uploaded data'); 
     } 

     $image = $_FILES['image']; 

     // ensure the file was successfully uploaded 
     assertValidUpload($image['error']); 

     if (!is_uploaded_file($image['tmp_name'])) { 
      throw new Exception('File is not an uploaded file'); 
     } 

     $info = getImageSize($image['tmp_name']); 

     if (!$info) { 
      throw new Exception('File is not an image'); 
     } 
    } 
    catch (Exception $ex) { 
     $errors[] = $ex->getMessage(); 
    } 

    if (count($errors) == 0) { 
     // no errors, so insert the image 

     $query = sprintf(
     "insert into images (filename, mime_type, file_size, file_data, username) 
      values ('%s', '%s', %d, '%s','%s')", 
     mysql_real_escape_string($image['name']), 
     mysql_real_escape_string($info['mime']), 
     $image['size'], 
     mysql_real_escape_string(
      file_get_contents($image['tmp_name'])), 

     mysql_real_escape_string(
      file_get_contents($_SESSION['user']) 
     ) 
    ); 

     mysql_query($query, $db); 

     $id = (int) mysql_insert_id($db); 

     // finally, redirect the user to view the new image 
     header('Location: view.php?id=' . $id); 
     exit; 
    } 
?> 
<html> 
    <head> 
     <title>Error</title> 
    </head> 
    <body> 
     <div> 
      <p> 
       The following errors occurred: 
      </p> 

      <ul> 
       <?php foreach ($errors as $error) { ?> 
        <li> 
         <?php echo htmlSpecialChars($error) ?> 
        </li> 
       <?php } ?> 
      </ul> 

      <p> 
       <a href="upload.php">Try again</a> 
      </p> 
     </div> 
    </body> 
</html> 
+0

-1为了在数据库中存储图像的想法。 –

回答

-1

是的! 下面是第一部分中的部分解决方案:

$query = sprintf(
      "insert into images (filename, mime_type, file_size, file_data, username) 
       values ('%s', '%s', %d, '%s','%s')", 
      mysql_real_escape_string($image['name']), 
      mysql_real_escape_string($info['mime']), 
      $image['size'], 
      mysql_real_escape_string(
       file_get_contents($image['tmp_name'])), 

      mysql_real_escape_string (
       htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8') 
      ) 
     ); 

现在该图片努力与地址连接。