2015-02-02 1 views
-1

사용자가 빈 사진 태그 대신 기본 이미지를 원하는 사진을 추가하지 않기로 선택하면 프로필 사진을 계정에 업로드 할 수있는 웹 기반 데이터베이스를 구축 중입니다. 어떤 이유에서든 파일이 선택되지 않으면, MySQL의 BLOB가 어쨌든 채워집니다. 무엇을 모르겠습니다. 추가 할 필요가있는 데이터베이스 나 코드 조각 내에서 변경할 필요가있는 것이 있습니까 ?? 데이터에 입력하기위한파일 업로드 - PHP MySQL

코드 : 표시 데이터

$username = mysql_prep($_POST["username"]); 
     $password = password_encrypt($_POST["password"]); 
//  $confirm = password_encrypt($_POST["confirm"]); 
     $firstName = mysql_prep($_POST["firstName"]); 
     $lastName = mysql_prep($_POST["lastName"]); 
     $File = $_FILES['fileField']['name']; 
     $tempnum = rand(20, 980780980); 
     $newfile = $tempnum . $File; 

//  if("password" != "confirm") { 
//   $_SESSION["message"] = "ERROR: Passwords must match."; 
//  }else{ 
      if (file_exists("images/profile_pictures/" . $newfile)) { 
       echo $newfile . " already exists. "; 
      } else { 
       $query = "INSERT INTO users ("; 
       $query .= " username, password, firstName, lastName, image"; 
       $query .= ") VALUES ("; 
       $query .= " '{$username}', '{$password}', '{$firstName}', '{$lastName}', '{$newfile}'"; 
       $query .= ")"; 
       $result = mysqli_query($connection, $query); 
       copy($_FILES['fileField']['tmp_name'], 'images/profile_pictures/' . $newfile); 
       if ($result > 0) { 
        // Success 
        $_SESSION["message"] = "User created."; 
        redirect_to("add_user.php"); 
       } else { 
        // Failure 
        $_SESSION["message"] = "User creation failed."; 
       } 
      } 
     } 
// } 
} 
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="add_user.php"> 
       <h2>Create New User</h2> 
       <p> 
        <label for="fname">First Name:</label> 
        <input id="fname" type="text" class="textbox" name="firstName" value="" /> 
       </p> 
       <p> 
        <label for="lname">Last Name:</label> 
        <input id="lname" type="text" class="textbox" name="lastName" value="" /> 
       </p> 
       <p> 
        <label for="uname">Username:</label> 
        <input id="uname" type="text" class="textbox" name="username" value="" /> 
       </p> 
       <p> 
        <label for="pword">Password:</label> 
        <input id="pword" type="password" class="textbox" name="password" value="" /> 
       </p> 
       <p> 
        <label for="pword">Confirm Password:</label> 
        <input id="pword" type="password" class="textbox" name="confirm" value="" /> 
       </p> 
<!--    <p>--> 
<!--     <label for="profile">Profile Picture:--> 
<!--     <input id="fileField" type="file" name="fileField"></label>--> 
<!--    </p>--> 
       <input type="submit" class="button" name="submit" value="Add User" onclick="return confirm('Do you wish to add a new user?');"/> 
       <p><a href="manage_users.php" class="link">Cancel</a></p> 
      </form> 
     </div> 

코드 :

<table id="users"> 
       <tr> 
        <th style="text-align: left; width: 75px;">Image</th> 
        <th style="text-align: left; width: 150px;">First Name</th> 
        <th style="text-align: left; width: 150px;">Last Name</th> 
        <th style="text-align: left; width: 150px;">Username</th> 
        <th style="text-align: left; width: 35px;">Actions</th> 
       </tr> 
       <?php while($user = mysqli_fetch_assoc($user_set)) { ?> 
        <tr> 
         <td> 
         <?php 

          if($user["image"] == ""){ 
          echo "<img id='profilepic' src='images/profile_pictures/default.gif' alt='Default Profile Pic'>"; 
          } else{ 
          echo "<img id='profilepic' src='images/profile_pictures/".$user['image']."' />"; 
          }?> 
         </td> 
         <!--htmlentities converts all applicable characters to html entities--> 
         <td><?php echo htmlentities($user["firstName"]); ?></td> 
         <td><?php echo htmlentities($user["lastName"]); ?></td> 
         <td><?php echo htmlentities($user["username"]); ?></td> 
         <td style="text-align: center;"><a href="edit_users.php?id=<?php echo urlencode($user["userId"]); ?>"> 
           <img id="icon" src="images/edit.png" alt="Edit Icon"></a> 
         <a href="delete_user.php?id=<?php echo urlencode($user["userId"]); ?>" onclick="return confirm('Are you sure?');"> 
          <img id="icon" src="images/delete.png" alt="Delete Icon"></a></td> 
        </tr> 
       <?php } ?> 
      </table> 
+1

당신은 이미지의 위치 대신에 데이터베이스의 이미지 자체를 저장할 수 있습니다

이 시도해보십시오. 이론적으로 이미지가 업로드되었는지 확인하십시오. 셀에 기본 사진의 위치를 ​​저장하지 않은 경우 – CAO

답변

0

가 데이터를 표시하는 NULL 대신 코드에서 if($user["image"] == "")의를 사용하여 시도 적이 있습니까? if(!$user["image"])

+0

답장을 보내 주셔서 감사합니다. 예, 저도 해봤지만 아무 것도 선택되지 않았는데도 데이터베이스에 들어가는 것처럼 보입니까? 그래서 실제로 null이 아닙니다. – Kate

+0

그런 다음 코드를 반전하십시오. 'if'부분을 양식에 넣어보십시오. 그러면 사용자가 이미지를 사용하지 않을 때 기본 부분을 얻을 수 있습니다. 표시해야하는 경우 더 이상 중요하지 않으며 기본 또는 사용자 지정입니다. 데이터베이스에서 정보를 선택하기 만하면됩니다. – Clover