2011-10-16 35 views
0

如何將上傳圖片的位置路徑插入數據庫中用戶的pic_location?將上傳圖片的位置路徑插入到用戶的pic_location

所以我設法通過PHP上傳圖片的文件夾,但現在我的問題是,我不知道查詢將會怎樣我想做到這一點......

成員在他上傳圖片以便SESSION正在運行時登錄。

我會給你我的代碼看看看看,如果你能發現怎麼辦...

感謝

我picUpload.php(如果我上傳的圖片,並在那裏我要插入的位置,路徑到用戶的pic_loaction列的表用戶)

<?php 
     include 'connect.php'; 
     include 'header.php'; 

     if(isset($_SESSION['signed_in']) && $_SESSION['signed_in'] == true) 
     { 
      //This is the directory where images will be saved 
      $target=$_SERVER['DOCUMENT_ROOT'] . "/avatars/" . basename($_FILES['file']['name']); 

      //This gets all the other information from the form 
      $pic_location=($_FILES['file']['name']); 

      //Writes the information to the database 
      $sql = "UPDATE users SET pic_location='$target' WHERE user_id=" . $_SESSION['user_id']; 

      //Writes the photo to the server 
      if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) 
      { 

      //Tells you if its all ok 
      echo "The file ". basename($_FILES['file']['name']). " has been uploaded, and your information has been added to the directory"; 
      } 
      else { 

      //Gives and error if its not 
      echo "Sorry, there was a problem uploading your file."; 
      } 

     } 
     else 
     { 
      //nothing 
     } 
?> 

我connect.php

<?php 
    session_start(); 
    //connect.php 

    $server = 'localhost'; 
    $username = 'root'; 
    $password = ''; 
    $database = 'mydatabase'; 

    if(!mysql_connect('localhost', 'root', '')) 
    { 
     exit('Error: could not establish database connection'); 
    } 
    if(!mysql_select_db($database)) 
    { 
     exit('Error: could not select the database'); 
    } 
?> 

我的header.php

<!DOCTYPE HTML> 
    <head> 
     <title> ShareLink </title> 
     <link rel="stylesheet" href="style.css" type="text/css"> 

     <script src="jquery-1.6.4.min.js" type="text/javascript" charset="utf-8"></script> 
     <script type="text/javascript" src="script.js"></script> 

     <!-- Back to Top easing jQuery --> 
      <link rel="stylesheet" type="text/css" media="screen,projection" href="css/ui.totop.css" /> 

     <!-- jquery --> 
     <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script> 
     <!-- easing plugin (optional) --> 
     <script src="js/easing.js" type="text/javascript"></script> 
     <!-- UItoTop plugin --> 
     <script src="js/jquery.ui.totop.js" type="text/javascript"></script> 


     <script type="text/javascript"> 
      $(document).ready(function() { 
       /* 
       var defaults = { 
        containerID: 'moccaUItoTop', // fading element id 
        containerHoverClass: 'moccaUIhover', // fading element hover class 
        scrollSpeed: 1200, 
        easingType: 'linear' 
       }; 
       */ 

       $().UItoTop({ easingType: 'easeOutQuart' }); 

      }); 
     </script> 
    </head> 
    <body> 
    <h1> ShareLink </h1> 
     <div id="wrapper"> 
     <div id="menu"> 
      <a class="item" href="/index.php">Home</a> - 
      <a class="item" href="/create_topic.php">Create a topic</a> - 
      <a class="item" href="/create_cat.php">Create a category</a> - 
      <a class="item" href="/members.php"> Members </a> - 
      <a class="item" href="/search_form.php"> Search </a> - 
      <a class="item" href="/profile.php"> Profile </a> 


      <div id="userbar"> 
      <?php 
      if($_SESSION['signed_in']) 
      { 
       echo 'Hello <b>' . htmlentities($_SESSION['user_name']) . '</b>. <a class="item" href="signout.php">Log out</a>'; 
      } 
      else 
      { 
       print'<a class="item" href="signin.php">Log in</a> or <a class="item" href="signup.php">Register</a>'; 
      } 
      ?></div> 
     </div> 
      <div id="content"> 

,這裏是我的用戶表中我database.sql文件

CREATE TABLE users ( 
user_id  INT(8) NOT NULL AUTO_INCREMENT, 
user_name VARCHAR(30) NOT NULL, 
user_pass VARCHAR(255) NOT NULL, 
user_email VARCHAR(255) NOT NULL, 
user_date DATETIME NOT NULL, 
user_level INT(8) NOT NULL, 
pic_location VARCHAR(255) NOT NULL, 
UNIQUE INDEX user_name_unique (user_name), 
PRIMARY KEY (user_id) 
); 
+2

您需要保存$ target,這就是圖片現在的位置。 「UPDATE users SET pic_location ='」。mysql_real_escape_string($ target)。''WHERE user_id =「。intval($ userid) – galchen

+0

查看我的更新文件!我用你給我的代碼是有道理的,但是我得到一個錯誤:第12行的C:\ xampp \ htdocs \ picUpload.php中的未定義變量:user_id ...如何解決這個問題,因爲我看不到是什麼那裏錯了? –

+1

,因爲未定義$ user_id。你說用戶在這一點上登錄 - 這是你需要將用戶ID爲 – galchen

回答

1

存儲變量$target爲pic_location和查詢不插入它應該是更新查詢。

1

正如punit寫的,使用變量$ target來更新用戶的記錄。在查詢中,使用UPDATE而不是INSERT,確保您定義了用戶的ID(您現在擁有的INSERT只是簡單地添加了一個新行,但是您想要修改現有的行,因此您需要一個WHERE子句),並且在查詢中使用它之前,您絕對應該清理$ target,否則您會有一次盲目的SQL注入。

相關問題