2017-12-09 3 views
0

로그인 한 사용자가 추가 한 파일을 표시하는 데 문제가 있습니다. 변수를 SQL 쿼리에 올바르게 전달하는 방법을 모르겠습니다. 아무도 도와 줄 수 있습니까?PHP에서 sql을 사용하여 세션 변수를 사용하는 방법

현재 코드는 다음과 같습니다

<?php 
include_once 'dbconnect.php'; 
?> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>File Uploading With PHP and MySql</title> 
<link rel="stylesheet" href="style.css" type="text/css" /> 
</head> 
<body> 
<div id="header"> 
<label>File Uploading With PHP and MySql</label> 
</div> 
<div id="body"> 
<table width="80%" border="1"> 
    <tr> 
    <th colspan="4">your uploads...<label><a href="index.php">upload new files...</a></label></th> 
    </tr> 
    <tr> 
    <td>File Name</td> 
    <td>File Type</td> 
    <td>File Size(KB)</td> 
    <td>View</td> 
    </tr> 
    <?php 
$sql="SELECT * FROM files"; 
$result_set=mysql_query($sql); 
while($row=mysql_fetch_array($result_set)) 
{ 
    ?> 
     <tr> 
     <td><?php echo $row['file'] ?></td> 
     <td><?php echo $row['type'] ?></td> 
     <td><?php echo $row['size'] ?></td> 
     <td><a href="uploads/<?php echo $row['file'] ?>" target="_blank">view file</a></td> 
     </tr> 
     <?php 
} 
?> 
    </table> 

</div> 
</body> 
</html> 

나는이 레코드 변경하려고 :

$sql="SELECT * FROM files";

$sql="SELECT file, type, size FROM files WHERE userId ='$_SESSION[userId]'";

을하지만 난 아직도를 올바른 r을 얻지 못한다. esult. 누구든지 도와 줄 수 있습니까?

+0

mysql은 더 이상 사용되지 않으며 대신 mysqli를 사용하십시오. –

+0

무슨 뜻인지 구체적으로 말씀해 주시겠습니까? – martyniuk

+0

https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php –

답변

1

$ _SESSION 변수를 포함하는 방법에 문제가있는 것 같습니다. 너는 userId 주위에 따옴표가 $_SESSION['userId'] 또는 {$_SESSION['userId']} 같이 있어야합니다.

더 중요한 것은 MySQL 쿼리에 직접 변수를 입력하지 않아야한다는 것입니다. MySQL 대신 MySQLi 또는 PDO을 사용하고 준비된 문 (예 : here 또는 here)을 조사하는 것이 좋습니다.

관련 문제